changeset: 97038:84070c1225c5 branch: 2.7 parent: 97036:4fc7b803ac00 user: Serhiy Storchaka date: Fri Jul 24 09:02:53 2015 +0300 files: Lib/test/test_random.py Misc/NEWS Modules/_randommodule.c description: Issue #24620: Random.setstate() now validates the value of state last element. diff -r 4fc7b803ac00 -r 84070c1225c5 Lib/test/test_random.py --- a/Lib/test/test_random.py Fri Jul 24 08:05:45 2015 +0300 +++ b/Lib/test/test_random.py Fri Jul 24 09:02:53 2015 +0300 @@ -319,6 +319,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)) def test_referenceImplementation(self): # Compare the python implementation with results from the original diff -r 4fc7b803ac00 -r 84070c1225c5 Misc/NEWS --- a/Misc/NEWS Fri Jul 24 08:05:45 2015 +0300 +++ b/Misc/NEWS Fri Jul 24 09:02:53 2015 +0300 @@ -34,6 +34,8 @@ 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 -r 4fc7b803ac00 -r 84070c1225c5 Modules/_randommodule.c --- a/Modules/_randommodule.c Fri Jul 24 08:05:45 2015 +0300 +++ b/Modules/_randommodule.c Fri Jul 24 09:02:53 2015 +0300 @@ -363,6 +363,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);