diff --git a/Lib/test/test_decimal.py b/Lib/test/test_decimal.py index 7524822632bae7..7e6adf2580ea7e 100644 --- a/Lib/test/test_decimal.py +++ b/Lib/test/test_decimal.py @@ -4026,6 +4026,9 @@ def test_flag_comparisons(self): self.assertNotEqual(d, c.flags) self.assertNotEqual(c.flags, d) + self.assertNotEqual(None, c.flags) + self.assertNotEqual(c.flags, None) + @requires_IEEE_754 def test_float_operation(self): Decimal = self.decimal.Decimal diff --git a/Misc/NEWS.d/next/Library/2026-09-24-10-17-06.gh-issue-158063.Bx9Jpl.rst b/Misc/NEWS.d/next/Library/2026-09-24-10-17-06.gh-issue-158063.Bx9Jpl.rst new file mode 100644 index 00000000000000..dcf1a167c8d1d4 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-09-24-10-17-06.gh-issue-158063.Bx9Jpl.rst @@ -0,0 +1,3 @@ +Fix an out-of-bounds read in the C implementation of :mod:`decimal` when +comparing the ``flags`` or ``traps`` of a :class:`~decimal.Context` with an +object that is not a signal dict, such as ``None``. diff --git a/Modules/_decimal/_decimal.c b/Modules/_decimal/_decimal.c index ada9b02d690717..26666ddf5c75d6 100644 --- a/Modules/_decimal/_decimal.c +++ b/Modules/_decimal/_decimal.c @@ -795,7 +795,8 @@ signaldict_richcompare(PyObject *v, PyObject *w, int op) decimal_state *state = get_module_state_by_def(Py_TYPE(v)); assert(PyDecSignalDict_Check(state, v)); - if ((SdFlagAddr(v) == NULL) || (SdFlagAddr(w) == NULL)) { + if ((SdFlagAddr(v) == NULL) || + (PyDecSignalDict_Check(state, w) && SdFlagAddr(w) == NULL)) { return value_error_ptr(INVALID_SIGNALDICT_ERROR_MSG); }