Skip to content

Commit

Permalink
pythongh-113841: fix possible undefined division by 0 in _Py_c_pow() (p…
Browse files Browse the repository at this point in the history
…ythonGH-127211)

`x**y == 1/x**-y ` thus changing `/=` to `*=` by negating the exponent.
  • Loading branch information
skirpichev authored Nov 24, 2024
1 parent a4d4c1e commit f7bb658
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 1 deletion.
5 changes: 5 additions & 0 deletions Lib/test/test_complex.py
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,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.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix possible undefined behavior division by zero in :class:`complex`'s
:c:func:`_Py_c_pow`.
2 changes: 1 addition & 1 deletion Objects/complexobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,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);
Expand Down

0 comments on commit f7bb658

Please sign in to comment.