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

Skip to content

Commit e9ee207

Browse files
committed
Issue #29061: secrets.randbelow() would hang with a negative input
1 parent 9ea82dd commit e9ee207

4 files changed

Lines changed: 7 additions & 0 deletions

File tree

Lib/secrets.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626

2727
def randbelow(exclusive_upper_bound):
2828
"""Return a random int in the range [0, n)."""
29+
if exclusive_upper_bound <= 0:
30+
raise ValueError("Upper bound must be positive.")
2931
return _sysrand._randbelow(exclusive_upper_bound)
3032

3133
DEFAULT_ENTROPY = 32 # number of bytes to return by default

Lib/test/test_secrets.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ def test_randbelow(self):
7070
for i in range(2, 10):
7171
self.assertIn(secrets.randbelow(i), range(i))
7272
self.assertRaises(ValueError, secrets.randbelow, 0)
73+
self.assertRaises(ValueError, secrets.randbelow, -1)
7374

7475

7576
class Token_Tests(unittest.TestCase):

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,7 @@ Daniel Dittmar
369369
Josip Djolonga
370370
Walter Dörwald
371371
Jaromir Dolecek
372+
Brendan Donegan
372373
Ismail Donmez
373374
Robert Donohue
374375
Marcos Donolo

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ Library
4343
- Issue #29085: Allow random.Random.seed() to use high quality OS randomness
4444
rather than the pid and time.
4545

46+
- Issue #29061: Fixed bug in secrets.randbelow() which would hang when given
47+
a negative input. Patch by Brendan Donegan.
48+
4649
- Issue #29079: Prevent infinite loop in pathlib.resolve() on Windows
4750

4851
- Issue #13051: Fixed recursion errors in large or resized

0 commit comments

Comments
 (0)