mirror of
https://github.com/python/cpython.git
synced 2025-11-11 14:44:57 +00:00
Issue #29061: secrets.randbelow() would hang with a negative input
This commit is contained in:
parent
9ea82ddad5
commit
e9ee207622
4 changed files with 7 additions and 0 deletions
|
|
@ -26,6 +26,8 @@ choice = _sysrand.choice
|
||||||
|
|
||||||
def randbelow(exclusive_upper_bound):
|
def randbelow(exclusive_upper_bound):
|
||||||
"""Return a random int in the range [0, n)."""
|
"""Return a random int in the range [0, n)."""
|
||||||
|
if exclusive_upper_bound <= 0:
|
||||||
|
raise ValueError("Upper bound must be positive.")
|
||||||
return _sysrand._randbelow(exclusive_upper_bound)
|
return _sysrand._randbelow(exclusive_upper_bound)
|
||||||
|
|
||||||
DEFAULT_ENTROPY = 32 # number of bytes to return by default
|
DEFAULT_ENTROPY = 32 # number of bytes to return by default
|
||||||
|
|
|
||||||
|
|
@ -70,6 +70,7 @@ class Random_Tests(unittest.TestCase):
|
||||||
for i in range(2, 10):
|
for i in range(2, 10):
|
||||||
self.assertIn(secrets.randbelow(i), range(i))
|
self.assertIn(secrets.randbelow(i), range(i))
|
||||||
self.assertRaises(ValueError, secrets.randbelow, 0)
|
self.assertRaises(ValueError, secrets.randbelow, 0)
|
||||||
|
self.assertRaises(ValueError, secrets.randbelow, -1)
|
||||||
|
|
||||||
|
|
||||||
class Token_Tests(unittest.TestCase):
|
class Token_Tests(unittest.TestCase):
|
||||||
|
|
|
||||||
|
|
@ -369,6 +369,7 @@ Daniel Dittmar
|
||||||
Josip Djolonga
|
Josip Djolonga
|
||||||
Walter Dörwald
|
Walter Dörwald
|
||||||
Jaromir Dolecek
|
Jaromir Dolecek
|
||||||
|
Brendan Donegan
|
||||||
Ismail Donmez
|
Ismail Donmez
|
||||||
Robert Donohue
|
Robert Donohue
|
||||||
Marcos Donolo
|
Marcos Donolo
|
||||||
|
|
|
||||||
|
|
@ -43,6 +43,9 @@ Library
|
||||||
- Issue #29085: Allow random.Random.seed() to use high quality OS randomness
|
- Issue #29085: Allow random.Random.seed() to use high quality OS randomness
|
||||||
rather than the pid and time.
|
rather than the pid and time.
|
||||||
|
|
||||||
|
- Issue #29061: Fixed bug in secrets.randbelow() which would hang when given
|
||||||
|
a negative input. Patch by Brendan Donegan.
|
||||||
|
|
||||||
- Issue #29079: Prevent infinite loop in pathlib.resolve() on Windows
|
- Issue #29079: Prevent infinite loop in pathlib.resolve() on Windows
|
||||||
|
|
||||||
- Issue #13051: Fixed recursion errors in large or resized
|
- Issue #13051: Fixed recursion errors in large or resized
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue