From 94e83cd8d3ec060ba379d1ddfe0385ae2ba4a801 Mon Sep 17 00:00:00 2001 From: lazerg Date: Thu, 24 Sep 2026 15:17:13 +0500 Subject: [PATCH] gh-158063: Fix out-of-bounds read when comparing decimal signal dicts --- Lib/test/test_decimal.py | 3 +++ .../Library/2026-09-24-10-17-06.gh-issue-158063.Bx9Jpl.rst | 3 +++ Modules/_decimal/_decimal.c | 3 ++- 3 files changed, 8 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Library/2026-09-24-10-17-06.gh-issue-158063.Bx9Jpl.rst diff --git a/Lib/test/test_decimal.py b/Lib/test/test_decimal.py index 7524822632bae7a..7e6adf2580ea7e8 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 000000000000000..dcf1a167c8d1d45 --- /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 ada9b02d690717f..26666ddf5c75d6c 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); }