diff --git a/Lib/test/test_decimal.py b/Lib/test/test_decimal.py index ef7aa5358b5cc64..bfddfa935a09f65 100644 --- a/Lib/test/test_decimal.py +++ b/Lib/test/test_decimal.py @@ -5982,6 +5982,42 @@ def test_c_signaldict_segfault(self): with self.assertRaisesRegex(ValueError, err_msg): sd.copy() + def test_c_localcontext_reentrancy(self): + # See gh-162657 for details. Determining the truth value of a traps + # or flags value can run arbitrary Python code, which may replace the + # current context. The context manager must keep the previous context + # alive and restore it on exit. + Context = C.Context + Inexact = C.Inexact + getcontext = C.getcontext + localcontext = C.localcontext + setcontext = C.setcontext + + # Allocated in __bool__() below. They reuse the memory of the dropped + # context, so that a dangling reference to it becomes observable even + # without ASan. + new_contexts = [] + + class Trap: + def __bool__(self): + setcontext(Context(prec=1)) + new_contexts.extend(Context(prec=1) for _ in range(4)) + return True + + saved_context = getcontext() + try: + for name in ('traps', 'flags'): + with self.subTest(name=name): + # The current context is now only referenced by _decimal. + setcontext(Context(prec=2)) + signals = dict(getattr(getcontext(), name)) + signals[Inexact] = Trap() + with localcontext(**{name: signals}) as ctx: + self.assertEqual(ctx.prec, 2) + self.assertEqual(getcontext().prec, 2) + finally: + setcontext(saved_context) + def test_format_fallback_capitals(self): # Fallback to _pydecimal formatting (triggered by `#` format which # is unsupported by mpdecimal) should honor the current context. diff --git a/Misc/NEWS.d/next/Library/2026-11-26-15-40-00.gh-issue-162657.LclCtx.rst b/Misc/NEWS.d/next/Library/2026-11-26-15-40-00.gh-issue-162657.LclCtx.rst new file mode 100644 index 000000000000000..cb6a281490cbc86 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-11-26-15-40-00.gh-issue-162657.LclCtx.rst @@ -0,0 +1,3 @@ +Fix a use-after-free in :func:`decimal.localcontext` when an object passed +via the ``traps`` or ``flags`` argument replaces the current context while +its truth value is determined. Patch by Xu Ziqiang. diff --git a/Modules/_decimal/_decimal.c b/Modules/_decimal/_decimal.c index 3a128a13222f9b5..579fd9a9e81467f 100644 --- a/Modules/_decimal/_decimal.c +++ b/Modules/_decimal/_decimal.c @@ -2148,16 +2148,10 @@ _decimal_localcontext_impl(PyObject *module, PyObject *local, PyObject *prec, return NULL; } - int ret = context_setattrs( - local_copy, prec, rounding, - Emin, Emax, capitals, - clamp, flags, traps - ); - if (ret < 0) { - Py_DECREF(local_copy); - return NULL; - } - + /* The manager owns local_copy and keeps the global context alive: + * context_setattrs() below can run arbitrary Python code (e.g. __bool__() + * of a traps or flags value), which may replace the current context and + * drop the last reference to it. */ PyDecContextManagerObject *self; self = PyObject_GC_New(PyDecContextManagerObject, state->PyDecContextManager_Type); @@ -2170,6 +2164,13 @@ _decimal_localcontext_impl(PyObject *module, PyObject *local, PyObject *prec, self->global = Py_NewRef(global); PyObject_GC_Track(self); + if (context_setattrs(self->local, prec, rounding, + Emin, Emax, capitals, + clamp, flags, traps) < 0) { + Py_DECREF(self); + return NULL; + } + return (PyObject *)self; }