Skip to content

Commit 7054b71

Browse files
authored
gh-161389: Fix crash when passing invalid flags and traps to decimal.Context (#161393)
1 parent 5aa2f70 commit 7054b71

3 files changed

Lines changed: 22 additions & 0 deletions

File tree

‎Lib/test/test_decimal.py‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3763,6 +3763,20 @@ def test_to_integral_value(self):
37633763
self.assertRaises(TypeError, c.to_integral_value, '10')
37643764
self.assertRaises(TypeError, c.to_integral_value, 10, 'x')
37653765

3766+
def test_crashing_signal_dict_traps(self):
3767+
# gh-161389
3768+
Context = self.decimal.Context
3769+
SignalDict = type(Context().traps)
3770+
with self.assertRaisesRegex((KeyError, ValueError), "signal dict"):
3771+
Context(traps=SignalDict())
3772+
3773+
def test_crashing_signal_dict_flags(self):
3774+
# gh-161389
3775+
Context = self.decimal.Context
3776+
SignalDict = type(Context().flags)
3777+
with self.assertRaisesRegex((KeyError, ValueError), "signal dict"):
3778+
Context(flags=SignalDict())
3779+
37663780
@requires_cdecimal
37673781
class CContextAPItests(ContextAPItests, unittest.TestCase):
37683782
decimal = C
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fixes a crash in :class:`decimal.Context` when passing invalid signaling
2+
dicts as *flags* and *traps*.

‎Modules/_decimal/_decimal.c‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1162,6 +1162,9 @@ context_settraps_dict(PyObject *self, PyObject *value)
11621162

11631163
decimal_state *state = get_module_state_from_ctx(self);
11641164
if (PyDecSignalDict_Check(state, value)) {
1165+
if (SdFlagAddr(value) == NULL) {
1166+
return value_error_int(INVALID_SIGNALDICT_ERROR_MSG);
1167+
}
11651168
flags = SdFlags(value);
11661169
}
11671170
else {
@@ -1228,6 +1231,9 @@ context_setstatus_dict(PyObject *self, PyObject *value)
12281231

12291232
decimal_state *state = get_module_state_from_ctx(self);
12301233
if (PyDecSignalDict_Check(state, value)) {
1234+
if (SdFlagAddr(value) == NULL) {
1235+
return value_error_int(INVALID_SIGNALDICT_ERROR_MSG);
1236+
}
12311237
flags = SdFlags(value);
12321238
}
12331239
else {

0 commit comments

Comments
 (0)