diff -urN musl.orig/include/stdlib.h musl/include/stdlib.h --- musl.orig/include/stdlib.h Thu May 8 09:04:08 2014 +++ musl/include/stdlib.h Thu May 8 09:11:06 2014 @@ -44,6 +44,9 @@ void *realloc (void *, size_t); void free (void *); void *aligned_alloc(size_t alignment, size_t size); +#ifdef _BSD_SOURCE +void *reallocarray(void *, size_t, size_t); +#endif _Noreturn void abort (void); int atexit (void (*) (void)); diff -urN musl.orig/src/stdlib/reallocarray.c musl/src/stdlib/reallocarray.c --- musl.orig/src/stdlib/reallocarray.c Thu Jan 1 00:00:00 1970 +++ musl/src/stdlib/reallocarray.c Thu May 8 09:06:30 2014 @@ -0,0 +1,17 @@ +#include +#include +#include + +/* this is sqrt(SIZE_MAX+1), as s1*s2 <= SIZE_MAX + * if both s1 < MUL_NO_OVERFLOW and s2 < MUL_NO_OVERFLOW */ +#define MUL_NO_OVERFLOW (1UL << (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 && SSIZE_MAX / nmemb < size) { + errno = ENOMEM; + return NULL; + } + return realloc(optr, size * nmemb); +}