8000 python: Check if (m|re)alloc's return value is NULL. (Thanks to Mateusz) · scott2b/msgpack-python@47b0c27 · GitHub
[go: up one dir, main page]

8000
Skip to content
This repository was archived by the owner on Jan 18, 2024. It is now read-only.

Commit 47b0c27

Browse files
committed
python: Check if (m|re)alloc's return value is NULL. (Thanks to Mateusz)
1 parent 56aaed3 commit 47b0c27

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

msgpack/_msgpack.pyx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,7 @@ cdef class Unpacker(object):
239239

240240
def __dealloc__(self):
241241
free(self.buf);
242+
self.buf = NULL;
242243

243244
def __init__(self, file_like=None, Py_ssize_t read_size=0, bint use_list=0,
244245
object object_hook=None, object list_hook=None):
@@ -252,6 +253,8 @@ cdef class Unpacker(object):
252253
raise ValueError("`file_like.read` must be a callable.")
253254
self.read_size = read_size
254255
self.buf = <char*>malloc(read_size)
256+
if self.buf == NULL:
257+
raise MemoryError("Unable to allocate internal buffer.")
255258
self.buf_size = read_size
256259
self.buf_head = 0
257260
self.buf_tail = 0
@@ -295,7 +298,9 @@ cdef class Unpacker(object):
295298
new_size = tail + _buf_len
296299
if new_size < buf_size*2:
297300
new_size = buf_size*2
298-
buf = <char*>realloc(buf, new_size)
301+
buf = <char*>realloc(buf, new_size)
302+
if buf == NULL:
303+
raise MemoryError("Unable to enlarge internal buffer.") # self.buf still holds old buffer and will be freed during obj destruction
299304
buf_size = new_size
300305

301306
memcpy(buf + tail, <char*>(_buf), _buf_len)

0 commit comments

Comments
 (0)
0