summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSergey B Kirpichev <skirpichev@gmail.com>2024-11-24 07:37:37 (GMT)
committerGitHub <noreply@github.com>2024-11-24 07:37:37 (GMT)
commitf7bb658124aba74be4c13f498bf46cfded710ef9 (patch)
treebe3f2fdeda629af39f82d9911dc54f2717bfa60b
parenta4d4c1ede21f9fa72280f4fc0f50212eecfac9ae (diff)
downloadcpython-f7bb658124aba74be4c13f498bf46cfded710ef9.zip
cpython-f7bb658124aba74be4c13f498bf46cfded710ef9.tar.gz
cpython-f7bb658124aba74be4c13f498bf46cfded710ef9.tar.bz2
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.
-rw-r--r--Lib/test/test_complex.py5
-rw-r--r--Misc/NEWS.d/next/Core_and_Builtins/2024-11-24-07-01-28.gh-issue-113841.WFg-Bu.rst2
-rw-r--r--Objects/complexobject.c2
3 files changed, 8 insertions, 1 deletions
diff --git a/Lib/test/test_complex.py b/Lib/test/test_complex.py
index ecc9731..c51327c 100644
--- a/Lib/test/test_complex.py
+++ b/Lib/test/test_complex.py
@@ -338,6 +338,11 @@ class ComplexTest(ComplexesAreIdenticalMixin, unittest.TestCase):
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.
diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2024-11-24-07-01-28.gh-issue-113841.WFg-Bu.rst b/Misc/NEWS.d/next/Core_and_Builtins/2024-11-24-07-01-28.gh-issue-113841.WFg-Bu.rst
new file mode 100644
index 0000000..2b07fdf
--- /dev/null
+++ b/Misc/NEWS.d/next/Core_and_Builtins/2024-11-24-07-01-28.gh-issue-113841.WFg-Bu.rst
@@ -0,0 +1,2 @@
+Fix possible undefined behavior division by zero in :class:`complex`'s
+:c:func:`_Py_c_pow`.
diff --git a/Objects/complexobject.c b/Objects/complexobject.c
index 7b4948f..9faa575 100644
--- a/Objects/complexobject.c
+++ b/Objects/complexobject.c
@@ -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);