Sitelet https://github.com/MessagePack-CSharp/MessagePack-CSharp/pull/2287
Skip to content

Fix OldSpec byte[] encoding emitting unsupported str8 headers - #2287

Merged
AArnott merged 4 commits into
masterfrom
copilot/fix-messagepackwriter-issue
Aug 4, 2026
Merged

AArnott merged 4 commits into
masterfrom
copilot/fix-messagepackwriter-issue

Conversation

Copilot AI commented Jul 31, 2026 •

Copy link
Copy Markdown
Contributor

With MessagePackWriter.OldSpec = true, writing a byte[] of length 32–255 incorrectly emitted the post-2013 str8 marker (0xD9), which legacy MessagePack readers cannot decode.

Cause

WriteBinHeader correctly delegated to WriteStringHeader in old-spec mode, but WriteStringHeader always used MessagePackPrimitives.TryWriteStringHeader, which selects str8 for those lengths. The string write path already avoided str8 via WriteString_PostEncoding; header writes did not.

Changes

  • WriteStringHeader: when OldSpec is set and length is 32–255, emit str16 (0xDA) instead of str8
  • Docs: correct OldSpec remarks that listed Str8 as a valid old-spec encoding
  • Tests: cover writer/byte[] paths at the 31/32/255/256 boundaries and extend OldSpecBinaryFormatterTest with 32 and 255

Example

byte[] value = new byte[32];
Array.Fill(value, (byte)'A');
ArrayBufferWriter<byte> buffer = new();
MessagePackWriter writer = new(buffer) { OldSpec = true };
writer.Write(value);
writer.Flush();
// before: D92041...  (str8)
// after:  DA002041... (str16)

Copilot AI changed the title [WIP] Fix MessagePackWriter.OldSpec ignoring byte[] values for legacy compatibility Fix OldSpec byte[] encoding emitting unsupported str8 headers Jul 31, 2026
Copilot AI requested a review from AArnott July 31, 2026 01:39

@AArnott AArnott left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

@copilot review my feedback.

Comment thread src/MessagePack/MessagePackWriter.cs Outdated

Copilot AI commented Jul 31, 2026

Copy link
Copy Markdown
Contributor Author

@copilot review my feedback.

Addressed in 7ec5a75: the OldSpec str16 path now writes a constant 0 for the high length byte.

@AArnott
AArnott marked this pull request as ready for review July 31, 2026 01:48
Copilot AI requested a review from AArnott July 31, 2026 01:48
@AArnott
AArnott requested a review from neuecc July 31, 2026 01:48
@AArnott AArnott added this to the v3.1 milestone Jul 31, 2026
This was referenced Sep 28, 2026
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.

MessagePackWriter.OldSpec is ignored for byte[] values in v3

3 participants