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
30 changes: 23 additions & 7 deletions msgpack/_packer.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ cdef class Packer:
cdef bint use_float
cdef bint autoreset
cdef bint datetime
cdef bint _packing

def __cinit__(self, size_t buf_size=256*1024, **_kwargs):
self.pk.buf = <char*> PyMem_Malloc(buf_size)
Expand All @@ -128,6 +129,10 @@ cdef class Packer:
if self.exports > 0:
raise BufferError("Existing exports of data: Packer cannot be changed")

cdef _check_packing(self):
if self._packing:
raise RuntimeError("Packer cannot be called recursively while packing is in progress")

@cython.critical_section
def __init__(self, *, default=None,
bint use_single_float=False, bint autoreset=True, bint use_bin_type=True,
Expand Down Expand Up @@ -273,11 +278,15 @@ cdef class Packer:
def pack(self, object obj):
cdef int ret
self._check_exports()
self._check_packing()
self._packing = True
try:
ret = self._pack(obj, DEFAULT_RECURSE_LIMIT)
except:
self.pk.length = 0
raise
finally:
self._packing = False
if ret: # should not happen.
raise RuntimeError("internal error")
if self.autoreset:
Expand Down Expand Up @@ -332,13 +341,18 @@ cdef class Packer:
(`len(pairs)` and `for k, v in pairs:` should be supported.)
"""
self._check_exports()
size = len(pairs)
if size > ITEM_LIMIT:
raise ValueError("map too large")
msgpack_pack_map(&self.pk, size)
for k, v in pairs:
self._pack(k)
self._pack(v)
self._check_packing()
self._packing = True
try:
size = len(pairs)
if size > ITEM_LIMIT:
raise ValueError("map too large")
msgpack_pack_map(&self.pk, size)
for k, v in pairs:
self._pack(k)
self._pack(v)
finally:
self._packing = False
if self.autoreset:
buf = PyBytes_FromStringAndSize(self.pk.buf, self.pk.length)
self.pk.length = 0
Expand Down Expand Up @@ -367,6 +381,8 @@ cdef class Packer:

@cython.critical_section
def __getbuffer__(self, Py_buffer *buffer, int flags):
if self._packing:
raise BufferError("Packer buffer cannot be exported while packing is in progress")
PyBuffer_FillInfo(buffer, self, self.pk.buf, self.pk.length, 1, flags)
self.exports += 1

Expand Down
28 changes: 27 additions & 1 deletion test/test_buffer.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from pytest import raises
from pytest import mark, raises

from msgpack import Packer, packb, unpackb

Expand Down Expand Up @@ -47,3 +47,29 @@ def test_packer_getbuffer():
buffer.release()
packer.pack(42)
assert bytes(packer) == b"\x92*\xa5hello*"


@mark.skipif(
Packer.__module__ == "msgpack.fallback",
reason="reentrant guard is implemented in C extension only",
)
def test_packer_reentrant_default():
def default(o):
# touch the SAME packer, then return enough data to force a realloc
o()
return b"x" * (1 << 20)

def export():
memoryview(packer)

def pack():
packer.pack(1)

packer = Packer(default=default, buf_size=1024)
with raises(BufferError):
packer.pack([b"A" * 512, export])
with raises(BufferError):
packer.pack_map_pairs([(1, export)])
with raises(RuntimeError):
packer.pack([b"A" * 512, pack])
assert packer.pack([1, 2]) == b"\x92\x01\x02"
Loading