[3.13] gh-123243: Fix reference leak in `_decimal` (GH-123244) (#123280)

gh-123243: Fix reference leak in `_decimal` (GH-123244)
(cherry picked from commit 5ff638f1b5)

Co-authored-by: neonene <53406459+neonene@users.noreply.github.com>
This commit is contained in:
Miss Islington (bot) 2024-08-24 06:33:49 +02:00 committed by GitHub
parent 935b8b422b
commit a65fe07db4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 25 additions and 2 deletions

View File

@ -0,0 +1 @@
Fix memory leak in :mod:`!_decimal`.

View File

@ -1390,6 +1390,10 @@ context_new(PyTypeObject *type, PyObject *args UNUSED, PyObject *kwds UNUSED)
CtxCaps(self) = 1; CtxCaps(self) = 1;
self->tstate = NULL; self->tstate = NULL;
if (type == state->PyDecContext_Type) {
PyObject_GC_Track(self);
}
assert(PyObject_GC_IsTracked((PyObject *)self));
return (PyObject *)self; return (PyObject *)self;
} }
@ -2038,6 +2042,10 @@ PyDecType_New(PyTypeObject *type)
MPD(dec)->alloc = _Py_DEC_MINALLOC; MPD(dec)->alloc = _Py_DEC_MINALLOC;
MPD(dec)->data = dec->data; MPD(dec)->data = dec->data;
if (type == state->PyDec_Type) {
PyObject_GC_Track(dec);
}
assert(PyObject_GC_IsTracked((PyObject *)dec));
return (PyObject *)dec; return (PyObject *)dec;
} }
#define dec_alloc(st) PyDecType_New((st)->PyDec_Type) #define dec_alloc(st) PyDecType_New((st)->PyDec_Type)
@ -6143,8 +6151,22 @@ decimal_clear(PyObject *module)
Py_CLEAR(state->SignalTuple); Py_CLEAR(state->SignalTuple);
Py_CLEAR(state->PyDecimal); Py_CLEAR(state->PyDecimal);
PyMem_Free(state->signal_map); if (state->signal_map != NULL) {
PyMem_Free(state->cond_map); 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; return 0;
} }