Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b93d863

Browse files
committed
Issue #1621: Avoid signed overflow in list and tuple operations
Patch by Xiang Zhang.
1 parent 32d2ce3 commit b93d863

4 files changed

Lines changed: 28 additions & 11 deletions

File tree

Lib/test/list_tests.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,9 +266,21 @@ def test_extend(self):
266266
self.assertEqual(a, list("spameggs"))
267267

268268
self.assertRaises(TypeError, a.extend, None)
269-
270269
self.assertRaises(TypeError, a.extend)
271270

271+
# overflow test. issue1621
272+
class CustomIter:
273+
def __iter__(self):
274+
return self
275+
def __next__(self):
276+
raise StopIteration
277+
def __length_hint__(self):
278+
return sys.maxsize
279+
a = self.type2test([1,2,3,4])
280+
a.extend(CustomIter())
281+
self.assertEqual(a, [1,2,3,4])
282+
283+
272284
def test_insert(self):
273285
a = self.type2test([0, 1, 2])
274286
a.insert(0, -2)

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ Core and Builtins
1616
- Issue #27581: Don't rely on wrapping for overflow check in
1717
PySequence_Tuple(). Patch by Xiang Zhang.
1818

19+
- Issue #1621: Avoid signed integer overflow in list and tuple operations.
20+
Patch by Xiang Zhang.
21+
1922
- Issue #27419: Standard __import__() no longer look up "__import__" in globals
2023
or builtins for importing submodules or "from import". Fixed a crash if
2124
raise a warning about unabling to resolve package from __spec__ or

Objects/listobject.c

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -488,9 +488,9 @@ list_concat(PyListObject *a, PyObject *bb)
488488
return NULL;
489489
}
490490
#define b ((PyListObject *)bb)
491-
size = Py_SIZE(a) + Py_SIZE(b);
492-
if (size < 0)
491+
if (Py_SIZE(a) > PY_SSIZE_T_MAX - Py_SIZE(b))
493492
return PyErr_NoMemory();
493+
size = Py_SIZE(a) + Py_SIZE(b);
494494
np = (PyListObject *) PyList_New(size);
495495
if (np == NULL) {
496496
return NULL;
@@ -841,18 +841,20 @@ listextend(PyListObject *self, PyObject *b)
841841
return NULL;
842842
}
843843
m = Py_SIZE(self);
844-
mn = m + n;
845-
if (mn >= m) {
844+
if (m > PY_SSIZE_T_MAX - n) {
845+
/* m + n overflowed; on the chance that n lied, and there really
846+
* is enough room, ignore it. If n was telling the truth, we'll
847+
* eventually run out of memory during the loop.
848+
*/
849+
}
850+
else {
851+
mn = m + n;
846852
/* Make room. */
847853
if (list_resize(self, mn) < 0)
848854
goto error;
849855
/* Make the list sane again. */
850856
Py_SIZE(self) = m;
851857
}
852-
/* Else m + n overflowed; on the chance that n lied, and there really
853-
* is enough room, ignore it. If n was telling the truth, we'll
854-
* eventually run out of memory during the loop.
855-
*/
856858

857859
/* Run iterator to exhaustion. */
858860
for (;;) {

Objects/tupleobject.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -453,9 +453,9 @@ tupleconcat(PyTupleObject *a, PyObject *bb)
453453
return NULL;
454454
}
455455
#define b ((PyTupleObject *)bb)
456-
size = Py_SIZE(a) + Py_SIZE(b);
457-
if (size < 0)
456+
if (Py_SIZE(a) > PY_SSIZE_T_MAX - Py_SIZE(b))
458457
return PyErr_NoMemory();
458+
size = Py_SIZE(a) + Py_SIZE(b);
459459
np = (PyTupleObject *) PyTuple_New(size);
460460
if (np == NULL) {
461461
return NULL;

0 commit comments

Comments
 (0)