From 354b9691accd00a531358b652689ce7f580fbe54 Mon Sep 17 00:00:00 2001 From: Paul Eggert Date: Sun, 25 Apr 2021 23:11:15 -0700 Subject: [PATCH] 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. --- ChangeLog | 9 +++++++++ lib/reallocarray.c | 7 ++++--- modules/reallocarray | 4 ++-- 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/ChangeLog b/ChangeLog index 03b01b3d78..6a449380e6 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,14 @@ 2021-04-25 Paul Eggert + 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 diff --git a/lib/reallocarray.c b/lib/reallocarray.c index 03197c601b..d5cfa05d57 100644 --- a/lib/reallocarray.c +++ b/lib/reallocarray.c @@ -22,17 +22,18 @@ #include #include -#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); } diff --git a/modules/reallocarray b/modules/reallocarray index 6d762c157a..dcf851d5f0 100644 --- a/modules/reallocarray +++ b/modules/reallocarray @@ -8,8 +8,8 @@ m4/reallocarray.m4 Depends-on: extensions -xalloc-oversized -realloc-gnu +intprops [test $HAVE_REALLOCARRAY = 0 || test $REPLACE_REALLOCARRAY = 1] +realloc-gnu [test $HAVE_REALLOCARRAY = 0 || test $REPLACE_REALLOCARRAY = 1] stdlib configure.ac: -- 2.39.5