https://github.com/python/cpython/commit/e6d08bc1e302076129fd944ae9b20388e77f50df
commit: e6d08bc1e302076129fd944ae9b20388e77f50df
branch: 3.14
author: Miss Islington (bot) <[email protected]>
committer: sobolevn <[email protected]>
date: 2026-11-05T11:03:19Z
summary:

[3.14] gh-161389: Fix crash when passing invalid `flags` and `traps` to 
`decimal.Context` (GH-161393) (#161405)

gh-161389: Fix crash when passing invalid `flags` and `traps` to 
`decimal.Context` (GH-161393)
(cherry picked from commit 7054b71e1547fa575fd243916dad5a5fbc65b238)

Co-authored-by: sobolevn <[email protected]>

files:
A Misc/NEWS.d/next/Library/2026-11-05-11-56-50.gh-issue-161389.EIl2E6.rst
M Lib/test/test_decimal.py
M Modules/_decimal/_decimal.c

diff --git a/Lib/test/test_decimal.py b/Lib/test/test_decimal.py
index 17029c5aedb9088..e2a96a9e9887806 100644
--- a/Lib/test/test_decimal.py
+++ b/Lib/test/test_decimal.py
@@ -3728,6 +3728,20 @@ def test_to_integral_value(self):
         self.assertRaises(TypeError, c.to_integral_value, '10')
         self.assertRaises(TypeError, c.to_integral_value, 10, 'x')
 
+    def test_crashing_signal_dict_traps(self):
+        # gh-161389
+        Context = self.decimal.Context
+        SignalDict = type(Context().traps)
+        with self.assertRaisesRegex((KeyError, ValueError), "signal dict"):
+            Context(traps=SignalDict())
+
+    def test_crashing_signal_dict_flags(self):
+        # gh-161389
+        Context = self.decimal.Context
+        SignalDict = type(Context().flags)
+        with self.assertRaisesRegex((KeyError, ValueError), "signal dict"):
+            Context(flags=SignalDict())
+
 @requires_cdecimal
 class CContextAPItests(ContextAPItests, unittest.TestCase):
     decimal = C
diff --git 
a/Misc/NEWS.d/next/Library/2026-11-05-11-56-50.gh-issue-161389.EIl2E6.rst 
b/Misc/NEWS.d/next/Library/2026-11-05-11-56-50.gh-issue-161389.EIl2E6.rst
new file mode 100644
index 000000000000000..62fd6fe712f6dfc
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2026-11-05-11-56-50.gh-issue-161389.EIl2E6.rst
@@ -0,0 +1,2 @@
+Fixes a crash in :class:`decimal.Context` when passing invalid signaling
+dicts as *flags* and *traps*.
diff --git a/Modules/_decimal/_decimal.c b/Modules/_decimal/_decimal.c
index bead9b99309bddf..60fdec74a0fc38b 100644
--- a/Modules/_decimal/_decimal.c
+++ b/Modules/_decimal/_decimal.c
@@ -1135,6 +1135,9 @@ context_settraps_dict(PyObject *self, PyObject *value)
 
     decimal_state *state = get_module_state_from_ctx(self);
     if (PyDecSignalDict_Check(state, value)) {
+        if (SdFlagAddr(value) == NULL) {
+            return value_error_int(INVALID_SIGNALDICT_ERROR_MSG);
+        }
         flags = SdFlags(value);
     }
     else {
@@ -1201,6 +1204,9 @@ context_setstatus_dict(PyObject *self, PyObject *value)
 
     decimal_state *state = get_module_state_from_ctx(self);
     if (PyDecSignalDict_Check(state, value)) {
+        if (SdFlagAddr(value) == NULL) {
+            return value_error_int(INVALID_SIGNALDICT_ERROR_MSG);
+        }
         flags = SdFlags(value);
     }
     else {

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]

Reply via email to