blob: b472d4461ba07cbdc1ab9e316d4e0a69d56e2066 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
#include <errno.h> // for errno, EINVAL, ENOMEM
#include <stdlib.h> // for calloc, malloc
#include <string.h> // for size_t, memset
void *calloc(size_t nelem, size_t elsize)
{
void *ptr;
size_t total;
if (nelem == 0 || elsize == 0) {
errno = EINVAL;
return NULL;
}
if (__builtin_mul_overflow(nelem, elsize, &total)) {
errno = ENOMEM;
return NULL;
}
ptr = malloc(total);
if (ptr != NULL) {
memset(ptr, 0, total);
}
return ptr;
}
|