diff --git a/libraries/AP_HAL_ChibiOS/hwdef/common/malloc.c b/libraries/AP_HAL_ChibiOS/hwdef/common/malloc.c index 167a74646b..b85785d87e 100644 --- a/libraries/AP_HAL_ChibiOS/hwdef/common/malloc.c +++ b/libraries/AP_HAL_ChibiOS/hwdef/common/malloc.c @@ -34,11 +34,21 @@ void *malloc(size_t size) { - return chHeapAllocAligned(NULL, size, MIN_ALIGNMENT); + if (size == 0) { + return NULL; + } + void *p = chHeapAllocAligned(NULL, size, MIN_ALIGNMENT); + if (p) { + memset(p, 0, size); + } + return p; } void *calloc(size_t nmemb, size_t size) { + if (nmemb * size == 0) { + return NULL; + } void *p = chHeapAllocAligned(NULL, nmemb*size, MIN_ALIGNMENT); if (p != NULL) { memset(p, 0, nmemb*size);