Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions msgpack/_packer.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,11 @@ cdef class Packer:
raise TypeError("default must be a callable.")
self._default = default

# Clear the borrowed pointer first: converting a new value may fail
# after the old _berrors has been released.
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
Expand Down
16 changes: 10 additions & 6 deletions msgpack/_unpacker.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -344,15 +344,17 @@ cdef class Unpacker:
Py_ssize_t max_map_len=-1,
Py_ssize_t max_ext_len=-1):
cdef const char *cerr=NULL
cdef char *new_buf

if self._unpacking:
raise RuntimeError("Unpacker.__init__() cannot be called while unpacking is in progress")

unpack_clear(&self.ctx)
unpack_init(&self.ctx)
if self.buf != NULL:
PyMem_Free(self.buf)
self.buf = NULL
# The references below are replaced before init_ctx() runs, and the
# arguments may still be rejected, so don't keep borrowed pointers.
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
Expand Down Expand Up @@ -383,11 +385,13 @@ cdef class Unpacker:
if not read_size:
read_size = min(max_buffer_size, 1024**2)

new_buf = <char*>PyMem_Malloc(read_size)
if new_buf == NULL:
raise MemoryError("Unable to allocate internal buffer.")
PyMem_Free(self.buf)
self.buf = new_buf
self.max_buffer_size = max_buffer_size
self.read_size = read_size
self.buf = <char*>PyMem_Malloc(read_size)
if self.buf == NULL:
raise MemoryError("Unable to allocate internal buffer.")
self.buf_size = read_size
self.buf_head = 0
self.buf_tail = 0
Expand Down
12 changes: 12 additions & 0 deletions test/test_pack.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,18 @@ def testIgnoreErrorsPack():
assert re == "abcdef"


@pytest.mark.skipif(
Packer.__module__ == "msgpack.fallback", reason="specific to C extension reinit"
)
def testFailedReinitDropsUnicodeErrors():
packer = Packer(unicode_errors="".join(["ign", "ore"]))
assert packer.pack("abc\udc80") == b"\xa3abc"
with pytest.raises(TypeError):
packer.__init__(unicode_errors=1)
with pytest.raises(UnicodeEncodeError):
packer.pack("abc\udc80")


def testDecodeBinary():
re = unpackb(packb(b"abc"), use_list=1)
assert re == b"abc"
Expand Down
30 changes: 30 additions & 0 deletions test/test_unpack.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,36 @@ def hook(code, data):
assert unpacker.unpack() == {"a": 1}


@mark.skipif(
Unpacker.__module__ == "msgpack.fallback",
reason="specific to C extension reinit",
)
def test_unpacker_failed_reinit_drops_hooks():
class Hook:
def __call__(self, obj):
return obj

hook = Hook()
hook_ref = weakref.ref(hook)
unpacker = Unpacker(object_hook=hook)
del hook

# Both are rejected partway through __init__, after the old hook is dropped.
with raises(TypeError):
unpacker.__init__(unicode_errors=1)
with raises(ValueError):
unpacker.__init__(read_size=10, max_buffer_size=5)
gc.collect()
assert hook_ref() is None

unpacker.feed(b"\x80")
assert unpacker.unpack() == {}

unpacker.__init__()
unpacker.feed(packb({"a": 1}))
assert unpacker.unpack() == {"a": 1}


@mark.skipif(
Unpacker.__module__ == "msgpack.fallback",
reason="reentrant guard is implemented in C extension only",
Expand Down
Loading