From 59321065dbe80160744386b9f3698215bb30a747 Mon Sep 17 00:00:00 2001 From: Marian Buschsieweke Date: Tue, 22 Sep 2020 14:36:31 +0200 Subject: [PATCH 1/2] core/thread.c: Silence -Wcast-align flase positives Verified that each warning generated by -Wcast-align is indeed a false positive and used an (intermediate) cast to `uintptr_t` to silence the warnings. --- core/thread.c | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/core/thread.c b/core/thread.c index 731538565c..74c2984753 100644 --- a/core/thread.c +++ b/core/thread.c @@ -171,7 +171,9 @@ void thread_add_to_list(list_node_t *list, thread_t *thread) #ifdef DEVELHELP uintptr_t thread_measure_stack_free(char *stack) { - uintptr_t *stackp = (uintptr_t *)stack; + /* Alignment of stack has been fixed (if needed) by thread_create(), so + * we can silence -Wcast-align here */ + uintptr_t *stackp = (uintptr_t *)(uintptr_t)stack; /* assume that the comparison fails before or after end of stack */ /* assume that the stack grows "downwards" */ @@ -216,8 +218,10 @@ kernel_pid_t thread_create(char *stack, int stacksize, uint8_t priority, if (stacksize < 0) { DEBUG("thread_create: stacksize is too small!\n"); } - /* allocate our thread control block at the top of our stackspace */ - thread_t *thread = (thread_t *)(stack + stacksize); + /* allocate our thread control block at the top of our stackspace. Cast to + * (uintptr_t) intermediately to silence -Wcast-align. (We manually made + * sure alignment is correct above.) */ + thread_t *thread = (thread_t *)(uintptr_t)(stack + stacksize); #ifdef PICOLIBC_TLS stacksize -= _tls_size(); @@ -228,9 +232,10 @@ kernel_pid_t thread_create(char *stack, int stacksize, uint8_t priority, #if defined(DEVELHELP) || defined(SCHED_TEST_STACK) if (flags & THREAD_CREATE_STACKTEST) { - /* assign each int of the stack the value of it's address */ - uintptr_t *stackmax = (uintptr_t *)(stack + stacksize); - uintptr_t *stackp = (uintptr_t *)stack; + /* assign each int of the stack the value of it's address. Alignment + * has been handled above, so silence -Wcast-align */ + uintptr_t *stackmax = (uintptr_t *)(uintptr_t)(stack + stacksize); + uintptr_t *stackp = (uintptr_t *)(uintptr_t)stack; while (stackp < stackmax) { *stackp = (uintptr_t)stackp; @@ -238,8 +243,9 @@ kernel_pid_t thread_create(char *stack, int stacksize, uint8_t priority, } } else { - /* create stack guard */ - *(uintptr_t *)stack = (uintptr_t)stack; + /* create stack guard. Alignment has been handled above, so silence + * -Wcast-align */ + *(uintptr_t *)(uintptr_t)stack = (uintptr_t)stack; } #endif From e028f6bbe6e820506bf07ed26485876332123c80 Mon Sep 17 00:00:00 2001 From: Marian Buschsieweke Date: Fri, 25 Sep 2020 14:30:33 +0200 Subject: [PATCH 2/2] core/thread: thread_measure_stack_free() const arg Add `const` to stack pointer passed to thread_measure_stack_free() --- core/include/thread.h | 2 +- core/thread.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/core/include/thread.h b/core/include/thread.h index a6108bf862..4f2a1ed3ab 100644 --- a/core/include/thread.h +++ b/core/include/thread.h @@ -540,7 +540,7 @@ const char *thread_getname(kernel_pid_t pid); * * @return the amount of unused space of the thread's stack */ -uintptr_t thread_measure_stack_free(char *stack); +uintptr_t thread_measure_stack_free(const char *stack); #endif /* DEVELHELP */ /** diff --git a/core/thread.c b/core/thread.c index 74c2984753..f943a15416 100644 --- a/core/thread.c +++ b/core/thread.c @@ -169,7 +169,7 @@ void thread_add_to_list(list_node_t *list, thread_t *thread) } #ifdef DEVELHELP -uintptr_t thread_measure_stack_free(char *stack) +uintptr_t thread_measure_stack_free(const char *stack) { /* Alignment of stack has been fixed (if needed) by thread_create(), so * we can silence -Wcast-align here */