Skip to content

Commit 6236c98

Browse files
xry111vstinner
authored andcommitted
bpo-36856: Handle possible overflow in faulthandler_stack_overflow (GH-13205)
1 parent 79972f1 commit 6236c98

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

Modules/faulthandler.c

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1121,13 +1121,26 @@ faulthandler_stack_overflow(PyObject *self, PyObject *Py_UNUSED(ignored))
11211121
{
11221122
size_t depth, size;
11231123
uintptr_t sp = (uintptr_t)&depth;
1124-
uintptr_t stop;
1124+
uintptr_t stop, lower_limit, upper_limit;
11251125

11261126
faulthandler_suppress_crash_report();
11271127
depth = 0;
1128-
stop = stack_overflow(sp - STACK_OVERFLOW_MAX_SIZE,
1129-
sp + STACK_OVERFLOW_MAX_SIZE,
1130-
&depth);
1128+
1129+
if (STACK_OVERFLOW_MAX_SIZE <= sp) {
1130+
lower_limit = sp - STACK_OVERFLOW_MAX_SIZE;
1131+
}
1132+
else {
1133+
lower_limit = 0;
1134+
}
1135+
1136+
if (UINTPTR_MAX - STACK_OVERFLOW_MAX_SIZE >= sp) {
1137+
upper_limit = sp + STACK_OVERFLOW_MAX_SIZE;
1138+
}
1139+
else {
1140+
upper_limit = UINTPTR_MAX;
1141+
}
1142+
1143+
stop = stack_overflow(lower_limit, upper_limit, &depth);
11311144
if (sp < stop)
11321145
size = stop - sp;
11331146
else

0 commit comments

Comments
 (0)