+2020-05-12 Paul Eggert <eggert@cs.ucla.edu>
+
+ xalloc: pacify -Wanalyzer-possible-null-argument
+ Problem reported for GCC 10.1.0 by Bruno Haible in:
+ https://lists.gnu.org/r/bug-gnulib/2020-05/msg00118.html
+ * lib/xmalloc.c (HAVE_GNU_MALLOC, HAVE_GNU_REALLOC): New constants.
+ (xmalloc): Suppress unnecessary check if HAVE_GNU_MALLOC.
+ (xrealloc): Suppress unnecssary check if HAVE_GNU_REALLOC.
+
2020-05-11 Paul Eggert <eggert@cs.ucla.edu>
careadlinkat: fix GCC 10 workaround
#include <stdlib.h>
#include <string.h>
-/* 1 if calloc is known to be compatible with GNU calloc. This
- matters if we are not also using the calloc module, which defines
- HAVE_CALLOC_GNU and supports the GNU API even on non-GNU platforms. */
+/* 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. */
xmalloc (size_t n)
{
void *p = malloc (n);
- if (!p && n != 0)
+ if (!p && (HAVE_GNU_MALLOC || n))
xalloc_die ();
return p;
}
void *
xrealloc (void *p, size_t n)
{
- if (!n && p)
+ if (!HAVE_GNU_REALLOC && !n && p)
{
- /* The GNU and C99 realloc behaviors disagree here. Act like
- GNU, even if the underlying realloc is C99. */
+ /* The GNU and C99 realloc behaviors disagree here. Act like GNU. */
free (p);
return NULL;
}
- p = realloc (p, n);
- if (!p && n)
+ void *r = realloc (p, n);
+ if (!r && (n || (HAVE_GNU_REALLOC && !p)))
xalloc_die ();
- return p;
+ return r;
}
/* If P is null, allocate a block of at least *PN bytes; otherwise,