Skip to content

Commit e6d08bc

Browse files
[3.14] gh-161389: Fix crash when passing invalid flags and traps to decimal.Context (GH-161393) (#161405)
gh-161389: Fix crash when passing invalid `flags` and `traps` to `decimal.Context` (GH-161393) (cherry picked from commit 7054b71) Co-authored-by: sobolevn <mail@sobolevn.me>
1 parent 0b87c95 commit e6d08bc

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
@@ -3728,6 +3728,20 @@ def test_to_integral_value(self):
37283728
self.assertRaises(TypeError, c.to_integral_value, '10')
37293729
self.assertRaises(TypeError, c.to_integral_value, 10, 'x')
37303730

3731+
def test_crashing_signal_dict_traps(self):
3732+
# gh-161389
3733+
Context = self.decimal.Context
3734+
SignalDict = type(Context().traps)
3735+
with self.assertRaisesRegex((KeyError, ValueError), "signal dict"):
3736+
Context(traps=SignalDict())
3737+
3738+
def test_crashing_signal_dict_flags(self):
3739+
# gh-161389
3740+
Context = self.decimal.Context
3741+
SignalDict = type(Context().flags)
3742+
with self.assertRaisesRegex((KeyError, ValueError), "signal dict"):
3743+
Context(flags=SignalDict())
3744+
37313745
@requires_cdecimal
37323746
class CContextAPItests(ContextAPItests, unittest.TestCase):
37333747
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
@@ -1135,6 +1135,9 @@ context_settraps_dict(PyObject *self, PyObject *value)
11351135

11361136
decimal_state *state = get_module_state_from_ctx(self);
11371137
if (PyDecSignalDict_Check(state, value)) {
1138+
if (SdFlagAddr(value) == NULL) {
1139+
return value_error_int(INVALID_SIGNALDICT_ERROR_MSG);
1140+
}
11381141
flags = SdFlags(value);
11391142
}
11401143
else {
@@ -1201,6 +1204,9 @@ context_setstatus_dict(PyObject *self, PyObject *value)
12011204

12021205
decimal_state *state = get_module_state_from_ctx(self);
12031206
if (PyDecSignalDict_Check(state, value)) {
1207+
if (SdFlagAddr(value) == NULL) {
1208+
return value_error_int(INVALID_SIGNALDICT_ERROR_MSG);
1209+
}
12041210
flags = SdFlags(value);
12051211
}
12061212
else {

0 commit comments

Comments
 (0)