Skip to content

Commit 59fe6b8

Browse files
fix: eliminate memory leak when parsing incorrect nested arrays
1 parent 0d600a3 commit 59fe6b8

2 files changed

Lines changed: 9 additions & 0 deletions

File tree

msgpack/_unpacker.pyx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,7 @@ cdef class Unpacker:
322322
self.buf = NULL
323323

324324
def __dealloc__(self):
325+
unpack_clear(&self.ctx)
325326
PyMem_Free(self.buf)
326327
self.buf = NULL
327328

msgpack/unpack_template.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,14 @@ static inline PyObject* unpack_data(unpack_context* ctx)
7272

7373
static inline void unpack_clear(unpack_context *ctx)
7474
{
75+
unsigned int i;
76+
for (i = 0; i < ctx->top; i++) {
77+
Py_CLEAR(ctx->stack[i].obj);
78+
/* map_key holds a live reference only while waiting for the value */
79+
if (ctx->stack[i].ct == CT_MAP_VALUE) {
80+
Py_CLEAR(ctx->stack[i].map_key);
81+
}
82+
}
7583
Py_CLEAR(ctx->stack[0].obj);
7684
}
7785

0 commit comments

Comments
 (0)