summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2015-07-24 06:02:53 (GMT)
committerSerhiy Storchaka <storchaka@gmail.com>2015-07-24 06:02:53 (GMT)
commit3b77d01dbc9e7cf8e32803a307f2ecf7b2bf0f05 (patch)
treec068f5321f638951218d1d980c0e1d851fb245b1
parentaffac0062dfdca716f4dc9228476b06a516c032b (diff)
downloadcpython-3b77d01dbc9e7cf8e32803a307f2ecf7b2bf0f05.zip
cpython-3b77d01dbc9e7cf8e32803a307f2ecf7b2bf0f05.tar.gz
cpython-3b77d01dbc9e7cf8e32803a307f2ecf7b2bf0f05.tar.bz2
Issue #24620: Random.setstate() now validates the value of state last element.
-rw-r--r--Lib/test/test_random.py5
-rw-r--r--Misc/NEWS2
-rw-r--r--Modules/_randommodule.c4
3 files changed, 11 insertions, 0 deletions
diff --git a/Lib/test/test_random.py b/Lib/test/test_random.py
index 250f443..e4876fd 100644
--- a/Lib/test/test_random.py
+++ b/Lib/test/test_random.py
@@ -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
diff --git a/Misc/NEWS b/Misc/NEWS
index 95d6e11..9027ec8 100644
--- 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
diff --git a/Modules/_randommodule.c b/Modules/_randommodule.c
index 8bb9e37..480df92 100644
--- a/Modules/_randommodule.c
+++ b/Modules/_randommodule.c
@@ -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);