Skip to content

Commit 53ff2ca

Browse files
[3.8] bpo-38150: Fix refleak in the finalizer of a _testcapimodule type (GH-16115) (GH-16118)
The PyLong created in the finalizer was not being cleaned up https://bugs.python.org/issue38150 Automerge-Triggered-By: @matrixise (cherry picked from commit a67ac2f) Co-authored-by: Eddie Elizondo <[email protected]>
1 parent 436b429 commit 53ff2ca

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
@@ -6090,7 +6090,7 @@ static void
60906090
heapctypesubclasswithfinalizer_finalize(PyObject *self)
60916091
{
60926092
PyObject *error_type, *error_value, *error_traceback, *m;
6093-
PyObject *oldtype = NULL, *newtype = NULL;
6093+
PyObject *oldtype = NULL, *newtype = NULL, *refcnt = NULL;
60946094

60956095
/* Save the current exception, if any. */
60966096
PyErr_Fetch(&error_type, &error_value, &error_traceback);
@@ -6108,18 +6108,26 @@ heapctypesubclasswithfinalizer_finalize(PyObject *self)
61086108
if (PyObject_SetAttrString(self, "__class__", newtype) < 0) {
61096109
goto cleanup_finalize;
61106110
}
6111-
if (PyObject_SetAttrString(
6112-
oldtype, "refcnt_in_del", PyLong_FromSsize_t(Py_REFCNT(oldtype))) < 0) {
6111+
refcnt = PyLong_FromSsize_t(Py_REFCNT(oldtype));
6112+
if (refcnt == NULL) {
61136113
goto cleanup_finalize;
61146114
}
6115-
if (PyObject_SetAttrString(
6116-
newtype, "refcnt_in_del", PyLong_FromSsize_t(Py_REFCNT(newtype))) < 0) {
6115+
if (PyObject_SetAttrString(oldtype, "refcnt_in_del", refcnt) < 0) {
6116+
goto cleanup_finalize;
6117+
}
6118+
Py_DECREF(refcnt);
6119+
refcnt = PyLong_FromSsize_t(Py_REFCNT(newtype));
6120+
if (refcnt == NULL) {
6121+
goto cleanup_finalize;
6122+
}
6123+
if (PyObject_SetAttrString(newtype, "refcnt_in_del", refcnt) < 0) {
61176124
goto cleanup_finalize;
61186125
}
61196126

61206127
cleanup_finalize:
61216128
Py_XDECREF(oldtype);
61226129
Py_XDECREF(newtype);
6130+
Py_XDECREF(refcnt);
61236131

61246132
/* Restore the saved exception. */
61256133
PyErr_Restore(error_type, error_value, error_traceback);

0 commit comments

Comments
 (0)