Skip to content

Fix folds that wrap an inner fold, plus RingArray and Serialize leftovers - #3257

Merged
harendra-kumar merged 8 commits into
masterfrom
ring-fold-fixes
Oct 15, 2026
Merged

harendra-kumar merged 8 commits into
masterfrom
ring-fold-fixes

Conversation

@harendra-kumar

Copy link
Copy Markdown
Member

More fixes from the review pass, mostly folds that wrap an inner fold, plus a few RingArray and Serialize leftovers.

Folds:

  • bracketIO signalled Done one input late and dropped that input, e.g. foldBreak over take 2 on [1..5] left [4,5] instead of [3,4,5]. If the stream ended right there, the release action only ran on GC.
  • finallyIO only ran its action on GC when the inner fold finished early.
  • foldMaybes/foldEithers dropped the inner fold's state without calling its final.
  • refold used the deprecated extractM, so the source fold was never finalized and folds without an extract (e.g. splitWith) just errored.
  • Small doc fix for toStreamK/toStreamKRev.

RingArray / Serialize:

  • RingArray.Generic.copyToMutArray wrote past the end of the target array when the range didn't wrap around.
  • Derived deserializeAt could read beyond the buffer on truncated input (constructor names / record headers weren't bounds checked).
  • A few wrong RingArray error messages and a wrong moveReverse doc example.

Added tests for the fold and RingArray fixes; changelog entries for bracketIO and finallyIO.

When the requested range did not wrap around the end of the ring,
copyToMutArray still copied from the start index to the end of the ring
into an array of the requested length, writing past its end, and then
copied a negative number of elements. copyToMutArray 0 2 on a full
5-element ring failed an assertion in debug builds and corrupted memory
otherwise.
With encodeConstrNames, the derived deserializeAt compared the bytes of
the constructor names with the buffer without checking that the name
length read from the buffer fits in it. With encodeRecordFields, it
compared the record header with the buffer without checking that the
header fits in the buffer. A truncated or corrupt buffer made it read
beyond the end of the buffer.

When the constructor name does not fit, raise an error. When the record
header does not fit, take the slow path that reads the header field by
field with bounds checks.
The replace error message named insert, the eqArray error message named
eqArrayN, and the moveReverse doc example defined moveForward.
When a Nothing (foldMaybes) or a Left (foldEithers) input terminated the
fold, the state of the inner fold was dropped without calling its final.
A resource released in final, e.g. by finallyIO or bracketIO, was not
released until GC, or never for a fold with a plain effectful final.
For example, folding [Just 1, Nothing] with foldMaybes over a fold whose
final writes an IORef returned Nothing without writing the IORef.
refold used the deprecated extractM to get the initial value of the
Refold, calling the source fold's extract instead of its final. The
source fold was never finalized, and refold failed for folds whose
extract is an error, e.g. splitWith, split_, concatMap, longest and
duplicate. For example, refold Refold.sconcat over
splitWith (++) (fromPure [1]) (take 1 (foldl' (++) [])) failed with
"splitWith: cannot be used for scanning" instead of folding [[2]] to
[1,2].
The definitions in the docs used Data.Foldable's foldl' and Prelude's
foldr, which define functions on Foldable containers, not folds.
bracketIO stored the result of the inner fold's step, Partial or Done,
and always returned Partial. Done was returned only on the next input,
which was dropped: Stream.foldBreak with bracketIO over Fold.take 2 on
[1..5] left [4,5] instead of [3,4,5]. If the stream ended right after
the inner fold terminated, final returned the result without running
the release action, which then ran only on GC.
finallyIO ran the action only in final. When the inner fold returned
Done from initial or step, e.g. Fold.take 1 on [1,2,3], the action ran
only when the IORef holding it was garbage collected.
@harendra-kumar
harendra-kumar merged commit 2dc4882 into master Oct 15, 2026
17 of 19 checks passed
@harendra-kumar
harendra-kumar deleted the ring-fold-fixes branch October 21, 2026 11:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant