From 9b4483d19bad3875940b477e49d45c58873c3c7f Mon Sep 17 00:00:00 2001 From: obsidian_nomad Date: Sat, 21 Nov 2026 02:04:34 +0530 Subject: [PATCH] Clear borrowed C pointers before re-initializing Packer and Unpacker --- msgpack/_packer.pyx | 7 ++++--- msgpack/_unpacker.pyx | 5 +++++ test/test_pack.py | 14 ++++++++++++++ test/test_unpack.py | 14 ++++++++++++++ 4 files changed, 37 insertions(+), 3 deletions(-) diff --git a/msgpack/_packer.pyx b/msgpack/_packer.pyx index c44c6366..f77be9f8 100644 --- a/msgpack/_packer.pyx +++ b/msgpack/_packer.pyx @@ -143,10 +143,11 @@ cdef class Packer: raise TypeError("default must be a callable.") self._default = default + # Clear the borrowed pointer before replacing its owner so a failed + # conversion below cannot leave it pointing at a freed object. + self.unicode_errors = NULL self._berrors = unicode_errors - if unicode_errors is None: - self.unicode_errors = NULL - else: + if unicode_errors is not None: self.unicode_errors = self._berrors # returns -2 when default should(o) be called diff --git a/msgpack/_unpacker.pyx b/msgpack/_unpacker.pyx index 44f83f09..f90e4eff 100644 --- a/msgpack/_unpacker.pyx +++ b/msgpack/_unpacker.pyx @@ -353,6 +353,11 @@ cdef class Unpacker: if self.buf != NULL: PyMem_Free(self.buf) self.buf = NULL + self.buf_size = self.buf_head = self.buf_tail = 0 + # ctx.user borrows the objects below. Drop those pointers before the + # attributes are replaced so a failed __init__ cannot leave them dangling. + self.ctx.user.object_hook = self.ctx.user.list_hook = self.ctx.user.ext_hook = NULL + self.ctx.user.unicode_errors = NULL self.object_hook = object_hook self.object_pairs_hook = object_pairs_hook diff --git a/test/test_pack.py b/test/test_pack.py index 9ca6e182..e268a18b 100644 --- a/test/test_pack.py +++ b/test/test_pack.py @@ -220,3 +220,17 @@ def __int__(self): buf_size = Counting() Packer(buf_size=buf_size) assert buf_size.count == 1 + + +@pytest.mark.skipif( + Packer.__module__ == "msgpack.fallback", + reason="unicode_errors is converted to char* only in the C extension", +) +def test_failed_reinit_drops_old_unicode_errors(): + packer = Packer(unicode_errors="".join(["surrogate", "escape"])) + # The old error handler name is released before the new one fails to convert. + with pytest.raises(TypeError): + packer.__init__(unicode_errors=1) + assert packer.pack("abc") == packb("abc") + with pytest.raises(UnicodeEncodeError): + packer.pack("abc\udcff") diff --git a/test/test_unpack.py b/test/test_unpack.py index 03e49ca3..98faad03 100644 --- a/test/test_unpack.py +++ b/test/test_unpack.py @@ -185,3 +185,17 @@ def test_unpack_raises_extra_data_on_stream_with_trailing_bytes(): unpack(stream) assert exc_info.value.unpacked == 100 assert exc_info.value.extra == packb(200) + + +@mark.skipif( + Unpacker.__module__ == "msgpack.fallback", + reason="specific to C extension reinit", +) +def test_unpacker_failed_reinit_drops_old_hooks(): + unpacker = Unpacker(object_hook=lambda d: "hook") + # unicode_errors is converted after the old object_hook was released. + with raises(TypeError): + unpacker.__init__(unicode_errors=1) + gc.collect() + unpacker.feed(b"\x80") + assert unpacker.unpack() == {}