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>
This commit is contained in:
parent
d87c470017
commit
6ff1378b6f
11
src/cmocka.c
11
src/cmocka.c
|
@ -45,6 +45,8 @@
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
#include <float.h>
|
#include <float.h>
|
||||||
|
#include <errno.h>
|
||||||
|
#include <limits.h>
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* This allows to add a platform specific header file. Some embedded platforms
|
* 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,
|
void* _test_calloc(const size_t number_of_elements, const size_t size,
|
||||||
const char* file, const int line) {
|
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) {
|
if (ptr) {
|
||||||
memset(ptr, 0, number_of_elements * size);
|
memset(ptr, 0, number_of_elements * size);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue