From 7b6a4da2061f6a6d22665ae86ed619434bfec931 Mon Sep 17 00:00:00 2001 From: Nicolas Pitre Date: Wed, 17 Mar 2021 12:17:08 -0400 Subject: [PATCH] libc/minimal: assorted fixes to malloc() and friends - When malloc() is called with a size of 0 we should not set errno to ENOMEM as there is no actual allocation failure in that case. This duplicates the realloc() behavior. - Put unlock_ret assignments on separate lines, otherwise gcc complains about unused variables when the tests on it are disabled. - There NULL return added in 952970d6cb are completely pointless. First, there is no reason for sys_mutex_unlock() to fail, and even if it did, those returns would be blatent memory leaks. Remove them. No one should blindly modify code just to make static code analysers happy. - Replace all CHECKIF() by explicit assertion statements to uniformize those checks and drop the NULL returns entirely. We can't return anything in the free() case, and there are no runtime conditions for sys_mutex_lock() to sometimes succeed and sometimes fail anyway. Signed-off-by: Nicolas Pitre --- lib/libc/minimal/source/stdlib/malloc.c | 40 +++++++++---------------- 1 file changed, 14 insertions(+), 26 deletions(-) diff --git a/lib/libc/minimal/source/stdlib/malloc.c b/lib/libc/minimal/source/stdlib/malloc.c index fe39c8ab6be..ccb3898038d 100644 --- a/lib/libc/minimal/source/stdlib/malloc.c +++ b/lib/libc/minimal/source/stdlib/malloc.c @@ -11,7 +11,6 @@ #include #include #include -#include #include #include #include @@ -38,24 +37,19 @@ Z_GENERIC_SECTION(POOL_SECTION) static char z_malloc_heap_mem[HEAP_BYTES]; void *malloc(size_t size) { - int lock_ret = sys_mutex_lock(&z_malloc_heap_mutex, K_FOREVER); + int lock_ret; - CHECKIF(lock_ret != 0) { - return NULL; - } + lock_ret = sys_mutex_lock(&z_malloc_heap_mutex, K_FOREVER); + __ASSERT_NO_MSG(lock_ret == 0); void *ret = sys_heap_aligned_alloc(&z_malloc_heap, __alignof__(z_max_align_t), size); - if (ret == NULL) { + if (ret == NULL && size != 0) { errno = ENOMEM; } - int unlock_ret = sys_mutex_unlock(&z_malloc_heap_mutex); - - CHECKIF(unlock_ret != 0) { - return NULL; - } + (void) sys_mutex_unlock(&z_malloc_heap_mutex); return ret; } @@ -72,36 +66,30 @@ static int malloc_prepare(const struct device *unused) void *realloc(void *ptr, size_t requested_size) { - int lock_ret = sys_mutex_lock(&z_malloc_heap_mutex, K_FOREVER); + int lock_ret; - CHECKIF(lock_ret != 0) { - return NULL; - } + lock_ret = sys_mutex_lock(&z_malloc_heap_mutex, K_FOREVER); + __ASSERT_NO_MSG(lock_ret == 0); void *ret = sys_heap_aligned_realloc(&z_malloc_heap, ptr, - __alignof__(z_max_align_t), - requested_size); + __alignof__(z_max_align_t), + requested_size); if (ret == NULL && requested_size != 0) { errno = ENOMEM; } - int unlock_ret = sys_mutex_unlock(&z_malloc_heap_mutex); + (void) sys_mutex_unlock(&z_malloc_heap_mutex); - CHECKIF(unlock_ret != 0) { - return NULL; - } return ret; } void free(void *ptr) { - int lock_ret = sys_mutex_lock(&z_malloc_heap_mutex, K_FOREVER); - - CHECKIF(lock_ret != 0) { - return; - } + int lock_ret; + lock_ret = sys_mutex_lock(&z_malloc_heap_mutex, K_FOREVER); + __ASSERT_NO_MSG(lock_ret == 0); sys_heap_free(&z_malloc_heap, ptr); (void) sys_mutex_unlock(&z_malloc_heap_mutex); }