]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.13] GH-141312: Allow only integers to longrangeiter_setstate state (GH-141317...
authorSergey Miryanov <sergey.miryanov@gmail.com>
Sat, 15 Nov 2025 19:14:17 +0000 (00:14 +0500)
committerGitHub <noreply@github.com>
Sat, 15 Nov 2025 19:14:17 +0000 (21:14 +0200)
This fixes an assertion error when the new computed start is not an integer.
(cherry picked from commit 10bec7c1eb3ee27f490a067426eef452b15f78f9)

Lib/test/test_range.py
Misc/NEWS.d/next/Core and Builtins/2025-11-10-23-07-06.gh-issue-141312.H-58GB.rst [new file with mode: 0644]
Objects/rangeobject.c

index 3870b153688b25fcd324a7779339e54f41a04ea4..2c9c290e8906b738b140421cf2ecfa1908389db9 100644 (file)
@@ -470,6 +470,16 @@ class RangeTest(unittest.TestCase):
         it.__setstate__(2**64 - 7)
         self.assertEqual(list(it), [12, 10])
 
+    def test_iterator_invalid_setstate(self):
+        for invalid_value in (1.0, ""):
+            ranges = (('rangeiter', range(10, 100, 2)),
+                      ('longrangeiter', range(10, 2**65, 2)))
+            for rng_name, rng in ranges:
+                with self.subTest(invalid_value=invalid_value, range=rng_name):
+                    it = iter(rng)
+                    with self.assertRaises(TypeError):
+                        it.__setstate__(invalid_value)
+
     def test_odd_bug(self):
         # This used to raise a "SystemError: NULL result without error"
         # because the range validation step was eating the exception
diff --git a/Misc/NEWS.d/next/Core and Builtins/2025-11-10-23-07-06.gh-issue-141312.H-58GB.rst b/Misc/NEWS.d/next/Core and Builtins/2025-11-10-23-07-06.gh-issue-141312.H-58GB.rst
new file mode 100644 (file)
index 0000000..fdb136c
--- /dev/null
@@ -0,0 +1,2 @@
+Fix the assertion failure in the ``__setstate__`` method of the range iterator
+when a non-integer argument is passed. Patch by Sergey Miryanov.
index 7da6162744ffd6fc360debd5b3c99a019688e4ba..57e1e00832057295a6bde648184c78fac889fb9d 100644 (file)
@@ -1014,6 +1014,11 @@ longrangeiter_reduce(longrangeiterobject *r, PyObject *Py_UNUSED(ignored))
 static PyObject *
 longrangeiter_setstate(longrangeiterobject *r, PyObject *state)
 {
+    if (!PyLong_CheckExact(state)) {
+        PyErr_Format(PyExc_TypeError, "state must be an int, not %T", state);
+        return NULL;
+    }
+
     PyObject *zero = _PyLong_GetZero();  // borrowed reference
     int cmp;