diff options
author | Paul Eggert <eggert@cs.ucla.edu> | 2021-04-17 18:48:38 -0700 |
---|---|---|
committer | Paul Eggert <eggert@cs.ucla.edu> | 2021-04-17 18:50:14 -0700 |
commit | 77735ddeee703ff917dbbd90d06c740a2a6466d7 (patch) | |
tree | 2db8679ede447a24419d74f4e19fa0a9422da3ba | |
parent | 58fe105490e918b2281285f38b460a7b519d2d0c (diff) | |
download | gnulib-77735ddeee703ff917dbbd90d06c740a2a6466d7.tar.gz gnulib-77735ddeee703ff917dbbd90d06c740a2a6466d7.tar.bz2 |
xalloc: adjust to malloc ptrdiff_t change
* lib/xmalloc.c (HAVE_GNU_CALLOC, HAVE_GNU_MALLOC, HAVE_GNU_REALLOC):
Remove.
(xmalloc, xrealloc, xcalloc): Simplify by assuming GNU behavior.
* modules/xalloc (Depends-on): Add calloc-gnu, malloc-gnu,
realloc-gnu.
-rw-r--r-- | ChangeLog | 7 | ||||
-rw-r--r-- | lib/xmalloc.c | 41 | ||||
-rw-r--r-- | modules/xalloc | 3 |
3 files changed, 14 insertions, 37 deletions
@@ -1,5 +1,12 @@ 2021-04-17 Paul Eggert <eggert@cs.ucla.edu> + xalloc: adjust to malloc ptrdiff_t change + * lib/xmalloc.c (HAVE_GNU_CALLOC, HAVE_GNU_MALLOC, HAVE_GNU_REALLOC): + Remove. + (xmalloc, xrealloc, xcalloc): Simplify by assuming GNU behavior. + * modules/xalloc (Depends-on): Add calloc-gnu, malloc-gnu, + realloc-gnu. + malloc, etc.: check for ptrdiff_t overflow In glibc 2.30 and later, malloc, realloc and calloc reject attempts to create objects larger than PTRDIFF_MAX bytes. diff --git a/lib/xmalloc.c b/lib/xmalloc.c index 4a65895716..39ce893ad3 100644 --- a/lib/xmalloc.c +++ b/lib/xmalloc.c @@ -27,34 +27,13 @@ #include <stdlib.h> #include <string.h> -/* 1 if calloc, malloc and realloc are known to be compatible with GNU. - This matters if we are not also using the calloc-gnu, malloc-gnu - and realloc-gnu modules, which define HAVE_CALLOC_GNU, - HAVE_MALLOC_GNU and HAVE_REALLOC_GNU and support the GNU API even - on non-GNU platforms. */ -#if defined HAVE_CALLOC_GNU || (defined __GLIBC__ && !defined __UCLIBC__) -enum { HAVE_GNU_CALLOC = 1 }; -#else -enum { HAVE_GNU_CALLOC = 0 }; -#endif -#if defined HAVE_MALLOC_GNU || (defined __GLIBC__ && !defined __UCLIBC__) -enum { HAVE_GNU_MALLOC = 1 }; -#else -enum { HAVE_GNU_MALLOC = 0 }; -#endif -#if defined HAVE_REALLOC_GNU || (defined __GLIBC__ && !defined __UCLIBC__) -enum { HAVE_GNU_REALLOC = 1 }; -#else -enum { HAVE_GNU_REALLOC = 0 }; -#endif - /* Allocate N bytes of memory dynamically, with error checking. */ void * xmalloc (size_t n) { void *p = malloc (n); - if (!p && (HAVE_GNU_MALLOC || n)) + if (!p) xalloc_die (); return p; } @@ -65,15 +44,8 @@ xmalloc (size_t n) void * xrealloc (void *p, size_t n) { - if (!HAVE_GNU_REALLOC && !n && p) - { - /* The GNU and C99 realloc behaviors disagree here. Act like GNU. */ - free (p); - return NULL; - } - void *r = realloc (p, n); - if (!r && (n || (HAVE_GNU_REALLOC && !p))) + if (!r && (!p || n)) xalloc_die (); return r; } @@ -175,13 +147,8 @@ xzalloc (size_t n) void * xcalloc (size_t n, size_t s) { - void *p; - /* Test for overflow, since objects with size greater than - PTRDIFF_MAX cause pointer subtraction to go awry. Omit size-zero - tests if HAVE_GNU_CALLOC, since GNU calloc never returns NULL if - successful. */ - if (xalloc_oversized (n, s) - || (! (p = calloc (n, s)) && (HAVE_GNU_CALLOC || n != 0))) + void *p = calloc (n, s); + if (!p) xalloc_die (); return p; } diff --git a/modules/xalloc b/modules/xalloc index 5fa386a5d9..000933e946 100644 --- a/modules/xalloc +++ b/modules/xalloc @@ -8,10 +8,13 @@ m4/xalloc.m4 Depends-on: c99 +calloc-gnu extern-inline idx intprops +malloc-gnu minmax +realloc-gnu stdint xalloc-die xalloc-oversized |