From 88b4a886ccffef2f6cdbd20f8c983eb5ded1c813 Mon Sep 17 00:00:00 2001 From: "Dwayne C. Litzenberger" Date: Wed, 25 Apr 2012 16:08:02 -0400 Subject: _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 --- lib/Crypto/SelfTest/Util/test_number.py | 1 + src/_fastmath.c | 7 +++++-- 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); -- cgit v1.2.1