Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
gh-128961: fix array iterator segfault on __setstate__
  • Loading branch information
tom-pytel committed Jan 17, 2025
commit 7831074d519212f2d68d4fea7d355f4fdd1b9436
6 changes: 6 additions & 0 deletions Lib/test/test_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -1665,5 +1665,11 @@ 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)
Comment thread
erlend-aasland marked this conversation as resolved.

if __name__ == "__main__":
unittest.main()
13 changes: 8 additions & 5 deletions Modules/arraymodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -3090,11 +3090,14 @@ 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;
Comment thread
picnixz marked this conversation as resolved.
// if (ao != NULL) {
if (index < 0)
index = 0;
Comment thread
erlend-aasland marked this conversation as resolved.
Outdated
else if (index > Py_SIZE(ao))
index = Py_SIZE(ao); /* iterator exhausted */
Comment thread
erlend-aasland marked this conversation as resolved.
Outdated
self->index = index;
// }
Py_RETURN_NONE;
}

Expand Down