Skip to content
Merged
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
14 changes: 14 additions & 0 deletions Lib/test/test_decimal.py
Original file line number Diff line number Diff line change
Expand Up @@ -3763,6 +3763,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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fixes a crash in :class:`decimal.Context` when passing invalid signaling
dicts as *flags* and *traps*.
6 changes: 6 additions & 0 deletions Modules/_decimal/_decimal.c
Original file line number Diff line number Diff line change
Expand Up @@ -1162,6 +1162,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 {
Expand Down Expand Up @@ -1228,6 +1231,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 {
Expand Down
Loading