[Python-checkins] cpython (merge 3.5 -> default): Issue #24620: Random.setstate() now validates the value of state last element.

serhiy.storchaka python-checkins at python.org
Fri Jul 24 08:48:44 CEST 2015


https://hg.python.org/cpython/rev/f6e399ae670f
changeset:   97040:f6e399ae670f
parent:      97034:3bbd0cbfe836
parent:      97039:d8229c26dd92
user:        Serhiy Storchaka <storchaka at gmail.com>
date:        Fri Jul 24 09:07:12 2015 +0300
summary:
  Issue #24620: Random.setstate() now validates the value of state last element.

files:
  Lib/test/test_random.py |  5 +++++
  Misc/NEWS               |  2 ++
  Modules/_randommodule.c |  4 ++++
  3 files changed, 11 insertions(+), 0 deletions(-)


diff --git a/Lib/test/test_random.py b/Lib/test/test_random.py
--- a/Lib/test/test_random.py
+++ b/Lib/test/test_random.py
@@ -338,6 +338,11 @@
         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
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -57,6 +57,8 @@
 Library
 -------
 
+- Issue #24620: Random.setstate() now validates the value of state last element.
+
 - Issue #22485: Fixed an issue that caused `inspect.getsource` to return incorrect
   results on nested functions.
 
diff --git a/Modules/_randommodule.c b/Modules/_randommodule.c
--- a/Modules/_randommodule.c
+++ b/Modules/_randommodule.c
@@ -335,6 +335,10 @@
     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);

-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list