]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Issue #24620: Random.setstate() now validates the value of state last element.
authorSerhiy Storchaka <storchaka@gmail.com>
Fri, 24 Jul 2015 06:02:53 +0000 (09:02 +0300)
committerSerhiy Storchaka <storchaka@gmail.com>
Fri, 24 Jul 2015 06:02:53 +0000 (09:02 +0300)
Lib/test/test_random.py
Misc/NEWS
Modules/_randommodule.c

index e64804556db85bf118d34666d5cd2ffb9a4d4128..4b5232fe21d4aa44069778c458e98750f5659af9 100644 (file)
@@ -338,6 +338,11 @@ class MersenneTwister_TestBasicOps(TestBasicOps, unittest.TestCase):
         self.assertRaises(TypeError, self.gen.setstate, (2, ('a',)*625, None))
         # Last element s/b an int also
         self.assertRaises(TypeError, self.gen.setstate, (2, (0,)*624+('a',), None))
+        # Last element s/b between 0 and 624
+        with self.assertRaises((ValueError, OverflowError)):
+            self.gen.setstate((2, (1,)*624+(625,), None))
+        with self.assertRaises((ValueError, OverflowError)):
+            self.gen.setstate((2, (1,)*624+(-1,), None))
 
         # Little trick to make "tuple(x % (2**32) for x in internalstate)"
         # raise ValueError. I cannot think of a simple way to achieve this, so
index d38c4146f5a742efa16e8801821185c7a9138aaa..1a67632e9188cad8bb68de0ea7fd50da0384a8c0 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -66,6 +66,8 @@ Core and Builtins
 Library
 -------
 
+- Issue #24620: Random.setstate() now validates the value of state last element.
+
 - Issue #22153: Improve unittest docs. Patch from Martin Panter and evilzero.
 
 - Issue #24206: Fixed __eq__ and __ne__ methods of inspect classes.
index 4377ee0cf4d90ce78b1970636236599e0e0be1c3..416e266f0bd52fe29d46e6d829e0891760c40e94 100644 (file)
@@ -340,6 +340,10 @@ random_setstate(RandomObject *self, PyObject *state)
     index = PyLong_AsLong(PyTuple_GET_ITEM(state, i));
     if (index == -1 && PyErr_Occurred())
         return NULL;
+    if (index < 0 || index > N) {
+        PyErr_SetString(PyExc_ValueError, "invalid state");
+        return NULL;
+    }
     self->index = (int)index;
 
     Py_INCREF(Py_None);