diff options
author | Paul Eggert <eggert@cs.ucla.edu> | 2021-04-25 23:11:15 -0700 |
---|---|---|
committer | Paul Eggert <eggert@cs.ucla.edu> | 2021-04-25 23:12:25 -0700 |
commit | 354b9691accd00a531358b652689ce7f580fbe54 (patch) | |
tree | d10f9e2bedd2150a7138d9af901e9edf32aac0d6 | |
parent | 974ddbe54c37dd1f8f0f0f11f6f967d6fecd864d (diff) | |
download | gnulib-354b9691accd00a531358b652689ce7f580fbe54.tar.gz |
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.
-rw-r--r-- | ChangeLog | 9 | ||||
-rw-r--r-- | lib/reallocarray.c | 7 | ||||
-rw-r--r-- | modules/reallocarray | 4 |
3 files changed, 15 insertions, 5 deletions
@@ -1,5 +1,14 @@ 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 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 <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); } 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: |