fix: avoid memory leak when decoding invalid nested arrays (#671)

This commit is contained in:
Thomas Kowalski 2026-05-27 11:04:24 +02:00 committed by GitHub
parent 378edc60f1
commit 4a07745675
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 44 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);
}