From 124e34d71e9840e18f09e48d1511b0af15401a23 Mon Sep 17 00:00:00 2001 From: Philip Vedin Date: Thu, 8 Feb 2024 14:02:33 +0100 Subject: [PATCH] Ensure CC_ASSERT calls do not have side effects Since builds may be compiled without asserts, it may be the case that expressions that are to be asserted are never evaluated; this would lead to incorrect behaviour if they contain side effects. --- src/freertos/osal.c | 16 +++++++++++----- src/freertos/sys/osal_cc.h | 1 + src/linux/sys/osal_cc.h | 2 ++ src/rt-kernel/sys/osal_cc.h | 1 + 4 files changed, 15 insertions(+), 5 deletions(-) diff --git a/src/freertos/osal.c b/src/freertos/osal.c index 26d83e9..c879efa 100644 --- a/src/freertos/osal.c +++ b/src/freertos/osal.c @@ -234,20 +234,26 @@ void os_timer_set (os_timer_t * timer, uint32_t us) void os_timer_start (os_timer_t * timer) { /* Start timer by updating the period */ - CC_ASSERT ( - xTimerChangePeriod ( + BaseType_t status = xTimerChangePeriod ( timer->handle, (timer->us / portTICK_PERIOD_MS) / 1000, - portMAX_DELAY) == pdPASS); + portMAX_DELAY); + + CC_UNUSED (status); + CC_ASSERT (status == pdPASS); } void os_timer_stop (os_timer_t * timer) { - CC_ASSERT (xTimerStop (timer->handle, portMAX_DELAY) == pdPASS); + BaseType_t status = xTimerStop (timer->handle, portMAX_DELAY); + CC_UNUSED (status); + CC_ASSERT (status == pdPASS); } void os_timer_destroy (os_timer_t * timer) { - CC_ASSERT (xTimerDelete (timer->handle, portMAX_DELAY) == pdPASS); + BaseType_t status = xTimerDelete (timer->handle, portMAX_DELAY); + CC_UNUSED (status); + CC_ASSERT (status == pdPASS); free (timer); } diff --git a/src/freertos/sys/osal_cc.h b/src/freertos/sys/osal_cc.h index d7c7f0d..cde7ec2 100644 --- a/src/freertos/sys/osal_cc.h +++ b/src/freertos/sys/osal_cc.h @@ -82,6 +82,7 @@ extern "C" #define CC_ASSERT(exp) assert (exp) #define CC_STATIC_ASSERT(exp) _Static_assert (exp, "") +#define CC_UNUSED(var) (void)(var) #ifdef __cplusplus } diff --git a/src/linux/sys/osal_cc.h b/src/linux/sys/osal_cc.h index 6e4fe15..7103750 100644 --- a/src/linux/sys/osal_cc.h +++ b/src/linux/sys/osal_cc.h @@ -91,6 +91,8 @@ static inline void cc_assert (int exp) CLANG_ANALYZER_NORETURN #define CC_STATIC_ASSERT(exp) _Static_assert(exp, "") #endif +#define CC_UNUSED(var) (void)(var) + #ifdef __cplusplus } #endif diff --git a/src/rt-kernel/sys/osal_cc.h b/src/rt-kernel/sys/osal_cc.h index a08b18f..2bfc4b7 100644 --- a/src/rt-kernel/sys/osal_cc.h +++ b/src/rt-kernel/sys/osal_cc.h @@ -81,6 +81,7 @@ extern "C" { #define CC_ASSERT(exp) ASSERT (exp) #define CC_STATIC_ASSERT(exp) _Static_assert(exp, "") +#define CC_UNUSED(var) (void)(var) #ifdef __cplusplus }