Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
36 changes: 36 additions & 0 deletions Lib/test/test_decimal.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
21 changes: 11 additions & 10 deletions Modules/_decimal/_decimal.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
}

Expand Down
Loading