8000 [3.13] gh-130230: Fix crash in pow() with only Decimal third argument (GH-130237) by miss-islington · Pull Request #130246 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

[3.13] gh-130230: Fix crash in pow() with only Decimal third argument (GH-130237) #130246

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 18, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
gh-130230: Fix crash in pow() with only Decimal third argument (GH-13…
…0237)

(cherry picked from commit b93b7e5)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
  • Loading branch information
serhiy-storchaka authored and miss-islington committed Feb 18, 2025
commit 39e8192e4d58f5891d8177e9bc3b1d4020d7e643
9 changes: 9 additions & 0 deletions Lib/test/test_decimal.py
Original file line number Diff line number Diff line change
Expand Up @@ -4458,6 +4458,15 @@ def test_implicit_context(self):
self.assertIs(Decimal("NaN").fma(7, 1).is_nan(), True)
# three arg power
self.assertEqual(pow(Decimal(10), 2, 7), 2)
if self.decimal == C:
self.assertEqual(pow(10, Decimal(2), 7), 2)
self.assertEqual(pow(10, 2, Decimal(7)), 2)
else:
# XXX: Three-arg power doesn't use __rpow__.
self.assertRaises(TypeError, pow, 10, Decimal(2), 7)
# XXX: There is no special method to dispatch on the
# third arg of three-arg power.
self.assertRaises(TypeError, pow, 10, 2, Decimal(7))
# exp
self.assertEqual(Decimal("1.01").exp(), 3)
# is_normal
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix crash in :func:`pow` with only :class:`~decimal.Decimal` third argument.
20 changes: 19 additions & 1 deletion Modules/_decimal/_decimal.c
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,24 @@
return get_module_state(mod);
}

static inline decimal_state *
find_state_ternary(PyObject *left, PyObject *right, PyObject *modulus)
{
PyTypeObject *base;
if (PyType_GetBaseByToken(Py_TYPE(left), &dec_spec, &base) != 1) {

Check failure on line 147 in Modules/_decimal/_decimal.c

View workflow job for this annotation

GitHub Actions / Ubuntu (free-threading) / build and test (ubuntu-24.04)

implicit declaration of function ‘PyType_GetBaseByToken’ [-Werror=implicit-function-declaration]

Check failure on line 147 in Modules/_decimal/_decimal.c

View workflow job for this annotation

GitHub Actions / Ubuntu (free-threading) / build and test (ubuntu-24.04)

‘dec_spec’ undeclared (first use in this function)

Check failure on line 147 in Modules/_decimal/_decimal.c

View workflow job for this annotation

GitHub Actions / Ubuntu SSL tests with OpenSSL (ubuntu-24.04, 3.0.15)

implicit declaration of function ‘PyType_GetBaseByToken’ [-Werror=implicit-function-declaration]

Check failure on line 147 in Modules/_decimal/_decimal.c

View workflow job for this annotation

GitHub Actions / Ubuntu SSL tests with OpenSSL (ubuntu-24.04, 3.0.15)

‘dec_spec’ undeclared (first use in this function)

Check failure on line 147 in Modules/_decimal/_decimal.c

View workflow job for this annotation

GitHub Actions / Ubuntu SSL tests with OpenSSL (ubuntu-24.04, 3.3.2)

implicit declaration of function ‘PyType_GetBaseByToken’ [-Werror=implicit-function-declaration]

Check failure on line 147 in Modules/_decimal/_decimal.c

View workflow job for this annotation

GitHub Actions / Ubuntu SSL tests with OpenSSL (ubuntu-24.04, 3.3.2)

‘dec_spec’ undeclared (first use in this function)

Check failure on line 147 in Modules/_decimal/_decimal.c

View workflow job for this annotation

GitHub Actions / Ubuntu SSL tests with OpenSSL (ubuntu-24.04, 3.2.3)

implicit declaration of function ‘PyType_GetBaseByToken’ [-Werror=implicit-function-declaration]

Check failure on line 147 in Modules/_decimal/_decimal.c

View workflow job for this annotation

GitHub Actions / Ubuntu SSL tests with OpenSSL (ubuntu-24.04, 3.2.3)

‘dec_spec’ undeclared (first use in this function)

Check failure on line 147 in Modules/_decimal/_decimal.c

View workflow job for this annotation

GitHub Actions / Ubuntu SSL tests with OpenSSL (ubuntu-24.04, 3.1.7)

implicit declaration of function ‘PyType_GetBaseByToken’ [-Werror=implicit-function-declaration]

Check failure on line 147 in Modules/_decimal/_decimal.c

View workflow job for this annotation

GitHub Actions / Ubuntu SSL tests with OpenSSL (ubuntu-24.04, 3.1.7)

‘dec_spec’ undeclared (first use in this function)

Check failure on line 147 in Modules/_decimal/_decimal.c

View workflow job for this annotation

GitHub Actions / Ubuntu / build and test (ubuntu-24.04)

implicit declaration of function ‘PyType_GetBaseByToken’ [-Werror=implicit-function-declaration]

Check failure on line 147 in Modules/_decimal/_decimal.c

View workflow job for this annotation

GitHub Actions / Ubuntu / build and test (ubuntu-24.04)

‘dec_spec’ undeclared (first use in this function)

Check failure on line 147 in Modules/_decimal/_decimal.c

View workflow job for this annotation

GitHub Actions / Hypothesis tests on Ubuntu

implicit declaration of function ‘PyType_GetBaseByToken’ [-Werror=implicit-function-declaration]

Check failure on line 147 in Modules/_decimal/_decimal.c

View workflow job for this annotation

GitHub Actions / Hypothesis tests on Ubuntu

‘dec_spec’ undeclared (first use in this function)

Check failure on line 147 in Modules/_decimal/_decimal.c

View workflow job for this annotation

GitHub Actions / Ubuntu (free-threading) / build and test (ubuntu-24.04-arm)

implicit declaration of function ‘PyType_GetBaseByToken’ [-Werror=implicit-function-declaration]

Check failure on line 147 in Modules/_decimal/_decimal.c

View workflow job for this annotation

GitHub Actions / Ubuntu (free-threading) / build and test (ubuntu-24.04-arm)

‘dec_spec’ undeclared (first use in this function)

Check failure on line 147 in Modules/_decimal/_decimal.c

View workflow job for this annotation

GitHub Actions / Address sanitizer (ubuntu-24.04)

implicit declaration of function ‘PyType_GetBaseByToken’ [-Werror=implicit-function-declaration]

Check failure on line 147 in Modules/_decimal/_decimal.c

View workflow job for this annotation

GitHub Actions / Address sanitizer (ubuntu-24.04)

‘dec_spec’ undeclared (first use in this function)

Check warning on line 147 in Modules/_decimal/_decimal.c

View workflow job for this annotation

GitHub Actions / Windows / build (arm64)

'PyType_GetBaseByToken' undefined; assuming extern returning int [D:\a\cpython\cpython\PCbuild\_decimal.vcxproj]

Check failure on line 147 in Modules/_decimal/_decimal.c

View workflow job for this annotation

GitHub Actions / Windows / build (arm64)

'dec_spec': undeclared identifier [D:\a\cpython\cpython\PCbuild\_decimal.vcxproj]

Check failure on line 147 in Modules/_decimal/_decimal.c

View workflow job for this annotation

GitHub Actions / Ubuntu / build and test (ubuntu-24.04-arm)

implicit declaration of function ‘PyType_GetBaseByToken’ [-Werror=implicit-function-declaration]

Check failure on line 147 in Modules/_decimal/_decimal.c

View workflow job for this annotation

GitHub Actions / Ubuntu / build and test (ubuntu-24.04-arm)

‘dec_spec’ undeclared (first use in this function)

Check warning on line 147 in Modules/_decimal/_decimal.c

View workflow job for this annotation

GitHub Actions / Windows (free-threading) / build (arm64)

'PyType_GetBaseByToken' undefined; assuming extern returning int [D:\a\cpython\cpython\PCbuild\_decimal.vcxproj]

Check failure on line 147 in Modules/_decimal/_decimal.c

View workflow job for this annotation

GitHub Actions / Windows (free-threading) / build (arm64)

'dec_spec': undeclared identifier [D:\a\cpython\cpython\PCbuild\_decimal.vcxproj]

Check warning on line 147 in Modules/_decimal/_decimal.c

View workflow job for this annotation

GitHub Actions / Windows / build and test (x64)

'PyType_GetBaseByToken' undefined; assuming extern returning int [D:\a\cpython\cpython\PCbuild\_decimal.vcxproj]

Check failure on line 147 in Modules/_decimal/_decimal.c

View workflow job for this annotation

GitHub Actions / Windows / build and test (x64)

'dec_spec': undeclared identifier [D:\a\cpython\cpython\PCbuild\_decimal.vcxproj]

Check warning on line 147 in Modules/_decimal/_decimal.c

View workflow job for this annotation

GitHub Actions / Windows (free-threading) / build and test (x64)

'PyType_GetBaseByToken' undefined; assuming extern returning int [D:\a\cpython\cpython\PCbuild\_decimal.vcxproj]

Check failure on line 147 in Modules/_decimal/_decimal.c

View workflow job for this annotation

GitHub Actions / Windows (free-threading) / build and test (x64)

'dec_spec': undeclared identifier [D:\a\cpython\cpython\PCbuild\_decimal.vcxproj]
assert(!PyErr_Occurred());
if (PyType_GetBaseByToken(Py_TYPE(right), &dec_spec, &base) != 1) {

Check failure on line 149 in Modules/_decimal/_decimal.c

View workflow job for this annotation

GitHub Actions / Windows / build (arm64)

'dec_spec': undeclared identifier [D:\a\cpython\cpython\PCbuild\_decimal.vcxproj]

Check failure on line 149 in Modules/_decimal/_decimal.c

View workflow job for this annotation

GitHub Actions / Windows (free-threading) / build (arm64)

'dec_spec': undeclared identifier [D:\a\cpython\cpython\PCbuild\_decimal.vcxproj]

Check failure on line 149 in Modules/_decimal/_decimal.c

View workflow job for this annotation

GitHub Actions / Windows / build and test (x64)

'dec_spec': undeclared identifier [D:\a\cpython\cpython\PCbuild\_decimal.vcxproj]

Check failure on line 149 in Modules/_decimal/_decimal.c

View workflow job for this annotation

GitHub Actions / Windows (free-threading) / build and test (x64)

'dec_spec': undeclared identifier [D:\a\cpython\cpython\PCbuild\_decimal.vcxproj]
assert(!PyErr_Occurred());
PyType_GetBaseByToken(Py_TYPE(modulus), &dec_spec, &base);

Check failure on line 151 in Modules/_decimal/_decimal.c

View workflow job for this annotation

GitHub Actions / Windows / build (arm64)

'dec_spec': undeclared identifier [D:\a\cpython\cpython\PCbuild\_decimal.vcxproj]

Check failure on line 151 in Modules/_decimal/_decimal.c

View workflow job for this annotation

GitHub Actions / Windows (free-threading) / build (arm64)

'dec_spec': undeclared identifier [D:\a\cpython\cpython\PCbuild\_decimal.vcxproj]

Check failure on line 151 in Modules/_decimal/_decimal.c

View workflow job for this annotation

GitHub Actions / Windows / build and test (x64)

'dec_spec': undeclared identifier [D:\a\cpython\cpython\PCbuild\_decimal.vcxproj]

Check failure on line 151 in Modules/_decimal/_decimal.c

View workflow job for this annotation

GitHub Actions / Windows (free-threading) / build and test (x64)

'dec_spec': undeclared identifier [D:\a\cpython\cpython\PCbuild\_decimal.vcxproj]
}
}
assert(base != NULL);
void *state = _PyType_GetModuleState(base);
assert(state != NULL);
Py_DECREF(base);
return (decimal_state *)state;
}


#if !defined(MPD_VERSION_HEX) || MPD_VERSION_HEX < 0x02050000
#error "libmpdec version >= 2.5.0 required"
Expand Down Expand Up @@ -4305,7 +4323,7 @@
PyObject *context;
uint32_t status = 0;

decimal_state *state = find_state_left_or_right(base, exp);
decimal_state *state = find_state_ternary(base, exp, mod);
CURRENT_CONTEXT(state, context);
CONVERT_BINOP(&a, &b, base, exp, context);

Expand Down
Loading
0