From 6dde4e63160695cacdbc24eb510d9548d9270fc0 Mon Sep 17 00:00:00 2001 From: Russ Butler Date: Mon, 6 Mar 2017 17:15:51 -0600 Subject: [PATCH] Prevent underflow in heap size calculation If the free memory on a device is small enough then the calculation to determine heap size could underflow to a large value. If this happens then malloc will never return failure and instead will cause a crash on allocation. This patch prevents the underflow so malloc works as expected even with low amounts of free memory. --- rtos/rtx/TARGET_CORTEX_M/RTX_CM_lib.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rtos/rtx/TARGET_CORTEX_M/RTX_CM_lib.h b/rtos/rtx/TARGET_CORTEX_M/RTX_CM_lib.h index 679f850a7f..b96ef812c3 100644 --- a/rtos/rtx/TARGET_CORTEX_M/RTX_CM_lib.h +++ b/rtos/rtx/TARGET_CORTEX_M/RTX_CM_lib.h @@ -478,7 +478,7 @@ void set_stack_heap(void) { mbed_stack_isr_start = ISR_STACK_START; #else /* Interrupt stack - reserve space at the end of the free block */ - mbed_stack_isr_size = ISR_STACK_SIZE; + mbed_stack_isr_size = ISR_STACK_SIZE < free_size ? ISR_STACK_SIZE : free_size; mbed_stack_isr_start = free_start + free_size - mbed_stack_isr_size; free_size -= mbed_stack_isr_size; #endif