summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDwayne C. Litzenberger <dlitz@dlitz.net>2012-04-25 16:08:02 -0400
committerDwayne C. Litzenberger <dlitz@dlitz.net>2012-04-25 16:08:02 -0400
commit88b4a886ccffef2f6cdbd20f8c983eb5ded1c813 (patch)
tree10de5895572a5158f8da950b6323ffa2916aad6b
parent95d65366e9ac7e194bf8317d69785c9a5b877790 (diff)
downloadpycrypto-88b4a886ccffef2f6cdbd20f8c983eb5ded1c813.tar.gz
_fastmath: missing Py_BLOCK_THREADS on isPrime(1)
When _fastmath is present, the following code caused the Python interpreter to abort with a fatal error: from Crypto.Util.number import isPrime isPrime(1) # Fatal Python error: PyEval_SaveThread: NULL tstate Bug report: https://bugs.launchpad.net/pycrypto/+bug/988431
-rw-r--r--lib/Crypto/SelfTest/Util/test_number.py1
-rw-r--r--src/_fastmath.c7
2 files changed, 6 insertions, 2 deletions
diff --git a/lib/Crypto/SelfTest/Util/test_number.py b/lib/Crypto/SelfTest/Util/test_number.py
index 7a74e3a..f34233f 100644
--- a/lib/Crypto/SelfTest/Util/test_number.py
+++ b/lib/Crypto/SelfTest/Util/test_number.py
@@ -252,6 +252,7 @@ class MiscTests(unittest.TestCase):
def test_isPrime(self):
"""Util.number.isPrime"""
+ self.assertEqual(number.isPrime(1), False) # Regression test: isPrime(1) caused some versions of PyCrypto to crash.
self.assertEqual(number.isPrime(2), True)
self.assertEqual(number.isPrime(3), True)
self.assertEqual(number.isPrime(4), False)
diff --git a/src/_fastmath.c b/src/_fastmath.c
index 4b5dede..41734aa 100644
--- a/src/_fastmath.c
+++ b/src/_fastmath.c
@@ -1342,8 +1342,11 @@ rabinMillerTest (mpz_t n, int rounds, PyObject *randfunc)
}
Py_BEGIN_ALLOW_THREADS;
- if ((mpz_tstbit (n, 0) == 0) || (mpz_cmp_ui (n, 3) < 0))
- return (mpz_cmp_ui (n, 2) == 0);
+ if ((mpz_tstbit (n, 0) == 0) || (mpz_cmp_ui (n, 3) < 0)) {
+ return_val = (mpz_cmp_ui (n, 2) == 0);
+ Py_BLOCK_THREADS;
+ return return_val;
+ }
mpz_init (tmp);
mpz_init (n_1);