◐ Shell
reader mode source ↗
Skip to content

Add missing autoreset in Packer.pack_ext_type#663

Merged
methane merged 3 commits into
msgpack:mainfrom
bysiber:fix/pack-ext-type-autoreset
Jun 1, 2026
Merged

Add missing autoreset in Packer.pack_ext_type#663
methane merged 3 commits into
msgpack:mainfrom
bysiber:fix/pack-ext-type-autoreset

Conversation

@bysiber

@bysiber bysiber commented Feb 20, 2026

Copy link
Copy Markdown
Contributor

Packer.pack_ext_type() writes to the internal buffer but never checks self._autoreset. Every other public pack method (pack, pack_map_pairs, pack_array_header, pack_map_header) has this pattern:

if self._autoreset:
    ret = self._buffer.getvalue()
    self._buffer = BytesIO()
    return ret

Without it, pack_ext_type() always returns None, and the packed ext data stays in the buffer. The next call to pack() then returns both the extension data and the new value concatenated together, corrupting the serialized stream.

packer = msgpack.Packer()
result = packer.pack_ext_type(5, b'\x01\x02\x03')
# result is None, expected bytes

packer.pack(99)
# Returns 7 bytes (ext header + ext data + int) instead of 1 byte

pack_ext_type writes to the internal buffer but never checks
self._autoreset, unlike every other public pack method. This means
it always returns None and the packed data leaks into the output of
the next pack() call, corrupting the serialized stream.

Add the same autoreset pattern used by pack(), pack_map_pairs(),
pack_array_header(), and pack_map_header().

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.

Hide comment

Pull request overview

This PR addresses a long-standing behavior mismatch in Packer.pack_ext_type() where the pure-Python fallback implementation writes to the internal buffer but doesn’t honor self._autoreset, causing pack_ext_type() to return None and potentially corrupt subsequent packed output.

Changes:

  • Add autoreset handling to msgpack/fallback.py:Packer.pack_ext_type() so it returns bytes and clears the internal buffer when self._autoreset is enabled.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@methane

methane commented Apr 27, 2026

Copy link
Copy Markdown
Member

@copilot apply changes based on the comments in this thread

Hide details View details @methane methane merged commit 284782d into msgpack:main Jun 1, 2026
26 checks passed
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.

3 participants