Skip to content

Fix ArgumentNullException in Decoder.Convert and fix secure hash of ROS<byte> - #1001

Merged
AArnott merged 3 commits into
v1.2from
fix-empty-segments
Jul 6, 2026
Merged

Fix ArgumentNullException in Decoder.Convert and fix secure hash of ROS<byte>#1001
AArnott merged 3 commits into
v1.2from
fix-empty-segments

Conversation

@AArnott

@AArnott AArnott commented Jul 6, 2026

Copy link
Copy Markdown
Owner

Fix ArgumentNullException thrown from Decoder.Convert

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.

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 SipHash so that it properly supports streaming hashing such that the boundaries of each segment do not impact the resulting value.

AArnott added 2 commits July 6, 2026 13:08
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.
@AArnott AArnott added this to the 1.2 milestone Jul 6, 2026
Copilot AI review requested due to automatic review settings July 6, 2026 19:51

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 SipHash to 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.

Comment thread src/Nerdbank.MessagePack/SecureHash/SipHash.cs
Comment thread src/Nerdbank.MessagePack/SecureHash/SipHash.cs
Comment thread src/Nerdbank.MessagePack/SecureHash/SipHash.cs Outdated
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.
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.

2 participants