Fix ArgumentNullException in Decoder.Convert and fix secure hash of ROS<byte> - #1001
Merged
Conversation
When a `ReadOnlySequence<byte>` that includes an empty `ReadOnlyMemory<byte>` segment is deserialized, the string interning converter may end up throwing `ArgumentNullException`. This fixes that bug.
Contributor
There was a problem hiding this comment.
Pull request overview
This PR addresses two correctness issues in the serialization stack: (1) decoding/interning strings from fragmented ReadOnlySequence<byte> inputs that include empty segments, and (2) computing collision-resistant hashes over ReadOnlySequence<byte> values in a way that is independent of segment boundaries.
Changes:
- Add/extend tests to cover
ReadOnlySequence<byte>inputs that include empty segments and to validate segment-boundary-independent hashing. - Update
Encoding.GetChars(ReadOnlySequence<byte>, ...)polyfill to safely handle empty sequences/segments during incremental decoding. - Refactor
SipHashto support incremental hashing over multi-segment sequences and update secure hashing call sites to use it.
Reviewed changes
Copilot reviewed 10 out of 10 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| test/Nerdbank.MessagePack.Tests/StructuralEqualityComparerTests.cs | Expands equality/hash tests for ReadOnlySequence<byte> across segmented and empty-segment cases. |
| test/Nerdbank.MessagePack.Tests/StringInterningTests.cs | Adds coverage for fragmented sequences that include an empty segment during string interning. |
| test/Nerdbank.MessagePack.Tests/SequenceBuilder.cs | Introduces a test helper to build ReadOnlySequence<T> instances that retain empty segments. |
| test/Nerdbank.MessagePack.Tests/MessagePackReaderTests.ReadString.cs | Switches to SequenceBuilder and adds a reader test including an empty segment. |
| test/Nerdbank.MessagePack.Tests/MessagePackReaderTests.cs | Switches fragmented-sequence construction to SequenceBuilder. |
| test/Nerdbank.MessagePack.Tests/ArraysOfPrimitivesTests.cs | Adds a bool[] roundtrip test using a sequence with an empty segment. |
| src/Nerdbank.MessagePack/SecureHash/SipHash.cs | Adds incremental/streaming SipHash implementation and a ReadOnlySequence<byte> overload. |
| src/Nerdbank.MessagePack/SecureHash/HashCollisionResistantPrimitives.cs | Simplifies ReadOnlySequence<byte> secure hashing by delegating to SipHash.Compute(sequence). |
| src/Nerdbank.MessagePack/PolyfillExtensions.cs | Avoids Decoder.Convert calls with empty segments and handles empty sequences. |
| src/Nerdbank.MessagePack/Extension.cs | Updates secure hashing of Extension payloads to hash the ReadOnlySequence<byte> directly. |
Previously, it was producing multiple hashes for 'equal' sequences based on where the segment boundaries fell. This change updates `SipHash` so that it properly supports streaming hashing such that the boundaries of each segment do not impact the resulting value.
AArnott
force-pushed
the
fix-empty-segments
branch
from
July 6, 2026 20:15
f586459 to
61f253c
Compare
AArnott
enabled auto-merge
July 6, 2026 20:17
This was referenced Jul 6, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fix ArgumentNullException thrown from Decoder.Convert
When a
ReadOnlySequence<byte>that includes an emptyReadOnlyMemory<byte>segment is deserialized, the string interning converter may end up throwingArgumentNullException.This fixes that bug.
Fix secure hashing of
ReadOnlySequence<byte>Previously, it was producing multiple hashes for 'equal' sequences based on where the segment boundaries fell.
This change updates
SipHashso that it properly supports streaming hashing such that the boundaries of each segment do not impact the resulting value.