Repository navigation
Conversation
Packer only checked for live buffer exports on entry to pack(), pack_map_pairs() and pack_ext_type(). Python code that runs during packing (a default callback, the pairs iterable, a list or dict subclass) could call memoryview(packer), after which msgpack_pack_write() reallocated pk.buf and left the export pointing at freed memory. Refuse exports from __getbuffer__ while one of those methods is running. The pure Python Packer already raises BufferError in this case because BytesIO cannot be resized while exported.
|
Please disallow reentrant calls to |
|
Independent ASan verification of this approach (HEAD I applied this patch ( For comparison, unpatched HEAD and PR #735 both produced ASan |
The Cython
Packerchecks for live buffer exports only on entry topack(),pack_map_pairs()andpack_ext_type(). Python code that runs mid-pack (adefaultcallback, thepairsiterable, a list or dict subclass) can callmemoryview(packer), and the nextmsgpack_pack_write()that grows the bufferPyMem_Reallocspk.bufunder that view. WithPacker(buf_size=16, autoreset=False, default=...)wheredefaultstoresmemoryview(packer)and returnsb"x" * 65536, the stored view readsb' 'instead of theb'\x91'array header, i.e. freed heap memory.__getbuffer__now raisesBufferErrorwhile one of those three methods is running, using a nesting counter so a reentrantpack()from a callback doesn't clear the outer guard. The pure PythonPackeralready raisesBufferErrorhere becauseBytesIOrefuses to resize while exported, so the new test intest_buffer.pypasses on both implementations and fails on the unpatched C extension.