summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlexander Dahl <ada@thorsis.com>2022-12-23 10:35:49 +0100
committerAndreas Schneider <asn@cryptomilk.org>2024-02-02 11:26:54 +0100
commit53de473cddf20505f24f00d15b245591a0d0e6ad (patch)
treeef8449e3db152eda17c9e2bf7d22faf39fa05a8f
parente738d6eada34361fb8497acd808278410946022f (diff)
downloadcmocka-53de473cddf20505f24f00d15b245591a0d0e6ad.zip
cmocka-53de473cddf20505f24f00d15b245591a0d0e6ad.tar.gz
cmocka-53de473cddf20505f24f00d15b245591a0d0e6ad.tar.bz2
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 <ada@thorsis.com> Reviewed-by: Andreas Schneider <asn@cryptomilk.org> (cherry picked from commit 6ff1378b6f4e82af7bb53868f8526cf8e0a0cf98)
-rw-r--r--src/cmocka.c11
1 files changed, 10 insertions, 1 deletions
diff --git a/src/cmocka.c b/src/cmocka.c
index 52897e1..38b5e42 100644
--- a/src/cmocka.c
+++ b/src/cmocka.c
@@ -45,6 +45,8 @@
#include <stdbool.h>
#include <time.h>
#include <float.h>
+#include <errno.h>
+#include <limits.h>
/*
* This allows to add a platform specific header file. Some embedded platforms
@@ -2095,7 +2097,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);
}