]> granicus.if.org Git - python/commitdiff
Issue #29061: secrets.randbelow() would hang with a negative input
authorRaymond Hettinger <python@rcn.com>
Fri, 30 Dec 2016 05:54:25 +0000 (22:54 -0700)
committerRaymond Hettinger <python@rcn.com>
Fri, 30 Dec 2016 05:54:25 +0000 (22:54 -0700)
Lib/secrets.py
Lib/test/test_secrets.py
Misc/ACKS
Misc/NEWS

index 27fa4503d75606aa025c870fad68b6d940f2ff95..130434229e96a96dfacf1fc8b90dc98ef80d9bec 100644 (file)
@@ -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
index 4c65cf00cd3e2952f29b75ce23673da667e74161..d31d07e01f98ba3c1d9c9d0b948a57dfbc21f8ed 100644 (file)
@@ -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):
index 5faed18582fb13eeff723a814c1d2063d554ec86..1c3f573d610f3cecb9a42552fda3b1000d1d9d1a 100644 (file)
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -369,6 +369,7 @@ Daniel Dittmar
 Josip Djolonga
 Walter Dörwald
 Jaromir Dolecek
+Brendan Donegan
 Ismail Donmez
 Robert Donohue
 Marcos Donolo
index 3436c0a52fbe5f69c939cd0f67ac704c2b52bbc2..f3c6e9e04e2644bf0934cb77ccba42a3892d41f1 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -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