]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.12] gh-128961: Fix exhausted array iterator crash in __setstate__() (GH-128962...
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Sat, 18 Jan 2025 10:14:07 +0000 (11:14 +0100)
committerGitHub <noreply@github.com>
Sat, 18 Jan 2025 10:14:07 +0000 (10:14 +0000)
(cherry picked from commit 4dade055f4e18a7e91bc70293abb4db745ad16ca)

Co-authored-by: Tomasz Pytel <tompytel@gmail.com>
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 9c21cf035b197538e1b6fd4ccc6707d6c495354e..9b4847bf11abc7c147b84c4acc5a43f069157bc1 100755 (executable)
@@ -1609,5 +1609,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 d24c5989af36fc8a5fc24771685e13fb851f4ee0..090a7b841c93ea04250bc8d67611535903f92cd8 100644 (file)
@@ -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;
 }