[2.7] bpo-29960 _random.Random corrupted on exception in setstate(). … (#1289)
(cherry picked from commit 9616a82e7802241a4b74cf7ae38d43c37bf66e48)
diff --git a/Modules/_randommodule.c b/Modules/_randommodule.c
index 4b9b0eb..852b810 100644
--- a/Modules/_randommodule.c
+++ b/Modules/_randommodule.c
@@ -341,6 +341,7 @@
int i;
unsigned long element;
long index;
+ unsigned long new_state[N];
if (!PyTuple_Check(state)) {
PyErr_SetString(PyExc_TypeError,
@@ -357,7 +358,7 @@
element = PyLong_AsUnsignedLong(PyTuple_GET_ITEM(state, i));
if (element == (unsigned long)-1 && PyErr_Occurred())
return NULL;
- self->state[i] = element & 0xffffffffUL; /* Make sure we get sane state */
+ new_state[i] = element & 0xffffffffUL; /* Make sure we get sane state */
}
index = PyLong_AsLong(PyTuple_GET_ITEM(state, i));
@@ -368,6 +369,8 @@
return NULL;
}
self->index = (int)index;
+ for (i = 0; i < N; i++)
+ self->state[i] = new_state[i];
Py_INCREF(Py_None);
return Py_None;