summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Lechner <david@lechnology.com>2020-03-26 20:17:18 -0500
committerDamien George <damien.p.george@gmail.com>2020-03-27 13:59:18 +1100
commit5e6cee07aba4fd73c13024f091a287171bea1f17 (patch)
treeff9bdff4b49ecd48de9281995d7f7e7646293ded
parentdbba6b05dc001d6e17328318e84243dfad592381 (diff)
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
-rw-r--r--ports/unix/mpthreadport.c6
1 files changed, 5 insertions, 1 deletions
diff --git a/ports/unix/mpthreadport.c b/ports/unix/mpthreadport.c
index c604c5d21..9c696f47f 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));