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
pull/5740/head
David Lechner 2020-03-26 20:17:18 -05:00 zatwierdzone przez Damien George
rodzic dbba6b05dc
commit 5e6cee07ab
1 zmienionych plików z 5 dodań i 1 usunięć

Wyświetl plik

@ -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
// this value seems to be about right for both 32-bit and 64-bit builds
*stack_size -= 8192;
if (*stack_size >= 2 * 8192) {
*stack_size -= 8192;
} else {
*stack_size /= 2;
}
// add thread to linked list of all threads
thread_t *th = malloc(sizeof(thread_t));