Commit 178f0b6d authored by Serhiy Storchaka's avatar Serhiy Storchaka

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

parent f79dfe3f
...@@ -338,6 +338,11 @@ class MersenneTwister_TestBasicOps(TestBasicOps, unittest.TestCase): ...@@ -338,6 +338,11 @@ class MersenneTwister_TestBasicOps(TestBasicOps, unittest.TestCase):
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))
# Little trick to make "tuple(x % (2**32) for x in internalstate)" # 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 # raise ValueError. I cannot think of a simple way to achieve this, so
......
...@@ -66,6 +66,8 @@ Core and Builtins ...@@ -66,6 +66,8 @@ Core and Builtins
Library Library
------- -------
- Issue #24620: Random.setstate() now validates the value of state last element.
- Issue #22153: Improve unittest docs. Patch from Martin Panter and evilzero. - Issue #22153: Improve unittest docs. Patch from Martin Panter and evilzero.
- Issue #24206: Fixed __eq__ and __ne__ methods of inspect classes. - Issue #24206: Fixed __eq__ and __ne__ methods of inspect classes.
......
...@@ -340,6 +340,10 @@ random_setstate(RandomObject *self, PyObject *state) ...@@ -340,6 +340,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