summaryrefslogtreecommitdiffstats
path: root/pw/reallocarray.c
blob: 73101333c30cdca1339a84ccff8d565ac15c9151 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
#include "reallocarray.h"

#define MUL_NO_OVERFLOW ((size_t)1 << (sizeof(size_t) * 4))
void *reallocarray(void *optr, size_t nmemb, size_t size) {
    if ((nmemb >= MUL_NO_OVERFLOW || size >= MUL_NO_OVERFLOW) &&
        nmemb > 0 && SIZE_MAX / nmemb < size) {
        errno = ENOMEM;
        return NULL;
    }
    return realloc(optr, size * nmemb);
}