]> git.ipfire.org Git - thirdparty/gnulib.git/commitdiff
xalloc: pacify -Wanalyzer-possible-null-argument
authorPaul Eggert <eggert@cs.ucla.edu>
Tue, 12 May 2020 16:24:05 +0000 (09:24 -0700)
committerPaul Eggert <eggert@cs.ucla.edu>
Tue, 12 May 2020 16:26:42 +0000 (09:26 -0700)
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.

ChangeLog
lib/xmalloc.c

index 58a7a67dcb09920b79ce8c0cad740dc301890992..ee83e22ba572426f28aeb6462df4eca0e5b9c071 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,12 @@
+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
index 486873602e4eda626b6303de665fe2485fb173f9..69c4e7dfae9e41f15b4b0bc7221ec02eb5d18ede 100644 (file)
 #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.  */
 
@@ -39,7 +51,7 @@ void *
 xmalloc (size_t n)
 {
   void *p = malloc (n);
-  if (!p && n != 0)
+  if (!p && (HAVE_GNU_MALLOC || n))
     xalloc_die ();
   return p;
 }
@@ -50,18 +62,17 @@ xmalloc (size_t n)
 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,