]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-128961: Fix exhausted array iterator crash in __setstate__() (#128962)
authorTomasz Pytel <tompytel@gmail.com>
Sat, 18 Jan 2025 09:55:29 +0000 (04:55 -0500)
committerGitHub <noreply@github.com>
Sat, 18 Jan 2025 09:55:29 +0000 (10:55 +0100)
Lib/test/test_array.py
Misc/NEWS.d/next/Library/2025-01-17-21-33-11.gh-issue-128961.XwvyIZ.rst [new file with mode: 0644]
Modules/arraymodule.c

index f621f343eb062a94bc3cfe27388ef51d4ad9d5f4..58ea89c4fac833bbe0a9c61b1e9d837b434f6a7a 100755 (executable)
@@ -1665,5 +1665,13 @@ class LargeArrayTest(unittest.TestCase):
         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()
diff --git a/Misc/NEWS.d/next/Library/2025-01-17-21-33-11.gh-issue-128961.XwvyIZ.rst b/Misc/NEWS.d/next/Library/2025-01-17-21-33-11.gh-issue-128961.XwvyIZ.rst
new file mode 100644 (file)
index 0000000..9c985df
--- /dev/null
@@ -0,0 +1 @@
+Fix a crash when setting state on an exhausted :class:`array.array` iterator.
index b80c964f20d65e05081ba5a4ae4ae59b13418343..fdbb0f0e8d6691ed8520208145a6df69d76a2c43 100644 (file)
@@ -3090,11 +3090,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;
 }