https://github.com/python/cpython/commit/036c3c455a2c4fb75bfe579bfd60dc90c3009eb8
commit: 036c3c455a2c4fb75bfe579bfd60dc90c3009eb8
branch: 3.15
author: Miss Islington (bot) <[email protected]>
committer: gpshead <[email protected]>
date: 2026-10-19T22:55:41Z
summary:

[3.15] gh-159739: Release tuple items if resizing fails (GH-159754) (#159787)

gh-159739: Release tuple items if resizing fails (GH-159754)

Release the references still held by the original tuple when
reallocation fails.

Backport to 3.15 and 3.14.
(cherry picked from commit 14cd72b0ca2c269b98e070e8509b1c5f8d8c1e75)

Co-authored-by: Jelle Zijlstra <[email protected]>
Co-authored-by: Jelle Zijlstra <[email protected]>

files:
A Misc/NEWS.d/next/C_API/2026-10-19-19-33-35.gh-issue-159739.dee9a0.rst
M Objects/tupleobject.c

diff --git 
a/Misc/NEWS.d/next/C_API/2026-10-19-19-33-35.gh-issue-159739.dee9a0.rst 
b/Misc/NEWS.d/next/C_API/2026-10-19-19-33-35.gh-issue-159739.dee9a0.rst
new file mode 100644
index 000000000000000..891cf551730af2b
--- /dev/null
+++ b/Misc/NEWS.d/next/C_API/2026-10-19-19-33-35.gh-issue-159739.dee9a0.rst
@@ -0,0 +1 @@
+Fix a reference leak when :c:func:`_PyTuple_Resize` fails.
diff --git a/Objects/tupleobject.c b/Objects/tupleobject.c
index 780a692f9825333..82f76a927a3358c 100644
--- a/Objects/tupleobject.c
+++ b/Objects/tupleobject.c
@@ -1086,6 +1086,11 @@ _PyTuple_Resize(PyObject **pv, Py_ssize_t newsize)
 #ifdef Py_REF_DEBUG
         _Py_DecRefTotal(_PyThreadState_GET());
 #endif
+        /* The failed reallocation leaves the original items owned by v. */
+        i = oldsize;
+        while (--i >= 0) {
+            Py_XDECREF(v->ob_item[i]);
+        }
         PyObject_GC_Del(v);
         return -1;
     }

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]

Reply via email to