diff options
author | Raymond Hettinger <python@rcn.com> | 2016-12-30 05:54:25 (GMT) |
---|---|---|
committer | Raymond Hettinger <python@rcn.com> | 2016-12-30 05:54:25 (GMT) |
commit | e9ee207622f0f78e31b8277ccb9eaa895ea3472d (patch) | |
tree | b1f0113b031a3dcc5343a68393337ecd94b58bd8 | |
parent | 9ea82ddad55d38ffad3cff5ce8afadaf0eaa59a3 (diff) | |
download | cpython-e9ee207622f0f78e31b8277ccb9eaa895ea3472d.zip cpython-e9ee207622f0f78e31b8277ccb9eaa895ea3472d.tar.gz cpython-e9ee207622f0f78e31b8277ccb9eaa895ea3472d.tar.bz2 |
Issue #29061: secrets.randbelow() would hang with a negative input
-rw-r--r-- | Lib/secrets.py | 2 | ||||
-rw-r--r-- | Lib/test/test_secrets.py | 1 | ||||
-rw-r--r-- | Misc/ACKS | 1 | ||||
-rw-r--r-- | Misc/NEWS | 3 |
4 files changed, 7 insertions, 0 deletions
diff --git a/Lib/secrets.py b/Lib/secrets.py index 27fa450..1304342 100644 --- a/Lib/secrets.py +++ b/Lib/secrets.py @@ -26,6 +26,8 @@ choice = _sysrand.choice def randbelow(exclusive_upper_bound): """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) DEFAULT_ENTROPY = 32 # number of bytes to return by default diff --git a/Lib/test/test_secrets.py b/Lib/test/test_secrets.py index 4c65cf0..d31d07e 100644 --- a/Lib/test/test_secrets.py +++ b/Lib/test/test_secrets.py @@ -70,6 +70,7 @@ class Random_Tests(unittest.TestCase): for i in range(2, 10): self.assertIn(secrets.randbelow(i), range(i)) self.assertRaises(ValueError, secrets.randbelow, 0) + self.assertRaises(ValueError, secrets.randbelow, -1) class Token_Tests(unittest.TestCase): @@ -369,6 +369,7 @@ Daniel Dittmar Josip Djolonga Walter Dörwald Jaromir Dolecek +Brendan Donegan Ismail Donmez Robert Donohue Marcos Donolo @@ -43,6 +43,9 @@ Library - Issue #29085: Allow random.Random.seed() to use high quality OS randomness 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 #13051: Fixed recursion errors in large or resized |