From 1484a12f4bdeaf872a5704c2871222a2feca732c Mon Sep 17 00:00:00 2001 From: Michael du Breuil Date: Mon, 26 Aug 2019 17:25:03 -0700 Subject: [PATCH] AP_HAL_Linux: Fix bad check order on heap_realloc --- libraries/AP_HAL_Linux/Util.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/libraries/AP_HAL_Linux/Util.cpp b/libraries/AP_HAL_Linux/Util.cpp index 155131a8f9..4af8c5b4c0 100644 --- a/libraries/AP_HAL_Linux/Util.cpp +++ b/libraries/AP_HAL_Linux/Util.cpp @@ -257,17 +257,17 @@ void *Util::heap_realloc(void *h, void *ptr, size_t new_size) old_size = old_header->allocation_size; } + if ((heapp->current_heap_usage + new_size - old_size) > heapp->max_heap_size) { + // fail the allocation as we don't have the memory. Note that we don't simulate fragmentation + return nullptr; + } + heapp->current_heap_usage -= old_size; if (new_size == 0) { free(old_header); return nullptr; } - if ((heapp->current_heap_usage + new_size - old_size) > heapp->max_heap_size) { - // fail the allocation as we don't have the memory. Note that we don't simulate fragmentation - return nullptr; - } - heap_allocation_header *new_header = (heap_allocation_header *)malloc(new_size + sizeof(heap_allocation_header)); if (new_header == nullptr) { // total failure to allocate, this is very surprising in SITL