From 7415a0fa4abc1b8e8af37afd5b5408c1d5dfff2e Mon Sep 17 00:00:00 2001 From: Paul Eggert Date: Sat, 17 Apr 2021 18:48:38 -0700 Subject: [PATCH 2/2] 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. --- ChangeLog | 7 +++++++ lib/xmalloc.c | 41 ++++------------------------------------- modules/xalloc | 3 +++ 3 files changed, 14 insertions(+), 37 deletions(-) diff --git a/ChangeLog b/ChangeLog index 825201fe2..03221a292 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,12 @@ 2021-04-17 Paul Eggert + 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 4a6589571..39ce893ad 100644 --- a/lib/xmalloc.c +++ b/lib/xmalloc.c @@ -27,34 +27,13 @@ #include #include -/* 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 5fa386a5d..000933e94 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 -- 2.27.0