Skip to content

Commit 036c3c4

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

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
@@ -1086,6 +1086,11 @@ _PyTuple_Resize(PyObject **pv, Py_ssize_t newsize)
10861086
#ifdef Py_REF_DEBUG
10871087
_Py_DecRefTotal(_PyThreadState_GET());
10881088
#endif
1089+
/* The failed reallocation leaves the original items owned by v. */
1090+
i = oldsize;
1091+
while (--i >= 0) {
1092+
Py_XDECREF(v->ob_item[i]);
1093+
}
10891094
PyObject_GC_Del(v);
10901095
return -1;
10911096
}

0 commit comments

Comments
 (0)