Skip to content

Commit f1966ef

Browse files
miss-islingtonjelle-openaiJelleZijlstra
authored
[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 14cd72b) Co-authored-by: Jelle Zijlstra <jelle@openai.com> Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
1 parent f14744f commit f1966ef

2 files changed

Lines changed: 6 additions & 0 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix a reference leak when :c:func:`_PyTuple_Resize` fails.

‎Objects/tupleobject.c‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1006,6 +1006,11 @@ _PyTuple_Resize(PyObject **pv, Py_ssize_t newsize)
10061006
#ifdef Py_REF_DEBUG
10071007
_Py_DecRefTotal(_PyThreadState_GET());
10081008
#endif
1009+
/* The failed reallocation leaves the original items owned by v. */
1010+
i = oldsize;
1011+
while (--i >= 0) {
1012+
Py_XDECREF(v->ob_item[i]);
1013+
}
10091014
PyObject_GC_Del(v);
10101015
return -1;
10111016
}

0 commit comments

Comments
 (0)