Commit b2f3c235 authored by Serhiy Storchaka's avatar Serhiy Storchaka

Issue #23985: Fixed integer overflow in iterator object. Patch by

Clement Rouault.
parents 041dd8ee 4faf5c56
# Test iterators. # Test iterators.
import sys
import unittest import unittest
from test.support import run_unittest, TESTFN, unlink, cpython_only from test.support import run_unittest, TESTFN, unlink, cpython_only
import pickle import pickle
...@@ -48,6 +49,10 @@ class SequenceClass: ...@@ -48,6 +49,10 @@ class SequenceClass:
else: else:
raise IndexError raise IndexError
class UnlimitedSequenceClass:
def __getitem__(self, i):
return i
# Main test suite # Main test suite
class TestCase(unittest.TestCase): class TestCase(unittest.TestCase):
...@@ -919,6 +924,26 @@ class TestCase(unittest.TestCase): ...@@ -919,6 +924,26 @@ class TestCase(unittest.TestCase):
lst.extend(gen()) lst.extend(gen())
self.assertEqual(len(lst), 760) 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(): def test_main():
run_unittest(TestCase) run_unittest(TestCase)
......
...@@ -10,6 +10,9 @@ Release date: 2015-05-24 ...@@ -10,6 +10,9 @@ Release date: 2015-05-24
Core and Builtins 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 - Issue #23985: Fix a possible buffer overrun when deleting a slice from
the front of a bytearray and then appending some other bytes data. the front of a bytearray and then appending some other bytes data.
......
...@@ -54,6 +54,11 @@ iter_iternext(PyObject *iterator) ...@@ -54,6 +54,11 @@ iter_iternext(PyObject *iterator)
seq = it->it_seq; seq = it->it_seq;
if (seq == NULL) if (seq == NULL)
return 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); result = PySequence_GetItem(seq, it->it_index);
if (result != NULL) { if (result != NULL) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment