2016-11-23 21:16:25 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <unistd.h>
|
2018-06-23 10:35:17 +00:00
|
|
|
#include <string.h>
|
2016-11-23 21:16:25 +00:00
|
|
|
|
2018-06-23 16:08:03 +00:00
|
|
|
#if ACKCONF_WANT_MALLOC
|
|
|
|
|
2016-11-23 21:16:25 +00:00
|
|
|
void* calloc(size_t nmemb, size_t size)
|
|
|
|
{
|
2018-06-21 20:33:47 +00:00
|
|
|
size_t bytes = nmemb * size;
|
|
|
|
void* ptr;
|
|
|
|
|
|
|
|
/* Test for overflow.
|
2016-11-23 21:16:25 +00:00
|
|
|
* See http://stackoverflow.com/questions/1815367/multiplication-of-large-numbers-how-to-catch-overflow
|
|
|
|
*/
|
|
|
|
|
2018-06-21 20:33:47 +00:00
|
|
|
if ((nmemb == 0) || (size == 0) || (nmemb > (SIZE_MAX / size)))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
ptr = malloc(bytes);
|
|
|
|
if (!ptr)
|
|
|
|
return NULL;
|
2016-11-23 21:16:25 +00:00
|
|
|
|
2018-06-21 20:33:47 +00:00
|
|
|
memset(ptr, 0, bytes);
|
|
|
|
return ptr;
|
2016-11-23 21:16:25 +00:00
|
|
|
}
|
2018-06-23 16:08:03 +00:00
|
|
|
|
|
|
|
#endif
|