Skip to content

refuse buffer export and re-entry while Packer is packing - #775

Open
kitkatt7 wants to merge 1 commit into
msgpack:mainfrom
kitkatt7:packer-reentrant-guard
Open

kitkatt7 wants to merge 1 commit into
msgpack:mainfrom
kitkatt7:packer-reentrant-guard

Conversation

@kitkatt7

Copy link
Copy Markdown

the C Packer only checks for exported buffers when pack() starts, but default (and the iteration in pack_map_pairs) runs arbitrary python while packing. if that code calls memoryview(packer), the outer pack keeps writing and msgpack_pack_write reallocs pk.buf, so the view is left pointing at freed memory. with PYTHONMALLOC=debug, a default that grabs the view and returns b"x" * (1 << 20) on Packer(default=default, buf_size=1024) gives a view that reads back 0xdd bytes. calling packer.pack() from default has a related problem: autoreset zeroes pk.length under the outer call and its output is silently corrupted. this adds a _packing flag set by pack() and pack_map_pairs(), the same way #704 guarded Unpacker. getbuffer raises BufferError and re-entry raises RuntimeError while the flag is set. pack_ext_type is left alone because #765 already removes the len() call, which was the only python it ran. the fallback writes through BytesIO, which already refuses resizing while exported, so the new test is C-only. the test fails on main and passes with the change; pytest passes for both the C and pure-python builds.

This branch has not been deployed

No deployments
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.

1 participant