Repository navigation
Refuse to export the Packer buffer during pack - #770
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.
|
Independent ASan verification of this approach (HEAD I applied this patch ( For comparison, unpatched HEAD and PR #735 both produced ASan |
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.