[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
- [PATCH 01/14] calloc: depend on stdckdint not xalloc-oversized, Paul Eggert, 2024/11/05
- [PATCH 02/14] malloc-gnu: depend on stdckdint not xalloc-oversized, Paul Eggert, 2024/11/05
- [PATCH 03/14] calloc: configure more like malloc, Paul Eggert, 2024/11/05
- [PATCH 04/14] calloc, malloc: tune a bit, Paul Eggert, 2024/11/05
- [PATCH 05/14] realloc: don’t require success for nongrowth, Paul Eggert, 2024/11/05
- [PATCH 06/14] stdlib: simplify preprocessor conditionals, Paul Eggert, 2024/11/05
- [PATCH 07/14] realloc-posix: realloc (..., 0) now returns nonnull, Paul Eggert, 2024/11/05
- [PATCH 08/14] realloc-posix: set CHERI bounds, Paul Eggert, 2024/11/05
- [PATCH 09/14] stdlib: make MB_CUR_MAX usable from extern inline, Paul Eggert, 2024/11/05
- [PATCH 10/14] realloc-posix: use _GL_USE_STDLIB_ALLOC, Paul Eggert, 2024/11/05