From 5e6cee07aba4fd73c13024f091a287171bea1f17 Mon Sep 17 00:00:00 2001 From: David Lechner Date: Thu, 26 Mar 2020 20:17:18 -0500 Subject: [PATCH] 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 --- ports/unix/mpthreadport.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/ports/unix/mpthreadport.c b/ports/unix/mpthreadport.c index c604c5d21c..9c696f47f4 100644 --- a/ports/unix/mpthreadport.c +++ b/ports/unix/mpthreadport.c @@ -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));