From 6ff1378b6f4e82af7bb53868f8526cf8e0a0cf98 Mon Sep 17 00:00:00 2001 From: Alexander Dahl Date: Fri, 23 Dec 2022 10:35:49 +0100 Subject: [PATCH] cmocka: Add overflow check for test_calloc() Makes the implementation behave the same like libc calloc() and not fail with unpredictable errors in test_malloc() anymore. Signed-off-by: Alexander Dahl Reviewed-by: Andreas Schneider --- src/cmocka.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/cmocka.c b/src/cmocka.c index 08655a8..211c315 100644 --- a/src/cmocka.c +++ b/src/cmocka.c @@ -45,6 +45,8 @@ #include #include #include +#include +#include /* * This allows to add a platform specific header file. Some embedded platforms @@ -2464,7 +2466,14 @@ void* _test_malloc(const size_t size, const char *file, const int line) { void* _test_calloc(const size_t number_of_elements, const size_t size, const char* file, const int line) { - void* const ptr = _test_malloc(number_of_elements * size, file, line); + void *ptr = NULL; + + if (size > 0 && number_of_elements > SIZE_MAX / size) { + errno = ENOMEM; + return NULL; + } + + ptr = _test_malloc(number_of_elements * size, file, line); if (ptr) { memset(ptr, 0, number_of_elements * size); }