mirror of https://github.com/python/cpython.git
bpo-44698: Fix undefined behaviour in complex exponentiation. (GH-27278) (#27366)
(cherry picked from commit 1d582bbc96
)
Co-authored-by: T. Wouters <thomas@python.org>
This commit is contained in:
parent
8a37e8cf45
commit
256d97c8a3
|
@ -1,4 +1,5 @@
|
||||||
import unittest
|
import unittest
|
||||||
|
import sys
|
||||||
from test import support
|
from test import support
|
||||||
from test.test_grammar import (VALID_UNDERSCORE_LITERALS,
|
from test.test_grammar import (VALID_UNDERSCORE_LITERALS,
|
||||||
INVALID_UNDERSCORE_LITERALS)
|
INVALID_UNDERSCORE_LITERALS)
|
||||||
|
@ -248,6 +249,26 @@ def test_pow(self):
|
||||||
b = 5.1+2.3j
|
b = 5.1+2.3j
|
||||||
self.assertRaises(ValueError, pow, a, b, 0)
|
self.assertRaises(ValueError, pow, a, b, 0)
|
||||||
|
|
||||||
|
# Check some boundary conditions; some of these used to invoke
|
||||||
|
# undefined behaviour (https://bugs.python.org/issue44698). We're
|
||||||
|
# not actually checking the results of these operations, just making
|
||||||
|
# sure they don't crash (for example when using clang's
|
||||||
|
# UndefinedBehaviourSanitizer).
|
||||||
|
values = (sys.maxsize, sys.maxsize+1, sys.maxsize-1,
|
||||||
|
-sys.maxsize, -sys.maxsize+1, -sys.maxsize+1)
|
||||||
|
for real in values:
|
||||||
|
for imag in values:
|
||||||
|
with self.subTest(real=real, imag=imag):
|
||||||
|
c = complex(real, imag)
|
||||||
|
try:
|
||||||
|
c ** real
|
||||||
|
except OverflowError:
|
||||||
|
pass
|
||||||
|
try:
|
||||||
|
c ** c
|
||||||
|
except OverflowError:
|
||||||
|
pass
|
||||||
|
|
||||||
def test_boolcontext(self):
|
def test_boolcontext(self):
|
||||||
for i in range(100):
|
for i in range(100):
|
||||||
self.assertTrue(complex(random() + 1e-6, random() + 1e-6))
|
self.assertTrue(complex(random() + 1e-6, random() + 1e-6))
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
Fix undefined behaviour in complex object exponentiation.
|
|
@ -514,8 +514,6 @@ static PyObject *
|
||||||
complex_pow(PyObject *v, PyObject *w, PyObject *z)
|
complex_pow(PyObject *v, PyObject *w, PyObject *z)
|
||||||
{
|
{
|
||||||
Py_complex p;
|
Py_complex p;
|
||||||
Py_complex exponent;
|
|
||||||
long int_exponent;
|
|
||||||
Py_complex a, b;
|
Py_complex a, b;
|
||||||
TO_COMPLEX(v, a);
|
TO_COMPLEX(v, a);
|
||||||
TO_COMPLEX(w, b);
|
TO_COMPLEX(w, b);
|
||||||
|
@ -525,12 +523,21 @@ complex_pow(PyObject *v, PyObject *w, PyObject *z)
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
errno = 0;
|
errno = 0;
|
||||||
exponent = b;
|
// Check if w is an integer value that fits inside a C long, so we can
|
||||||
int_exponent = (long)exponent.real;
|
// use a faster algorithm. TO_COMPLEX(w, b), above, already handled the
|
||||||
if (exponent.imag == 0. && exponent.real == int_exponent)
|
// conversion from larger longs, as well as other types.
|
||||||
p = c_powi(a, int_exponent);
|
if (PyLong_Check(w)) {
|
||||||
else
|
int overflow = 0;
|
||||||
p = _Py_c_pow(a, exponent);
|
long int_exponent = PyLong_AsLongAndOverflow(w, &overflow);
|
||||||
|
if (int_exponent == -1 && PyErr_Occurred())
|
||||||
|
return NULL;
|
||||||
|
if (overflow == 0)
|
||||||
|
p = c_powi(a, int_exponent);
|
||||||
|
else
|
||||||
|
p = _Py_c_pow(a, b);
|
||||||
|
} else {
|
||||||
|
p = _Py_c_pow(a, b);
|
||||||
|
}
|
||||||
|
|
||||||
Py_ADJUST_ERANGE2(p.real, p.imag);
|
Py_ADJUST_ERANGE2(p.real, p.imag);
|
||||||
if (errno == EDOM) {
|
if (errno == EDOM) {
|
||||||
|
|
Loading…
Reference in New Issue