Skip to content

Commit f7bb658

Browse files
authored
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.
1 parent a4d4c1e commit f7bb658

File tree

3 files changed

+8
-1
lines changed

3 files changed

+8
-1
lines changed

Lib/test/test_complex.py

+5
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,11 @@ def test_pow(self):
338338
except OverflowError:
339339
pass
340340

341+
# gh-113841: possible undefined division by 0 in _Py_c_pow()
342+
x, y = 9j, 33j**3
343+
with self.assertRaises(OverflowError):
344+
x**y
345+
341346
def test_pow_with_small_integer_exponents(self):
342347
# Check that small integer exponents are handled identically
343348
# regardless of their type.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix possible undefined behavior division by zero in :class:`complex`'s
2+
:c:func:`_Py_c_pow`.

Objects/complexobject.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ _Py_c_pow(Py_complex a, Py_complex b)
168168
at = atan2(a.imag, a.real);
169169
phase = at*b.real;
170170
if (b.imag != 0.0) {
171-
len /= exp(at*b.imag);
171+
len *= exp(-at*b.imag);
172172
phase += b.imag*log(vabs);
173173
}
174174
r.real = len*cos(phase);

0 commit comments

Comments
 (0)