Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Lib/test/test_capi/test_tuple.py
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,10 @@ def test__tuple_resize(self):
self.assertRaises(MemoryError, resize, a, PY_SSIZE_T_MAX)
self.assertRaises(SystemError, resize, a, -1)
self.assertRaises(SystemError, resize, a, PY_SSIZE_T_MIN)
a = ([1], [2], [3])
self.assertRaises(MemoryError, resize, a, PY_SSIZE_T_MAX)
self.assertRaises(SystemError, resize, a, -1)
self.assertRaises(SystemError, resize, a, PY_SSIZE_T_MIN)
# refcount > 1
a = (1, 2, 3)
self.assertRaises(SystemError, resize, a, 3, False)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fix a crash in :c:func:`_PyTuple_Resize` when resizing a non-empty tuple to a
negative or too large size. It now raises :exc:`SystemError` or
:exc:`MemoryError` respectively.
10 changes: 9 additions & 1 deletion Objects/tupleobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -1041,7 +1041,7 @@ _PyTuple_Resize(PyObject **pv, Py_ssize_t newsize)
Py_ssize_t oldsize;

v = (PyTupleObject *) *pv;
if (v == NULL || !Py_IS_TYPE(v, &PyTuple_Type) ||
if (v == NULL || !Py_IS_TYPE(v, &PyTuple_Type) || newsize < 0 ||
(Py_SIZE(v) != 0 && !_PyObject_IsUniquelyReferenced(*pv))) {
*pv = 0;
Py_XDECREF(v);
Expand All @@ -1068,6 +1068,14 @@ _PyTuple_Resize(PyObject **pv, Py_ssize_t newsize)
*pv = PyTuple_New(newsize);
return *pv == NULL ? -1 : 0;
}
/* Check for overflow */
if ((size_t)newsize > ((size_t)PY_SSIZE_T_MAX - (sizeof(PyTupleObject) -
sizeof(PyObject *))) / sizeof(PyObject *)) {
*pv = NULL;
Py_DECREF(v);
PyErr_NoMemory();
return -1;
}

if (_PyObject_GC_IS_TRACKED(v)) {
_PyObject_GC_UNTRACK(v);
Expand Down
Loading