From 406360b3a8437f487dfd4732585f276c275649df Mon Sep 17 00:00:00 2001 From: Thomas Kowalski Date: Wed, 29 Apr 2026 11:23:42 +0200 Subject: [PATCH] fix: eliminate memory leak when parsing incorrect nested arrays --- msgpack/_unpacker.pyx | 1 + msgpack/unpack_template.h | 8 ++++++++ 2 files changed, 9 insertions(+) diff --git a/msgpack/_unpacker.pyx b/msgpack/_unpacker.pyx index e25986e..29cdec4 100644 --- a/msgpack/_unpacker.pyx +++ b/msgpack/_unpacker.pyx @@ -322,6 +322,7 @@ cdef class Unpacker: self.buf = NULL def __dealloc__(self): + unpack_clear(&self.ctx) PyMem_Free(self.buf) self.buf = NULL diff --git a/msgpack/unpack_template.h b/msgpack/unpack_template.h index cce29e7..4230661 100644 --- a/msgpack/unpack_template.h +++ b/msgpack/unpack_template.h @@ -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); }