]> 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 250f443d1689fdce1623a052d719081fa56a59be..e4876fd0902e701d76cebc010817d2595a536a39 100644 (file)
@@ -319,6 +319,11 @@ class MersenneTwister_TestBasicOps(TestBasicOps):
         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))
 
     def test_referenceImplementation(self):
         # Compare the python implementation with results from the original
index 95d6e1186c78885572584b4f2a5563b00b027620..9027ec8785b73fdd4a09d4907d9c39681959d295 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -34,6 +34,8 @@ Core and Builtins
 Library
 -------
 
+- Issue #24620: Random.setstate() now validates the value of state last element.
+
 - Issue #13938: 2to3 converts StringTypes to a tuple. Patch from Mark Hammond.
 
 - Issue #24611: Fixed compiling the posix module on non-Windows platforms
index 8bb9e37d16bfe4e9425b6f71b501501571644f22..480df92eed289b4b6fdeb3769be6a16aa397010c 100644 (file)
@@ -363,6 +363,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);