]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Issue #25021: Correctly make sure that product.__setstate__ does not access
authorKristján Valur Jónsson <sweskman@gmail.com>
Sat, 12 Sep 2015 15:20:54 +0000 (15:20 +0000)
committerKristján Valur Jónsson <sweskman@gmail.com>
Sat, 12 Sep 2015 15:20:54 +0000 (15:20 +0000)
invalid memory.

Lib/test/test_itertools.py
Modules/itertoolsmodule.c

index bb3b61fdce2d21eeb2e82d7566a2845fd30080ef..35391c96b4e1452960f3817c1441c0c55a768e4b 100644 (file)
@@ -959,6 +959,16 @@ class TestBasicOps(unittest.TestCase):
             self.assertEqual(list(copy.deepcopy(product(*args))), result)
             self.pickletest(product(*args))
 
+    def test_product_issue_25021(self):
+        # test that indices are properly clamped to the length of the tuples
+        p = product((1, 2),(3,))
+        p.__setstate__((0, 0x1000))  # will access tuple element 1 if not clamped
+        self.assertEqual(next(p), (2, 3))
+        # test that empty tuple in the list will result in an immediate StopIteration
+        p = product((1, 2), (), (3,))
+        p.__setstate__((0, 0, 0x1000))  # will access tuple element 1 if not clamped
+        self.assertRaises(StopIteration, next, p)
+
     def test_repeat(self):
         self.assertEqual(list(repeat(object='a', times=3)), ['a', 'a', 'a'])
         self.assertEqual(lzip(range(3),repeat('a')),
index a9e5709b41b9db599d190ab05fc444104d0eb102..57a398f88d8eb3ea673d07c7cb8cfaedf4a3e561 100644 (file)
@@ -2205,13 +2205,21 @@ product_setstate(productobject *lz, PyObject *state)
     {
         PyObject* indexObject = PyTuple_GET_ITEM(state, i);
         Py_ssize_t index = PyLong_AsSsize_t(indexObject);
+        PyObject* pool;
+        Py_ssize_t poolsize;
         if (index < 0 && PyErr_Occurred())
             return NULL; /* not an integer */
+        pool = PyTuple_GET_ITEM(lz->pools, i);
+        poolsize = PyTuple_GET_SIZE(pool);
+        if (poolsize == 0) {
+            lz->stopped = 1;
+            Py_RETURN_NONE;
+        }
         /* clamp the index */
         if (index < 0)
             index = 0;
-        else if (index > n-1)
-            index = n-1;
+        else if (index > poolsize-1)
+            index = poolsize-1;
         lz->indices[i] = index;
     }