unix/mpthreadport: Fix crash when thread stack size <= 8k.

The stack size adjustment for detecting stack overflow in threads was not
taking into account that the requested stack size could be <= 8k, in which
case the subtraction would overflow.  This is fixed in this commit by
ensuring that the adjustment can't be more than the available size.

This fixes the test tests/thread/thread_stacksize1.py which sometimes
crashes with a segmentation fault because of an uncaught NLR jump, which is
a "maximum recursion depth exceeded" exception.

Suggested-by: @dpgeorge
This commit is contained in:
David Lechner 2020-03-26 20:17:18 -05:00 committed by Damien George
parent dbba6b05dc
commit 5e6cee07ab
1 changed files with 5 additions and 1 deletions

View File

@ -221,7 +221,11 @@ void mp_thread_create(void *(*entry)(void *), void *arg, size_t *stack_size) {
// adjust stack_size to provide room to recover from hitting the limit // adjust stack_size to provide room to recover from hitting the limit
// this value seems to be about right for both 32-bit and 64-bit builds // this value seems to be about right for both 32-bit and 64-bit builds
if (*stack_size >= 2 * 8192) {
*stack_size -= 8192; *stack_size -= 8192;
} else {
*stack_size /= 2;
}
// add thread to linked list of all threads // add thread to linked list of all threads
thread_t *th = malloc(sizeof(thread_t)); thread_t *th = malloc(sizeof(thread_t));