Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Fix Unpacker max_buffer_length handling #506

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
May 24, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Unpacker raises BufferFull when max_buffer_size is not enough.
  • Loading branch information
methane committed May 24, 2022
commit f233d0bcec6ac0f6ccf72512374ad47479a25f30
21 changes: 11 additions & 10 deletions 21 msgpack/_unpacker.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -440,15 +440,17 @@ cdef class Unpacker(object):
self.buf_size = buf_size
self.buf_tail = tail + _buf_len

cdef read_from_file(self):
next_bytes = self.file_like_read(
min(self.read_size,
self.max_buffer_size - (self.buf_tail - self.buf_head)
))
cdef int read_from_file(self) except -1:
cdef Py_ssize_t remains = self.max_buffer_size - (self.buf_tail - self.buf_head)
if remains <= 0:
raise BufferFull

next_bytes = self.file_like_read(min(self.read_size, remains))
if next_bytes:
self.append_buffer(PyBytes_AsString(next_bytes), PyBytes_Size(next_bytes))
else:
self.file_like = None
return 0

cdef object _unpack(self, execute_fn execute, bint iter=0):
cdef int ret
Expand All @@ -471,11 +473,10 @@ cdef class Unpacker(object):
if self.file_like is not None:
self.read_from_file()
continue
if prev_head == self.buf_tail:
if iter:
raise StopIteration("No more data to unpack.")
else:
raise OutOfData("No more data to unpack.")
if iter:
raise StopIteration("No more data to unpack.")
else:
raise OutOfData("No more data to unpack.")
elif ret == -2:
raise FormatError
elif ret == -3:
Expand Down
11 changes: 10 additions & 1 deletion 11 test/test_sequnpack.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# coding: utf-8
import io
from msgpack import Unpacker, BufferFull
from msgpack import pack
from msgpack import pack, packb
from msgpack.exceptions import OutOfData
from pytest import raises

Expand Down Expand Up @@ -78,6 +78,15 @@ def test_maxbuffersize():
assert ord("b") == next(unpacker)


def test_maxbuffersize_file():
buff = io.BytesIO(packb(b"a"*10) + packb(b"a"*100))
unpacker = Unpacker(buff, read_size=1, max_buffer_size=99)
assert unpacker.unpack() == b"a"*10
#assert unpacker.unpack() == b"a"*100
with raises(BufferFull):
unpacker.unpack() == b"a"*100


def test_readbytes():
unpacker = Unpacker(read_size=3)
unpacker.feed(b"foobar")
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.