Skip to content

Commit 1d582bb

Browse files
authored
bpo-44698: Fix undefined behaviour in complex exponentiation. (GH-27278)
1 parent 2b8ad9e commit 1d582bb

File tree

3 files changed

+37
-8
lines changed

3 files changed

+37
-8
lines changed

Diff for: Lib/test/test_complex.py

+21
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import unittest
2+
import sys
23
from test import support
34
from test.test_grammar import (VALID_UNDERSCORE_LITERALS,
45
INVALID_UNDERSCORE_LITERALS)
@@ -248,6 +249,26 @@ def test_pow(self):
248249
b = 5.1+2.3j
249250
self.assertRaises(ValueError, pow, a, b, 0)
250251

252+
# Check some boundary conditions; some of these used to invoke
253+
# undefined behaviour (https://bugs.python.org/issue44698). We're
254+
# not actually checking the results of these operations, just making
255+
# sure they don't crash (for example when using clang's
256+
# UndefinedBehaviourSanitizer).
257+
values = (sys.maxsize, sys.maxsize+1, sys.maxsize-1,
258+
-sys.maxsize, -sys.maxsize+1, -sys.maxsize+1)
259+
for real in values:
260+
for imag in values:
261+
with self.subTest(real=real, imag=imag):
262+
c = complex(real, imag)
263+
try:
264+
c ** real
265+
except OverflowError:
266+
pass
267+
try:
268+
c ** c
269+
except OverflowError:
270+
pass
271+
251272
def test_boolcontext(self):
252273
for i in range(100):
253274
self.assertTrue(complex(random() + 1e-6, random() + 1e-6))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix undefined behaviour in complex object exponentiation.

Diff for: Objects/complexobject.c

+15-8
Original file line numberDiff line numberDiff line change
@@ -514,8 +514,6 @@ static PyObject *
514514
complex_pow(PyObject *v, PyObject *w, PyObject *z)
515515
{
516516
Py_complex p;
517-
Py_complex exponent;
518-
long int_exponent;
519517
Py_complex a, b;
520518
TO_COMPLEX(v, a);
521519
TO_COMPLEX(w, b);
@@ -525,12 +523,21 @@ complex_pow(PyObject *v, PyObject *w, PyObject *z)
525523
return NULL;
526524
}
527525
errno = 0;
528-
exponent = b;
529-
int_exponent = (long)exponent.real;
530-
if (exponent.imag == 0. && exponent.real == int_exponent)
531-
p = c_powi(a, int_exponent);
532-
else
533-
p = _Py_c_pow(a, exponent);
526+
// Check if w is an integer value that fits inside a C long, so we can
527+
// use a faster algorithm. TO_COMPLEX(w, b), above, already handled the
528+
// conversion from larger longs, as well as other types.
529+
if (PyLong_Check(w)) {
530+
int overflow = 0;
531+
long int_exponent = PyLong_AsLongAndOverflow(w, &overflow);
532+
if (int_exponent == -1 && PyErr_Occurred())
533+
return NULL;
534+
if (overflow == 0)
535+
p = c_powi(a, int_exponent);
536+
else
537+
p = _Py_c_pow(a, b);
538+
} else {
539+
p = _Py_c_pow(a, b);
540+
}
534541

535542
Py_ADJUST_ERANGE2(p.real, p.imag);
536543
if (errno == EDOM) {

0 commit comments

Comments
 (0)