Skip to content

Commit 14cd72b

Browse files
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. Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
1 parent 8b2a373 commit 14cd72b

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
@@ -1066,6 +1066,11 @@ _PyTuple_Resize(PyObject **pv, Py_ssize_t newsize)
10661066
#ifdef Py_REF_DEBUG
10671067
_Py_DecRefTotal(_PyThreadState_GET());
10681068
#endif
1069+
/* The failed reallocation leaves the original items owned by v. */
1070+
i = oldsize;
1071+
while (--i >= 0) {
1072+
Py_XDECREF(v->ob_item[i]);
1073+
}
10691074
PyObject_GC_Del(v);
10701075
return -1;
10711076
}

0 commit comments

Comments
 (0)