mirror of
https://github.com/python/cpython.git
synced 2025-09-20 15:40:32 +00:00
gh-113841: fix possible undefined division by 0 in _Py_c_pow() (GH-127211)
`x**y == 1/x**-y ` thus changing `/=` to `*=` by negating the exponent.
This commit is contained in:
parent
a4d4c1ede2
commit
f7bb658124
3 changed files with 8 additions and 1 deletions
|
@ -338,6 +338,11 @@ class ComplexTest(ComplexesAreIdenticalMixin, unittest.TestCase):
|
||||||
except OverflowError:
|
except OverflowError:
|
||||||
pass
|
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):
|
def test_pow_with_small_integer_exponents(self):
|
||||||
# Check that small integer exponents are handled identically
|
# Check that small integer exponents are handled identically
|
||||||
# regardless of their type.
|
# regardless of their type.
|
||||||
|
|
|
@ -0,0 +1,2 @@
|
||||||
|
Fix possible undefined behavior division by zero in :class:`complex`'s
|
||||||
|
:c:func:`_Py_c_pow`.
|
|
@ -168,7 +168,7 @@ _Py_c_pow(Py_complex a, Py_complex b)
|
||||||
at = atan2(a.imag, a.real);
|
at = atan2(a.imag, a.real);
|
||||||
phase = at*b.real;
|
phase = at*b.real;
|
||||||
if (b.imag != 0.0) {
|
if (b.imag != 0.0) {
|
||||||
len /= exp(at*b.imag);
|
len *= exp(-at*b.imag);
|
||||||
phase += b.imag*log(vabs);
|
phase += b.imag*log(vabs);
|
||||||
}
|
}
|
||||||
r.real = len*cos(phase);
|
r.real = len*cos(phase);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue