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
gh-128961: Fix exhausted array iterator crash in __setstate__() (GH-1…
…28962)

(cherry picked from commit 4dade05)

Co-authored-by: Tomasz Pytel <tompytel@gmail.com>
  • Loading branch information
tom-pytel authored and miss-islington committed Jan 18, 2025
commit bf597151a275ddad0508bc2f77dac80fd85559eb
8 changes: 8 additions & 0 deletions Lib/test/test_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -1609,5 +1609,13 @@ def test_tolist(self, size):
self.assertEqual(ls[:8], list(example[:8]))
self.assertEqual(ls[-8:], list(example[-8:]))

def test_gh_128961(self):
a = array.array('i')
it = iter(a)
list(it)
it.__setstate__(0)
self.assertRaises(StopIteration, next, it)


if __name__ == "__main__":
unittest.main()
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix a crash when setting state on an exhausted :class:`array.array` iterator.
15 changes: 10 additions & 5 deletions Modules/arraymodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -2966,11 +2966,16 @@ array_arrayiterator___setstate__(arrayiterobject *self, PyObject *state)
Py_ssize_t index = PyLong_AsSsize_t(state);
if (index == -1 && PyErr_Occurred())
return NULL;
if (index < 0)
index = 0;
else if (index > Py_SIZE(self->ao))
index = Py_SIZE(self->ao); /* iterator exhausted */
self->index = index;
arrayobject *ao = self->ao;
if (ao != NULL) {
if (index < 0) {
index = 0;
}
else if (index > Py_SIZE(ao)) {
index = Py_SIZE(ao); /* iterator exhausted */
}
self->index = index;
}
Py_RETURN_NONE;
}

Expand Down