summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2015-05-21 17:51:53 (GMT)
committerSerhiy Storchaka <storchaka@gmail.com>2015-05-21 17:51:53 (GMT)
commitb2f3c2357c5bcc871cb3c789cae223faf0b472d1 (patch)
tree5eab5d85dd0122c7b40aead43188031550cd85cf
parent041dd8eef105f981693f7084b270481816dd9a7a (diff)
parent4faf5c5655277cec99b2b11f7fe34e73d3ae28b9 (diff)
downloadcpython-b2f3c2357c5bcc871cb3c789cae223faf0b472d1.zip
cpython-b2f3c2357c5bcc871cb3c789cae223faf0b472d1.tar.gz
cpython-b2f3c2357c5bcc871cb3c789cae223faf0b472d1.tar.bz2
Issue #23985: Fixed integer overflow in iterator object. Patch by
Clement Rouault.
-rw-r--r--Lib/test/test_iter.py25
-rw-r--r--Misc/NEWS3
-rw-r--r--Objects/iterobject.c5
3 files changed, 33 insertions, 0 deletions
diff --git a/Lib/test/test_iter.py b/Lib/test/test_iter.py
index e06f239..abc408f 100644
--- a/Lib/test/test_iter.py
+++ b/Lib/test/test_iter.py
@@ -1,5 +1,6 @@
# Test iterators.
+import sys
import unittest
from test.support import run_unittest, TESTFN, unlink, cpython_only
import pickle
@@ -48,6 +49,10 @@ class SequenceClass:
else:
raise IndexError
+class UnlimitedSequenceClass:
+ def __getitem__(self, i):
+ return i
+
# Main test suite
class TestCase(unittest.TestCase):
@@ -919,6 +924,26 @@ class TestCase(unittest.TestCase):
lst.extend(gen())
self.assertEqual(len(lst), 760)
+ @cpython_only
+ def test_iter_overflow(self):
+ # Test for the issue 22939
+ it = iter(UnlimitedSequenceClass())
+ # Manually set `it_index` to PY_SSIZE_T_MAX-2 without a loop
+ it.__setstate__(sys.maxsize - 2)
+ self.assertEqual(next(it), sys.maxsize - 2)
+ self.assertEqual(next(it), sys.maxsize - 1)
+ with self.assertRaises(OverflowError):
+ next(it)
+ # Check that Overflow error is always raised
+ with self.assertRaises(OverflowError):
+ next(it)
+
+ def test_iter_neg_setstate(self):
+ it = iter(UnlimitedSequenceClass())
+ it.__setstate__(-42)
+ self.assertEqual(next(it), 0)
+ self.assertEqual(next(it), 1)
+
def test_main():
run_unittest(TestCase)
diff --git a/Misc/NEWS b/Misc/NEWS
index 0f35d3f..58530a1 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,9 @@ Release date: 2015-05-24
Core and Builtins
-----------------
+- Issue #23985: Fixed integer overflow in iterator object. Patch by
+ Clement Rouault.
+
- Issue #23985: Fix a possible buffer overrun when deleting a slice from
the front of a bytearray and then appending some other bytes data.
diff --git a/Objects/iterobject.c b/Objects/iterobject.c
index baa51da..2fb0c88 100644
--- a/Objects/iterobject.c
+++ b/Objects/iterobject.c
@@ -54,6 +54,11 @@ iter_iternext(PyObject *iterator)
seq = it->it_seq;
if (seq == NULL)
return NULL;
+ if (it->it_index == PY_SSIZE_T_MAX) {
+ PyErr_SetString(PyExc_OverflowError,
+ "iter index too large");
+ return NULL;
+ }
result = PySequence_GetItem(seq, it->it_index);
if (result != NULL) {