]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.14] GH-141312: Allow only integers to longrangeiter_setstate state (GH-141317...
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Sat, 15 Nov 2025 19:30:25 +0000 (20:30 +0100)
committerGitHub <noreply@github.com>
Sat, 15 Nov 2025 19:30:25 +0000 (21:30 +0200)
This fixes an assertion error when the new computed start is not an integer.
(cherry picked from commit 10bec7c1eb3ee27f490a067426eef452b15f78f9)

Co-authored-by: Sergey Miryanov <sergey.miryanov@gmail.com>
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 f8cdfe68a6435e20973eabd98276e859de3d1277..e93346fb27703f6fe4ad11c488148a7882021b0e 100644 (file)
@@ -1042,6 +1042,11 @@ longrangeiter_reduce(PyObject *op, PyObject *Py_UNUSED(ignored))
 static PyObject *
 longrangeiter_setstate(PyObject *op, PyObject *state)
 {
+    if (!PyLong_CheckExact(state)) {
+        PyErr_Format(PyExc_TypeError, "state must be an int, not %T", state);
+        return NULL;
+    }
+
     longrangeiterobject *r = (longrangeiterobject*)op;
     PyObject *zero = _PyLong_GetZero();  // borrowed reference
     int cmp;