2021-04-25 Paul Eggert <eggert@cs.ucla.edu>
+ reallocarray: a bit more tuning
+ * lib/reallocarray.c: Include intprops.h.
+ (reallocarray): Use INT_MULTIPLY_WRAPV instead of
+ xalloc_oversized, as it now suffices and is likely a bit more
+ efficient (five fewer machine insns on Ubuntu 20.10 x86-64).
+ * modules/reallocarray (Depends-on): Depend on intprops, not
+ xalloc-oversized. Use conditional dependencies to avoid building
+ realloc-gnu when not needed.
+
reallocarray: don’t crash if item size is 0
This problem affects only platforms where xalloc_oversized
divides a number by the size arg. Fix this by defining
#include <stdlib.h>
#include <errno.h>
-#include "xalloc-oversized.h"
+#include "intprops.h"
void *
reallocarray (void *ptr, size_t nmemb, size_t size)
{
- if (xalloc_oversized (nmemb, size))
+ size_t nbytes;
+ if (INT_MULTIPLY_WRAPV (nmemb, size, &nbytes))
{
errno = ENOMEM;
return NULL;
}
/* Rely on the semantics of GNU realloc. */
- return realloc (ptr, nmemb * size);
+ return realloc (ptr, nbytes);
}