Skip to content

Commit a67ac2f

Browse files
eduardo-elizondomiss-islington
authored andcommitted
bpo-38150 Fix refleak in the finalizer of a _testcapimodule type (GH-16115)
The PyLong created in the finalizer was not being cleaned up https://bugs.python.org/issue38150 Automerge-Triggered-By: @matrixise
1 parent 14fd925 commit a67ac2f

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

Modules/_testcapimodule.c

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6304,7 +6304,7 @@ static void
63046304
heapctypesubclasswithfinalizer_finalize(PyObject *self)
63056305
{
63066306
PyObject *error_type, *error_value, *error_traceback, *m;
6307-
PyObject *oldtype = NULL, *newtype = NULL;
6307+
PyObject *oldtype = NULL, *newtype = NULL, *refcnt = NULL;
63086308

63096309
/* Save the current exception, if any. */
63106310
PyErr_Fetch(&error_type, &error_value, &error_traceback);
@@ -6322,18 +6322,26 @@ heapctypesubclasswithfinalizer_finalize(PyObject *self)
63226322
if (PyObject_SetAttrString(self, "__class__", newtype) < 0) {
63236323
goto cleanup_finalize;
63246324
}
6325-
if (PyObject_SetAttrString(
6326-
oldtype, "refcnt_in_del", PyLong_FromSsize_t(Py_REFCNT(oldtype))) < 0) {
6325+
refcnt = PyLong_FromSsize_t(Py_REFCNT(oldtype));
6326+
if (refcnt == NULL) {
63276327
goto cleanup_finalize;
63286328
}
6329-
if (PyObject_SetAttrString(
6330-
newtype, "refcnt_in_del", PyLong_FromSsize_t(Py_REFCNT(newtype))) < 0) {
6329+
if (PyObject_SetAttrString(oldtype, "refcnt_in_del", refcnt) < 0) {
6330+
goto cleanup_finalize;
6331+
}
6332+
Py_DECREF(refcnt);
6333+
refcnt = PyLong_FromSsize_t(Py_REFCNT(newtype));
6334+
if (refcnt == NULL) {
6335+
goto cleanup_finalize;
6336+
}
6337+
if (PyObject_SetAttrString(newtype, "refcnt_in_del", refcnt) < 0) {
63316338
goto cleanup_finalize;
63326339
}
63336340

63346341
cleanup_finalize:
63356342
Py_XDECREF(oldtype);
63366343
Py_XDECREF(newtype);
6344+
Py_XDECREF(refcnt);
63376345

63386346
/* Restore the saved exception. */
63396347
PyErr_Restore(error_type, error_value, error_traceback);

0 commit comments

Comments
 (0)