Skip to content

Commit

Permalink
Fix memory leak in Raw.copy().
Browse files Browse the repository at this point in the history
Raw_New borrows a reference to its argument, so Raw_Copy must dereference 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()
  • Loading branch information
tjgq committed Jul 12, 2024
1 parent 2c37da0 commit 0e4ab56
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion msgspec/_core.c
Original file line number Diff line number Diff line change
Expand Up @@ -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[] = {
Expand Down

0 comments on commit 0e4ab56

Please sign in to comment.