From 3b1a79f0303d5c837081dfdf7e8e4923c4d67cd6 Mon Sep 17 00:00:00 2001 From: alsaibie Date: Tue, 30 Jun 2020 08:31:45 +0300 Subject: [PATCH] added guard to prevent dealloc and realloc on null ptr --- microros_olimex_e407_extensions/Src/allocators.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/microros_olimex_e407_extensions/Src/allocators.c b/microros_olimex_e407_extensions/Src/allocators.c index 9ecf48db..9d41e202 100644 --- a/microros_olimex_e407_extensions/Src/allocators.c +++ b/microros_olimex_e407_extensions/Src/allocators.c @@ -14,8 +14,10 @@ void * __freertos_allocate(size_t size, void * state){ void __freertos_deallocate(void * pointer, void * state){ (void) state; // printf("-- Free %d (prev: %d B)\n",getBlockSize(pointer), xPortGetFreeHeapSize()); - usedMemory -= getBlockSize(pointer); - vPortFree(pointer); + if (NULL != pointer){ + usedMemory -= getBlockSize(pointer); + vPortFree(pointer); + } } void * __freertos_reallocate(void * pointer, size_t size, void * state){ @@ -23,8 +25,12 @@ void * __freertos_reallocate(void * pointer, size_t size, void * state){ // printf("-- Realloc %d -> %d (prev: %d B)\n",getBlockSize(pointer),size, xPortGetFreeHeapSize()); absoluteUsedMemory += size; usedMemory += size; - usedMemory -= getBlockSize(pointer); - return pvPortRealloc(pointer,size); + if (NULL != pointer){ + usedMemory -= getBlockSize(pointer); + return pvPortRealloc(pointer,size); + } else { + return pvPortMalloc(size); + } } void * __freertos_zero_allocate(size_t number_of_elements, size_t size_of_element, void * state){