fix: eliminate memory leak when parsing incorrect nested arrays

This commit is contained in:
Thomas Kowalski 2026-04-29 11:23:42 +02:00
parent 0d600a3328
commit 406360b3a8
No known key found for this signature in database
2 changed files with 9 additions and 0 deletions

View file

@ -322,6 +322,7 @@ cdef class Unpacker:
self.buf = NULL
def __dealloc__(self):
unpack_clear(&self.ctx)
PyMem_Free(self.buf)
self.buf = NULL

View file

@ -72,6 +72,14 @@ static inline PyObject* unpack_data(unpack_context* ctx)
static inline void unpack_clear(unpack_context *ctx)
{
unsigned int i;
for (i = 1; i < ctx->top; i++) {
Py_CLEAR(ctx->stack[i].obj);
/* map_key holds a live reference only while waiting for the value */
if (ctx->stack[i].ct == CT_MAP_VALUE) {
Py_CLEAR(ctx->stack[i].map_key);
}
}
Py_CLEAR(ctx->stack[0].obj);
}