Kaydet (Commit) 3b77d01d authored tarafından Serhiy Storchaka's avatar Serhiy Storchaka

Issue #24620: Random.setstate() now validates the value of state last element.

üst affac006
...@@ -319,6 +319,11 @@ class MersenneTwister_TestBasicOps(TestBasicOps): ...@@ -319,6 +319,11 @@ class MersenneTwister_TestBasicOps(TestBasicOps):
self.assertRaises(TypeError, self.gen.setstate, (2, ('a',)*625, None)) self.assertRaises(TypeError, self.gen.setstate, (2, ('a',)*625, None))
# Last element s/b an int also # Last element s/b an int also
self.assertRaises(TypeError, self.gen.setstate, (2, (0,)*624+('a',), None)) 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): def test_referenceImplementation(self):
# Compare the python implementation with results from the original # Compare the python implementation with results from the original
......
...@@ -34,6 +34,8 @@ Core and Builtins ...@@ -34,6 +34,8 @@ Core and Builtins
Library 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 #13938: 2to3 converts StringTypes to a tuple. Patch from Mark Hammond.
- Issue #24611: Fixed compiling the posix module on non-Windows platforms - Issue #24611: Fixed compiling the posix module on non-Windows platforms
......
...@@ -363,6 +363,10 @@ random_setstate(RandomObject *self, PyObject *state) ...@@ -363,6 +363,10 @@ random_setstate(RandomObject *self, PyObject *state)
index = PyLong_AsLong(PyTuple_GET_ITEM(state, i)); index = PyLong_AsLong(PyTuple_GET_ITEM(state, i));
if (index == -1 && PyErr_Occurred()) if (index == -1 && PyErr_Occurred())
return NULL; return NULL;
if (index < 0 || index > N) {
PyErr_SetString(PyExc_ValueError, "invalid state");
return NULL;
}
self->index = (int)index; self->index = (int)index;
Py_INCREF(Py_None); Py_INCREF(Py_None);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment