Repository navigation
Refuse to export the Packer buffer during pack - #770
Open
CAOShurong wants to merge 1 commit into
Open
CAOShurong wants to merge 1 commit into
CAOShurong wants to merge 1 commit into
Conversation
The C Packer accepted getbuffer() from a default callback, from dict.items(), and from pack_map_pairs() while it was still writing. Growing the buffer then freed the exported memory. Those methods now reject the export until they return. pytest -v test: 181 passed, 1 skipped. MSGPACK_PUREPYTHON=1 pytest -v test: 178 passed, 4 skipped. The pure Python buffer is not freed when it grows.
This branch has not been deployed
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.
The C Packer accepts getbuffer() while pack() is still writing. That happens from the default callback, from a dict subclass''s items(), and from pack_map_pairs(). _check_exports() runs only at the start of a public method, so the export is invisible to it. If packing then grows the buffer, PyMem_Realloc frees the block the memoryview still references.
pack(), pack_map_pairs(), and pack_ext_type() now count nested calls, and the buffer protocol raises BufferError until they return. An export taken between calls is unchanged, and �ytes() still copies. #735 checks exports after the default callback only, so the items() and pack_map_pairs() paths remain open there.
On Windows with Python 3.13, the extension was built with cython msgpack/_cmsgpack.pyx and installed with pip install -e .. The new tests failed on the unmodified extension and pass after this change. pytest -v test: 181 passed, 1 skipped. MSGPACK_PUREPYTHON=1 pytest -v test: 178 passed, 4 skipped.
The pure Python Packer is unchanged. Its buffer is a BytesIO, so growth does not free an existing view. pack_map_pairs() still keeps a partial header when iteration raises. This was not run under AddressSanitizer.
Fixes #769.