Repository navigation
Conversation
|
Independent ASan verification of this approach (HEAD I applied this patch and ran a stateful fuzz where Two notes:
Happy to share the reproducer or traces if useful. |
pack_ext_type, pack_array_header and pack_map_header also append to pk.buf, so calling any of them from default under an outer pack() let autoreset drain pk.length and silently truncate the outer output instead of raising. check _packing in those three as well.
|
good catch on both counts, and you were right that I had the #765 reasoning backwards. #765 is indeed still open, and more to the point, removing the len() call wouldn't have helped here anyway: the problem isn't the python that len() runs, it's the write into the outer pack's buffer, so pack_ext_type needed the flag regardless. checking it I found pack_array_header and pack_map_header have the same hole, since they append to pk.buf too. all three were silently truncating instead of raising, e.g. packer.pack([1, object(), 2]) with a default that calls packer.pack_ext_type(1, b"E" * 16) came back as c4026f6b02 rather than 9301c4026f6b02, because the inner autoreset drains pk.length and the outer pack carries on writing from zero. pushed a commit that checks _packing in those three as well, plus a parametrised test over all five writers. both builds still pass. one thing I did look at and deliberately left alone: a top-level pack_ext_type can still hand out a memoryview from inside an overridden len, but the view comes back length 0 since nothing has been written yet, so there's nothing stale to read. that one is really #765's repeated-len() territory and I think it belongs there rather than here. and yes, please do post the harness if you don't mind. the reentrant pack_ext_type case is one my own test didn't reach, so it would be useful to run the stateful version against this. |
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, pack_array_header and pack_map_header append to pk.buf too, so they check the flag as well. calling any of them from default under an outer pack() used to let autoreset drain pk.length and silently truncate the outer output:
packer.pack([1, object(), 2])with a default that callspacker.pack_ext_type(1, b"E" * 16)returnedc4026f6b02instead of9301c4026f6b02. they now raise RuntimeError like pack() does. (an earlier version of this description claimed pack_ext_type was covered by #765 removing its len() call. that was wrong on two counts: #765 is still open, and dropping the len() call wouldn't help here anyway, since the problem is the write into the outer buffer rather than the python that len() runs.)the fallback writes through BytesIO, which already refuses resizing while exported, so the new tests are C-only. they fail on main and pass with the change; pytest passes for both the C and pure-python builds.