Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-123243: Fix reference leak in _decimal #123244

Merged
merged 8 commits into from
Aug 24, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix memory leak in :mod:`!_decimal`.
26 changes: 24 additions & 2 deletions Modules/_decimal/_decimal.c
Original file line number Diff line number Diff line change
Expand Up @@ -1357,6 +1357,7 @@ context_new(PyTypeObject *type, PyObject *args UNUSED, PyObject *kwds UNUSED)
}
else {
self = (PyDecContextObject *)type->tp_alloc(type, 0);
assert(PyObject_GC_IsTracked((PyObject *)self));
kumaraditya303 marked this conversation as resolved.
Show resolved Hide resolved
}

if (self == NULL) {
Expand Down Expand Up @@ -1390,6 +1391,9 @@ context_new(PyTypeObject *type, PyObject *args UNUSED, PyObject *kwds UNUSED)
CtxCaps(self) = 1;
self->tstate = NULL;

if (type == state->PyDecContext_Type) {
PyObject_GC_Track(self);
}
return (PyObject *)self;
}

Expand Down Expand Up @@ -2024,6 +2028,7 @@ PyDecType_New(PyTypeObject *type)
}
else {
dec = (PyDecObject *)type->tp_alloc(type, 0);
assert(PyObject_GC_IsTracked((PyObject *)dec));
}
if (dec == NULL) {
return NULL;
Expand All @@ -2038,6 +2043,9 @@ PyDecType_New(PyTypeObject *type)
MPD(dec)->alloc = _Py_DEC_MINALLOC;
MPD(dec)->data = dec->data;

if (type == state->PyDec_Type) {
PyObject_GC_Track(dec);
}
return (PyObject *)dec;
}
#define dec_alloc(st) PyDecType_New((st)->PyDec_Type)
Expand Down Expand Up @@ -6143,8 +6151,22 @@ decimal_clear(PyObject *module)
Py_CLEAR(state->SignalTuple);
Py_CLEAR(state->PyDecimal);

PyMem_Free(state->signal_map);
PyMem_Free(state->cond_map);
if (state->signal_map != NULL) {
for (DecCondMap *cm = state->signal_map; cm->name != NULL; cm++) {
Py_DECREF(cm->ex);
}
PyMem_Free(state->signal_map);
state->signal_map = NULL;
}

if (state->cond_map != NULL) {
// cond_map[0].ex has borrowed a reference from signal_map[0].ex
for (DecCondMap *cm = state->cond_map + 1; cm->name != NULL; cm++) {
Py_DECREF(cm->ex);
}
PyMem_Free(state->cond_map);
state->cond_map = NULL;
}
return 0;
}

Expand Down
Loading