Skip to content

Commit 7831074

Browse files
committed
gh-128961: fix array iterator segfault on __setstate__
1 parent 7807b40 commit 7831074

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

Lib/test/test_array.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1665,5 +1665,11 @@ def test_tolist(self, size):
16651665
self.assertEqual(ls[:8], list(example[:8]))
16661666
self.assertEqual(ls[-8:], list(example[-8:]))
16671667

1668+
def test_gh_128961(self):
1669+
a = array.array('i')
1670+
it = iter(a)
1671+
list(it)
1672+
it.__setstate__(0)
1673+
16681674
if __name__ == "__main__":
16691675
unittest.main()

Modules/arraymodule.c

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3090,11 +3090,14 @@ array_arrayiterator___setstate__(arrayiterobject *self, PyObject *state)
30903090
Py_ssize_t index = PyLong_AsSsize_t(state);
30913091
if (index == -1 && PyErr_Occurred())
30923092
return NULL;
3093-
if (index < 0)
3094-
index = 0;
3095-
else if (index > Py_SIZE(self->ao))
3096-
index = Py_SIZE(self->ao); /* iterator exhausted */
3097-
self->index = index;
3093+
arrayobject *ao = self->ao;
3094+
// if (ao != NULL) {
3095+
if (index < 0)
3096+
index = 0;
3097+
else if (index > Py_SIZE(ao))
3098+
index = Py_SIZE(ao); /* iterator exhausted */
3099+
self->index = index;
3100+
// }
30983101
Py_RETURN_NONE;
30993102
}
31003103

0 commit comments

Comments
 (0)