Skip to content

Commit 57e3d34

Browse files
[3.15] gh-161389: Fix crash when passing invalid flags and traps to decimal.Context (GH-161393) (#161404)
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 61c0928 commit 57e3d34

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

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

11691169
decimal_state *state = get_module_state_from_ctx(self);
11701170
if (PyDecSignalDict_Check(state, value)) {
1171+
if (SdFlagAddr(value) == NULL) {
1172+
return value_error_int(INVALID_SIGNALDICT_ERROR_MSG);
1173+
}
11711174
flags = SdFlags(value);
11721175
}
11731176
else {
@@ -1234,6 +1237,9 @@ context_setstatus_dict(PyObject *self, PyObject *value)
12341237

12351238
decimal_state *state = get_module_state_from_ctx(self);
12361239
if (PyDecSignalDict_Check(state, value)) {
1240+
if (SdFlagAddr(value) == NULL) {
1241+
return value_error_int(INVALID_SIGNALDICT_ERROR_MSG);
1242+
}
12371243
flags = SdFlags(value);
12381244
}
12391245
else {

0 commit comments

Comments
 (0)