From d2a8288ce3f6193e25dca8bea8ff56dea7519c3d Mon Sep 17 00:00:00 2001 From: Tiago Quelhas Date: Fri, 12 Jul 2024 21:06:04 +0200 Subject: [PATCH] Fix memory leak in Raw.copy(). Raw_New borrows a reference to its PyBytesObject argument, so Raw_Copy must still dispose of it. The following is a minimal repro for this issue: import msgspec DECODER = msgspec.msgpack.Decoder(type=msgspec.Raw) PAYLOAD = b"\xda\xff\xff" + (65535 * b"\0") # array of 65535 zeros for _ in range(1000000): raw = DECODER.decode(PAYLOAD) raw.copy() --- msgspec/_core.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/msgspec/_core.c b/msgspec/_core.c index 0f287814..20f32516 100644 --- a/msgspec/_core.c +++ b/msgspec/_core.c @@ -1513,7 +1513,9 @@ Raw_copy(Raw *self, PyObject *unused) } PyObject *buf = PyBytes_FromStringAndSize(self->buf, self->len); if (buf == NULL) return NULL; - return Raw_New(buf); + PyObject *out = Raw_New(buf); + Py_DECREF(buf); + return out; } static PyMethodDef Raw_methods[] = {