https://github.com/python/cpython/commit/f1966ef82774501513c9336e923ff20e0e7dca83
commit: f1966ef82774501513c9336e923ff20e0e7dca83
branch: 3.14
author: Miss Islington (bot) <[email protected]>
committer: gpshead <[email protected]>
date: 2026-10-19T15:53:22-07:00
summary:

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

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 0f7a37faa91a577..9ed8fc3e9643863 100644
--- a/Objects/tupleobject.c
+++ b/Objects/tupleobject.c
@@ -1006,6 +1006,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