Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d60cd42

Browse files
committed
Issue #14815: Bugfix: the PyLong fed into the seed generator must be unsigned.
1 parent 50c4000 commit d60cd42

1 file changed

Lines changed: 5 additions & 4 deletions

File tree

Modules/_randommodule.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -228,16 +228,17 @@ random_seed(RandomObject *self, PyObject *args)
228228
Py_INCREF(Py_None);
229229
return Py_None;
230230
}
231-
/* If the arg is an int or long, use its absolute value; else use
232-
* the absolute value of its hash code.
231+
/* This algorithm relies on the number being unsigned.
232+
* So: if the arg is a PyLong, use its absolute value.
233+
* Otherwise use its hash value, cast to unsigned.
233234
*/
234235
if (PyLong_Check(arg))
235236
n = PyNumber_Absolute(arg);
236237
else {
237-
Py_ssize_t hash = PyObject_Hash(arg);
238+
Py_hash_t hash = PyObject_Hash(arg);
238239
if (hash == -1)
239240
goto Done;
240-
n = PyLong_FromSsize_t(hash);
241+
n = PyLong_FromSize_t((size_t)hash);
241242
}
242243
if (n == NULL)
243244
goto Done;

0 commit comments

Comments
 (0)