Skip to content

Commit

Permalink
[3.13] gh-123243: Fix reference leak in _decimal (GH-123244) (#123280)
Browse files Browse the repository at this point in the history
gh-123243: Fix reference leak in `_decimal` (GH-123244)
(cherry picked from commit 5ff638f)

Co-authored-by: neonene <[email protected]>
  • Loading branch information
miss-islington and neonene authored Aug 24, 2024
1 parent 935b8b4 commit a65fe07
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
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 @@ -1390,6 +1390,10 @@ 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);
}
assert(PyObject_GC_IsTracked((PyObject *)self));
return (PyObject *)self;
}

Expand Down Expand Up @@ -2038,6 +2042,10 @@ PyDecType_New(PyTypeObject *type)
MPD(dec)->alloc = _Py_DEC_MINALLOC;
MPD(dec)->data = dec->data;

if (type == state->PyDec_Type) {
PyObject_GC_Track(dec);
}
assert(PyObject_GC_IsTracked((PyObject *)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

0 comments on commit a65fe07

Please sign in to comment.