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:
Miss Islington (bot) 2021-07-26 12:29:52 -07:00 committed by GitHub
parent 8a37e8cf45
commit 256d97c8a3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 37 additions and 8 deletions

View file

@ -514,8 +514,6 @@ static PyObject *
complex_pow(PyObject *v, PyObject *w, PyObject *z)
{
Py_complex p;
Py_complex exponent;
long int_exponent;
Py_complex a, b;
TO_COMPLEX(v, a);
TO_COMPLEX(w, b);
@ -525,12 +523,21 @@ complex_pow(PyObject *v, PyObject *w, PyObject *z)
return NULL;
}
errno = 0;
exponent = b;
int_exponent = (long)exponent.real;
if (exponent.imag == 0. && exponent.real == int_exponent)
p = c_powi(a, int_exponent);
else
p = _Py_c_pow(a, exponent);
// Check if w is an integer value that fits inside a C long, so we can
// use a faster algorithm. TO_COMPLEX(w, b), above, already handled the
// conversion from larger longs, as well as other types.
if (PyLong_Check(w)) {
int overflow = 0;
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);
if (errno == EDOM) {