Skip to content

Commit 0297b36

Browse files
committed
Fix reading more than read_size.
1 parent 56ec7ee commit 0297b36

File tree

1 file changed

+14
-17
lines changed

1 file changed

+14
-17
lines changed

msgpack/_msgpack.pyx

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -278,8 +278,8 @@ cdef class Unpacker(object):
278278
279279
`unicode_errors` is used for decoding bytes.
280280
281-
`max_buffer_size` limits size of data waiting unpacked. 0 means unlimited
282-
(default).
281+
`max_buffer_size` limits size of data waiting unpacked.
282+
0 means system's INT_MAX (default).
283283
Raises `BufferFull` exception when it is insufficient.
284284
You shoud set this parameter when unpacking data from untrasted source.
285285
@@ -332,11 +332,11 @@ cdef class Unpacker(object):
332332
raise ValueError("`file_like.read` must be a callable.")
333333
if not max_buffer_size:
334334
max_buffer_size = INT_MAX
335+
if read_size > max_buffer_size:
336+
raise ValueError("read_size should be less or equal to max_buffer_size")
335337
if not read_size:
336338
read_size = min(max_buffer_size, 1024**2)
337339
self.max_buffer_size = max_buffer_size
338-
if read_size > max_buffer_size:
339-
raise ValueError("read_size should be less or equal to max_buffer_size")
340340
self.read_size = read_size
341341
self.buf = <char*>malloc(read_size)
342342
if self.buf == NULL:
@@ -419,18 +419,15 @@ cdef class Unpacker(object):
419419
self.buf_size = buf_size
420420
self.buf_tail = tail + _buf_len
421421

422-
# prepare self.buf from file_like
423-
cdef fill_buffer(self):
424-
if self.file_like is not None:
425-
next_bytes = self.file_like_read(
426-
max(self.read_size,
427-
self.max_buffer_size - (self.buf_tail - self.buf_head)
428-
))
429-
if next_bytes:
430-
self.append_buffer(PyBytes_AsString(next_bytes),
431-
PyBytes_Size(next_bytes))
432-
else:
433-
self.file_like = None
422+
cdef read_from_file(self):
423+
next_bytes = self.file_like_read(
424+
min(self.read_size,
425+
self.max_buffer_size - (self.buf_tail - self.buf_head)
426+
))
427+
if next_bytes:
428+
self.append_buffer(PyBytes_AsString(next_bytes), PyBytes_Size(next_bytes))
429+
else:
430+
self.file_like = None
434431

435432
cpdef unpack(self):
436433
"""unpack one object"""
@@ -443,7 +440,7 @@ cdef class Unpacker(object):
443440
return o
444441
elif ret == 0:
445442
if self.file_like is not None:
446-
self.fill_buffer()
443+
self.read_from_file()
447444
continue
448445
raise StopIteration("No more unpack data.")
449446
else:

0 commit comments

Comments
 (0)