diff options
Diffstat (limited to 'pw/reallocarray.c')
-rw-r--r-- | pw/reallocarray.c | 11 |
1 files changed, 11 insertions, 0 deletions
diff --git a/pw/reallocarray.c b/pw/reallocarray.c new file mode 100644 index 0000000..7310133 --- /dev/null +++ b/pw/reallocarray.c @@ -0,0 +1,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); +} |