Skip to content

Commit 151c88f

Browse files
committed
Improve logic handling trial C stack overflow
1 parent 774efb5 commit 151c88f

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

Python/ceval.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -328,15 +328,17 @@ _Py_CheckRecursiveCall(PyThreadState *tstate, const char *where)
328328
assert(tstate->c_stack_soft_limit != 0);
329329
if (tstate->c_stack_hard_limit == 0) {
330330
#ifdef USE_STACKCHECK
331-
assert(_PyOS_CheckStack(PYOS_STACK_MARGIN));
332331
if (_PyOS_CheckStack(PYOS_STACK_MARGIN * 2) == 0) {
333332
tstate->c_stack_soft_limit = here_addr - PYOS_STACK_MARGIN_BYTES;
334333
return 0;
335334
}
336-
else {
337-
assert(tstate->c_stack_soft_limit != UINTPTR_MAX);
338-
tstate->c_stack_hard_limit = tstate->c_stack_soft_limit - PYOS_STACK_MARGIN_BYTES;
335+
assert(tstate->c_stack_soft_limit != UINTPTR_MAX);
336+
int margin = PYOS_STACK_MARGIN*3/2;
337+
while (margin > 0 && _PyOS_CheckStack(margin)) {
338+
margin -= PYOS_STACK_MARGIN/2;
339339
}
340+
tstate->c_stack_hard_limit = here_addr - margin * sizeof(void *);
341+
tstate->c_stack_soft_limit = tstate->c_stack_hard_limit + PYOS_STACK_MARGIN_BYTES;
340342
#else
341343
assert(tstate->c_stack_soft_limit == UINTPTR_MAX);
342344
tstate->c_stack_soft_limit = here_addr - Py_C_STACK_SIZE;

0 commit comments

Comments
 (0)