bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [PATCH 04/14] calloc, malloc: tune a bit


From: Bruno Haible
Subject: Re: [PATCH 04/14] calloc, malloc: tune a bit
Date: Sat, 16 Nov 2024 15:18:22 +0100

Paul Eggert wrote:
> diff --git a/lib/calloc.c b/lib/calloc.c
> index 801b672078..124c1f9958 100644
> --- a/lib/calloc.c
> +++ b/lib/calloc.c
> @@ -34,15 +34,19 @@
>  void *
>  rpl_calloc (size_t n, size_t s)
>  {
> +#if !HAVE_MALLOC_0_NONNULL
>    if (n == 0 || s == 0)
>      n = s = 1;
> +#endif
>  

This patch causes a regression on 32-bit AIX 7.1. Namely, in a gnulib testdir
of calloc-gnu malloc-gnu, I see a test failure:

  FAIL: test-calloc-gnu
  =====================

  ../../gltests/test-calloc-gnu.c:39: assertion 'p != NULL' failed
  FAIL test-calloc-gnu (exit status: 134)


The background: On this platform, the default malloc(), calloc() don't obey
HAVE_MALLOC_0_NONNULL:

$ cat foo.c
#include <stdlib.h>
#include <stdio.h>
int main () {
  printf ("%p\n", malloc (0));
  printf ("%p\n", calloc (0,0));
}
$ xlc foo.c
$ ./a.out 
0
0

But Gnulib defines _LINUX_SOURCE_COMPAT to 1, and in this case the AIX
headers redirect malloc etc:

#if defined(_ALL_SOURCE) && defined(_LINUX_SOURCE_COMPAT)
...
#define malloc __linux_malloc
#define calloc __linux_calloc
#define realloc __linux_realloc
...
#endif  /* _LINUX_SOURCE_COMPAT */

Such that:

$ xlc -D_LINUX_SOURCE_COMPAT=1 foo.c
$ ./a.out 
200005a8
200005b8

As a consequence, Gnulib's configuration defines HAVE_MALLOC_0_NONNULL.

Now, when calloc.c gets compiled, the '#undef calloc' not only undoes
Gnulib's
  #define calloc rpl_calloc
but also the AIX
  #define calloc __linux_calloc
and the code ends up returning a NULL pointer.

This patch fixes it.


2024-11-16  Bruno Haible  <bruno@clisp.org>

        calloc-gnu: Fix bug on 32-bit AIX (regression 2024-11-04).
        * lib/stdlib.in.h (calloc): Consider _GL_USE_STDLIB_ALLOC.
        * lib/calloc.c: Define _GL_USE_STDLIB_ALLOC. Don't undefine calloc.
        * lib/malloc.c: Add comment.
        * lib/realloc.c: Likewise.

diff --git a/lib/calloc.c b/lib/calloc.c
index 124c1f9958..451c2195a7 100644
--- a/lib/calloc.c
+++ b/lib/calloc.c
@@ -17,6 +17,8 @@
 
 /* written by Jim Meyering and Bruno Haible */
 
+/* Ensure that we call the system's calloc() below.  */
+#define _GL_USE_STDLIB_ALLOC 1
 #include <config.h>
 
 /* Specification.  */
@@ -25,9 +27,6 @@
 #include <errno.h>
 #include <stdckdint.h>
 
-/* Call the system's calloc below.  */
-#undef calloc
-
 /* Allocate and zero-fill an NxS-byte block of memory from the heap,
    even if N or S is zero.  */
 
diff --git a/lib/malloc.c b/lib/malloc.c
index fdb5348268..045ff82c1a 100644
--- a/lib/malloc.c
+++ b/lib/malloc.c
@@ -17,6 +17,7 @@
 
 /* written by Jim Meyering and Bruno Haible */
 
+/* Ensure that we call the system's malloc() below.  */
 #define _GL_USE_STDLIB_ALLOC 1
 #include <config.h>
 
diff --git a/lib/realloc.c b/lib/realloc.c
index 31487b5e18..58044745f4 100644
--- a/lib/realloc.c
+++ b/lib/realloc.c
@@ -18,6 +18,7 @@
 
 /* written by Jim Meyering and Bruno Haible */
 
+/* Ensure that we call the system's realloc() below.  */
 #define _GL_USE_STDLIB_ALLOC 1
 #include <config.h>
 
diff --git a/lib/stdlib.in.h b/lib/stdlib.in.h
index b4e2e723fc..a34fe66bfb 100644
--- a/lib/stdlib.in.h
+++ b/lib/stdlib.in.h
@@ -377,7 +377,8 @@ _GL_WARN_ON_USE (atoll, "atoll is unportable - "
 #if @GNULIB_CALLOC_POSIX@
 # if @REPLACE_CALLOC_FOR_CALLOC_POSIX@ \
      || (@GNULIB_CALLOC_GNU@ && @REPLACE_CALLOC_FOR_CALLOC_GNU@)
-#  if !(defined __cplusplus && defined GNULIB_NAMESPACE)
+#  if !((defined __cplusplus && defined GNULIB_NAMESPACE) \
+        || _GL_USE_STDLIB_ALLOC)
 #   undef calloc
 #   define calloc rpl_calloc
 #  endif






reply via email to

[Prev in Thread] Current Thread [Next in Thread]