summaryrefslogtreecommitdiff
path: root/msgpack/_msgpack.pyx
diff options
context:
space:
mode:
authorINADA Naoki <songofacandy@gmail.com>2011-01-10 05:07:07 +0900
committerINADA Naoki <songofacandy@gmail.com>2011-01-10 05:07:07 +0900
commit47b0c273bb70d34b253c9e908a3f2d55e99b8cef (patch)
tree6f77d94543bbb03bed061cfe74609501ac2ecf1d /msgpack/_msgpack.pyx
parent56aaed36416ebe8f7df670d21b04219e67e6e2c9 (diff)
downloadmsgpack-python-47b0c273bb70d34b253c9e908a3f2d55e99b8cef.tar.gz
python: Check if (m|re)alloc's return value is NULL. (Thanks to Mateusz)
Diffstat (limited to 'msgpack/_msgpack.pyx')
-rw-r--r--msgpack/_msgpack.pyx7
1 files changed, 6 insertions, 1 deletions
diff --git a/msgpack/_msgpack.pyx b/msgpack/_msgpack.pyx
index c7eec81..e48f8b2 100644
--- a/msgpack/_msgpack.pyx
+++ b/msgpack/_msgpack.pyx
@@ -239,6 +239,7 @@ cdef class Unpacker(object):
def __dealloc__(self):
free(self.buf);
+ self.buf = NULL;
def __init__(self, file_like=None, Py_ssize_t read_size=0, bint use_list=0,
object object_hook=None, object list_hook=None):
@@ -252,6 +253,8 @@ cdef class Unpacker(object):
raise ValueError("`file_like.read` must be a callable.")
self.read_size = read_size
self.buf = <char*>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
@@ -295,7 +298,9 @@ cdef class Unpacker(object):
new_size = tail + _buf_len
if new_size < buf_size*2:
new_size = buf_size*2
- buf = <char*>realloc(buf, new_size)
+ buf = <char*>realloc(buf, new_size)
+ if buf == NULL:
+ raise MemoryError("Unable to enlarge internal buffer.") # self.buf still holds old buffer and will be freed during obj destruction
buf_size = new_size
memcpy(buf + tail, <char*>(_buf), _buf_len)