From 2f08f676dcbaf27ff5ce69f59d73f8799b3afee8 Mon Sep 17 00:00:00 2001 From: Marian Buschsieweke Date: Tue, 4 May 2021 14:11:39 +0200 Subject: [PATCH] cpu/esp_common: fix possible overflow in calloc implementation --- cpu/esp_common/syscalls.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/cpu/esp_common/syscalls.c b/cpu/esp_common/syscalls.c index d09f757bad..9bc337dc61 100644 --- a/cpu/esp_common/syscalls.c +++ b/cpu/esp_common/syscalls.c @@ -276,9 +276,13 @@ void* IRAM_ATTR __wrap__realloc_r(struct _reent *r, void* ptr, size_t size) void* IRAM_ATTR __wrap__calloc_r(struct _reent *r, size_t count, size_t size) { - void *result = heap_caps_malloc_default(count * size); + size_t size_total; + if (__builtin_mul_overflow(count, size, &size_total)) { + return NULL; + } + void *result = heap_caps_malloc_default(size_total); if (result) { - bzero(result, count * size); + bzero(result, size_total); } return result; }