https://github.com/python/cpython/commit/8fffbb098259ed413d75dbc2e6d3e540425bd9b9
commit: 8fffbb098259ed413d75dbc2e6d3e540425bd9b9
branch: 3.12
author: Miss Islington (bot) <[email protected]>
committer: gpshead <[email protected]>
date: 2024-12-02T19:47:42Z
summary:

[3.12] gh-113841: fix possible undefined division by 0 in _Py_c_pow() 
(GH-127211) (GH-127216) (GH-127530)

[3.13] gh-113841: fix possible undefined division by 0 in _Py_c_pow() 
(GH-127211) (GH-127216)

Note, that transformed expression is not an equivalent for original one 
(1/exp(-x) != exp(x) in general for floating-point numbers). Though, the 
difference seems to be ~1ULP for good libm implementations.

It's more interesting why division was used from beginning. Closest algorithm 
I've found (no error checks, of course;)) - it's Algorithm 190 from ACM: 
https://dl.acm.org/doi/10.1145/366663.366679. It uses subtraction in the 
exponent.

(cherry picked from commit f7bb658124aba74be4c13f498bf46cfded710ef9)

(cherry picked from commit f41d8d89e79d634895868656f50a0e16e339f9d6)

Co-authored-by: Sergey B Kirpichev <[email protected]>

files:
A 
Misc/NEWS.d/next/Core_and_Builtins/2024-11-24-07-01-28.gh-issue-113841.WFg-Bu.rst
M Lib/test/test_complex.py
M Objects/complexobject.c

diff --git a/Lib/test/test_complex.py b/Lib/test/test_complex.py
index 4272c2afcdc274..f12e1c8bd7094a 100644
--- a/Lib/test/test_complex.py
+++ b/Lib/test/test_complex.py
@@ -300,6 +300,11 @@ def test_pow(self):
                     except OverflowError:
                         pass
 
+        # gh-113841: possible undefined division by 0 in _Py_c_pow()
+        x, y = 9j, 33j**3
+        with self.assertRaises(OverflowError):
+            x**y
+
     def test_pow_with_small_integer_exponents(self):
         # Check that small integer exponents are handled identically
         # regardless of their type.
diff --git 
a/Misc/NEWS.d/next/Core_and_Builtins/2024-11-24-07-01-28.gh-issue-113841.WFg-Bu.rst
 
b/Misc/NEWS.d/next/Core_and_Builtins/2024-11-24-07-01-28.gh-issue-113841.WFg-Bu.rst
new file mode 100644
index 00000000000000..2b07fdfcc6b527
--- /dev/null
+++ 
b/Misc/NEWS.d/next/Core_and_Builtins/2024-11-24-07-01-28.gh-issue-113841.WFg-Bu.rst
@@ -0,0 +1,2 @@
+Fix possible undefined behavior division by zero in :class:`complex`'s
+:c:func:`_Py_c_pow`.
diff --git a/Objects/complexobject.c b/Objects/complexobject.c
index 7f7e7e6b08cdca..eff173802984b6 100644
--- a/Objects/complexobject.c
+++ b/Objects/complexobject.c
@@ -146,7 +146,7 @@ _Py_c_pow(Py_complex a, Py_complex b)
         at = atan2(a.imag, a.real);
         phase = at*b.real;
         if (b.imag != 0.0) {
-            len /= exp(at*b.imag);
+            len *= exp(-at*b.imag);
             phase += b.imag*log(vabs);
         }
         r.real = len*cos(phase);

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: [email protected]

Reply via email to