Skip to content

More fixes in Serialize, RingArray and friends - #3256

Merged
harendra-kumar merged 18 commits into
masterfrom
ring-serialize-fixes
Oct 15, 2026
Merged

harendra-kumar merged 18 commits into
masterfrom
ring-serialize-fixes

Conversation

@harendra-kumar

Copy link
Copy Markdown
Member

Code review fixes, mostly in Serialize and RingArray, plus some CI fixes.

Serialize

  • () and Proxy now actually write their byte, instead of leaving it uninitialized. Serializing the same value now always gives the same bytes.
  • Proxy and derived unit types now check the buffer bounds. Before, a corrupt list length could make deserialization run past the end of the buffer, or loop forever with a length of -1.
  • Deserializing a list or an Array with a bad length (negative, or too large for the buffer) now fails cleanly instead of reading garbage or crashing later.
  • New docs in Streamly.Data.MutByteArray on when to use Unbox and when to use Serialize. The old claim that Serialize is a superset of Unbox is removed. A stray blank line was also hiding the second half of that module's haddocks, so that is fixed too.

RingArray

  • getIndex compared the index with the ring size in bytes, not the element count, so out-of-range indices wrapped around. getIndex 5 on a 5-element ring returned the head element.
  • cast now also checks the head alignment. Before, elements could straddle the end of the ring and reads went past the end of the memory.
  • fold on an empty ring folded one garbage element: fold Fold.toList gave [0] instead of [].
  • eqArray and eqArrayN ignored the array's start offset, so comparing with a slice gave wrong results.

Misc

  • Show and Read for Array now work when nested: show (Just arr) can be read back again.
  • unsafeByteCmp now compares the bytes when it runs, not lazily when the result is evaluated.

CI

  • FreeBSD bumped to 14.5.
  • The Handle tests close their Handles and run serially on the JS backend.
  • The flaky concurrent DirIO tests are skipped on JS.

show (Just arr) produced "Just fromList [...]" which cannot be read
back, and Read failed on parenthesized input or leading whitespace. Use
showsPrec with showParen, and parens/prec/lexP in readPrec, the same as
the Stream instances.
unsafeByteCmp returned the result of compareByteArrays# as a thunk, the
bytes were compared when the result was evaluated. If either array was
modified in between, the result was that of the modified bytes.
The Serialize instances of () and Proxy advanced the offset by one byte
without writing it, leaving the byte uninitialized. The Serialize class
guarantees that the same value always serializes to the same bytes, which
these instances did not satisfy. Write a 0 to the byte.

Document which bytes Unbox does not write ("Unwritten bytes" in
Streamly.Internal.Data.Unbox), the deterministic encoding of Serialize and
its unit types ("Deterministic encoding" in
Streamly.Internal.Data.Serialize.Type), and the user facing summary in
Streamly.Data.MutByteArray. The Serialize instance of Array copies the
Unbox bytes of its elements and is not deterministic for element types whose
Unbox instance leaves bytes unwritten, add a TODO for it.
The Serialize instance of Proxy and the derived instances of types with
a single constructor without fields did not check the buffer bounds, they
deserialized a value one byte beyond the end of the buffer. A list of
such values with a corrupt length deserialized past the end of the buffer
without an error, a length of -1 never terminated.
Deserializing a list with a negative length did not stop at zero
elements, it read elements until it ran past the end of the buffer, or
never terminated if the elements took no space to deserialize.
Deserializing an Array with a negative length passed the bounds check and
failed later with "newByteArray: size must be >= 0". A length close to
maxBound made the end offset overflow and pass the bounds check. Check
for a negative length and compare the length with the remaining bytes in
the buffer.
getIndex compared the element index with the ring size in bytes, so an
index from the length up to the byte size wrapped around instead of
returning Nothing. getIndex 5 on a 5-element Int ring returned the head
element.
cast checked only that the ring byte length is a multiple of the target
element size. A ring head at a byte offset that is not a multiple of the
target element size makes elements straddle the end of the ring, so
reads go past the end of the memory. cast now returns Nothing in that
case.
fold read the element at the head before checking the ring size, so a
zero-sized ring folded one garbage element. createOfLast returns a
zero-sized ring for an empty stream, so RingArray.fold Fold.toList on
it returned [0] instead of []. foldlM' uses fold and had the same
problem.
eqArray and eqArrayN compared the ring with the bytes from offset 0 of
the array's memory instead of from the array's start offset, so they
gave wrong results when the array is a slice.
The read tests and the writeChunks test did not close the Handles they
opened, the finalizers closed them later at arbitrary times. On the JS
backend before GHC 9.14.1, a close can remove the table entry of a file
descriptor reused by a file opened at the same time (h$base_closeFile in
base.js), closing that file then fails with EINVAL. A close from a finalizer
races with the test running at that time even when the tests run serially,
and a close in a parallel test races with an open in another. Close all the
Handles in the tests and run the tests sequentially on JS before GHC 9.14.
The listDir*Par* tests crash the JS runtime intermittently, with
"h$ap_1_1_fast: unexpected closure type: 2" or "TypeError: Cannot read
properties of undefined (reading 't')". Mark them pending on JS.
Add a section to Streamly.Data.MutByteArray on choosing between Unbox and
Serialize: the purpose, the supported types, bounds checks, word size,
deterministic bytes, schema options and byte order. Remove the claim that
Serialize is a superset of Unbox. The Unbox and Serialize class docs point
to this section.

A blank line ended the module header doc, the sections after the FFI
section were not rendered by haddock. Join them to the module header.
Git for Windows 2.56 moved /mingw64 to /ucrt64, so curl and other
tools were not found in the PATH passed to packcheck.

The macOS image ships Command Line Tools 27.0 with default Xcode 26.6;
linking against the CLT SDK fails with a tapi "unknown architecture
arm64e.x1-macos" error. Select the default Xcode so that the compiler,
linker and SDK come from the same toolchain.

GHC records the SDK library path when it is installed, and the GHC
restored from the cache passed the CLT SDK path to the linker. Add the
Xcode version and build to the macOS GHC and dependency cache keys, so
that a runner image with a different Xcode installs GHC afresh.
With the default -j, 4 on the 4 CPU VM, the VM with 6 GB of memory
killed cabal while it was building fusion-plugin and other
dependencies in parallel. Limiting the parallelism keeps the job
within the default VM size.
The network package fails to build on the old Windows images that
AppVeyor uses, the AppVeyor webhook is deactivated. The GitHub Actions
Windows build covers Windows.
The x86_64 stack binary that packcheck downloaded ran under Rosetta on
the arm64 macOS runner and installed an x86_64 GHC, whose link failed
with the Command Line Tools SDK.
@harendra-kumar
harendra-kumar merged commit ae94ed1 into master Oct 15, 2026
18 of 19 checks passed
@harendra-kumar
harendra-kumar deleted the ring-serialize-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