I wrote:
> I think the fix should be that Gnulib's 'alignalloc' module reuses the
> configure test from the 'aligned_alloc' module. This will simplify the
> code in lib/alignalloc.h.

Done as follows. Tested with
  CC="gcc -fsanitize=address"
  CC="clang -fsanitize=address"
  CC="clang -fsanitize=scudo"


2026-09-23  Bruno Haible  <[email protected]>

        alignalloc: Don't use aligned_alloc if it is buggy.
        Reported by <[email protected]> in
        <https://lists.gnu.org/archive/html/bug-gnulib/2026-09/msg00097.html>.
        * m4/aligned_alloc.m4 (gl_FUNC_ALIGNED_ALLOC_WORKS): New macro,
        extracted from gl_FUNC_ALIGNED_ALLOC.
        (gl_FUNC_ALIGNED_ALLOC): Use it.
        * m4/alignalloc.m4 (gl_ALIGNALLOC): Invoke gl_FUNC_ALIGNED_ALLOC_WORKS
        and define HAVE_WORKING_ALIGNED_ALLOC.
        * lib/alignalloc.h (ALIGNALLOC_VIA_ALIGNED_ALLOC): Test
        HAVE_WORKING_ALIGNED_ALLOC, instead of testing for an address sanitizer.
        * modules/alignalloc (Files): Add m4/aligned_alloc.m4.

2026-09-23  Bruno Haible  <[email protected]>

        aligned_alloc: Document "clang -fsanitize=scudo" bug.
        Reported by <[email protected]> in
        <https://lists.gnu.org/archive/html/bug-gnulib/2026-09/msg00097.html>.
        * doc/posix-functions/aligned_alloc.texi: Mention the Scudo issue.

2026-09-23  Bruno Haible  <[email protected]>

        tests: Avoid test failures with -fsanitize=address.
        * tests/test-malloc-posix.c (__has_feature): New macro.
        (main): Skip the test if an address sanitizer is in use.
        * tests/test-calloc-posix.c (__has_feature): New macro.
        (main): Skip the test if an address sanitizer is in use.
        * tests/test-reallocarray.c (__has_feature): New macro.
        (main): Skip the test if an address sanitizer is in use.

>From 05e4f87c166bd34eb834de6c71622e1fffea9b99 Mon Sep 17 00:00:00 2001
From: Bruno Haible <[email protected]>
Date: Wed, 23 Sep 2026 23:34:03 +0200
Subject: [PATCH 1/3] tests: Avoid test failures with -fsanitize=address.

* tests/test-malloc-posix.c (__has_feature): New macro.
(main): Skip the test if an address sanitizer is in use.
* tests/test-calloc-posix.c (__has_feature): New macro.
(main): Skip the test if an address sanitizer is in use.
* tests/test-reallocarray.c (__has_feature): New macro.
(main): Skip the test if an address sanitizer is in use.
---
 ChangeLog                 | 10 ++++++++++
 tests/test-calloc-posix.c |  8 +++++++-
 tests/test-malloc-posix.c |  8 +++++++-
 tests/test-reallocarray.c |  8 +++++++-
 4 files changed, 31 insertions(+), 3 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 6600515ed4..83199b9257 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,13 @@
+2026-09-23  Bruno Haible  <[email protected]>
+
+	tests: Avoid test failures with -fsanitize=address.
+	* tests/test-malloc-posix.c (__has_feature): New macro.
+	(main): Skip the test if an address sanitizer is in use.
+	* tests/test-calloc-posix.c (__has_feature): New macro.
+	(main): Skip the test if an address sanitizer is in use.
+	* tests/test-reallocarray.c (__has_feature): New macro.
+	(main): Skip the test if an address sanitizer is in use.
+
 2026-09-20  Bruno Haible  <[email protected]>
 
 	c32tob: Add tests.
diff --git a/tests/test-calloc-posix.c b/tests/test-calloc-posix.c
index c163a60e36..3cf2a253cd 100644
--- a/tests/test-calloc-posix.c
+++ b/tests/test-calloc-posix.c
@@ -25,6 +25,11 @@
 
 #include "macros.h"
 
+/* For determining whether an address sanitizer is in use.  */
+#ifndef __has_feature
+# define __has_feature(a) 0
+#endif
+
 /* Work around clang bug
    <https://github.com/llvm/llvm-project/issues/114772>.  */
 void *(*volatile my_calloc) (size_t, size_t) = calloc;
@@ -34,7 +39,8 @@ void *(*volatile my_calloc) (size_t, size_t) = calloc;
 int
 main ()
 {
-#if defined __FILC__
+#if defined __SANITIZE_ADDRESS__ || __has_feature (address_sanitizer) \
+    || defined __FILC__
   /* Avoid a "filc safety error: attempt to allocate object that is too big" */
   fputs ("Skipping test: large allocations are unsupported\n", stderr);
   return 77;
diff --git a/tests/test-malloc-posix.c b/tests/test-malloc-posix.c
index c923577c69..3cc3e83bc2 100644
--- a/tests/test-malloc-posix.c
+++ b/tests/test-malloc-posix.c
@@ -25,6 +25,11 @@
 
 #include "macros.h"
 
+/* For determining whether an address sanitizer is in use.  */
+#ifndef __has_feature
+# define __has_feature(a) 0
+#endif
+
 /* Work around clang bug
    <https://github.com/llvm/llvm-project/issues/114772>.  */
 void *(*volatile my_malloc) (size_t) = malloc;
@@ -34,7 +39,8 @@ void *(*volatile my_malloc) (size_t) = malloc;
 int
 main ()
 {
-#if defined __FILC__
+#if defined __SANITIZE_ADDRESS__ || __has_feature (address_sanitizer) \
+    || defined __FILC__
   /* Avoid a "filc safety error: attempt to allocate object that is too big" */
   fputs ("Skipping test: large allocations are unsupported\n", stderr);
   return 77;
diff --git a/tests/test-reallocarray.c b/tests/test-reallocarray.c
index 66de88bc7a..9b71c520aa 100644
--- a/tests/test-reallocarray.c
+++ b/tests/test-reallocarray.c
@@ -28,6 +28,11 @@ SIGNATURE_CHECK (reallocarray, void *, (void *, size_t, size_t));
 
 #include "macros.h"
 
+/* For determining whether an address sanitizer is in use.  */
+#ifndef __has_feature
+# define __has_feature(a) 0
+#endif
+
 /* Work around clang bug
    <https://github.com/llvm/llvm-project/issues/114772>.  */
 void *(*volatile my_reallocarray) (void *, size_t, size_t) = reallocarray;
@@ -37,7 +42,8 @@ void *(*volatile my_reallocarray) (void *, size_t, size_t) = reallocarray;
 int
 main ()
 {
-#if defined __FILC__
+#if defined __SANITIZE_ADDRESS__ || __has_feature (address_sanitizer) \
+    || defined __FILC__
   /* Avoid a "filc safety error: attempt to allocate object that is too big" */
   fputs ("Skipping test: large allocations are unsupported\n", stderr);
   return 77;
-- 
2.53.0

>From 9341648bc1abae4d31e0a87d0a5eadd190c9e128 Mon Sep 17 00:00:00 2001
From: Bruno Haible <[email protected]>
Date: Wed, 23 Sep 2026 23:37:26 +0200
Subject: [PATCH 2/3] aligned_alloc: Document "clang -fsanitize=scudo" bug.

Reported by <[email protected]> in
<https://lists.gnu.org/archive/html/bug-gnulib/2026-09/msg00097.html>.

* doc/posix-functions/aligned_alloc.texi: Mention the Scudo issue.
---
 ChangeLog                              | 7 +++++++
 doc/posix-functions/aligned_alloc.texi | 2 +-
 2 files changed, 8 insertions(+), 1 deletion(-)

diff --git a/ChangeLog b/ChangeLog
index 83199b9257..02f3787f01 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,10 @@
+2026-09-23  Bruno Haible  <[email protected]>
+
+	aligned_alloc: Document "clang -fsanitize=scudo" bug.
+	Reported by <[email protected]> in
+	<https://lists.gnu.org/archive/html/bug-gnulib/2026-09/msg00097.html>.
+	* doc/posix-functions/aligned_alloc.texi: Mention the Scudo issue.
+
 2026-09-23  Bruno Haible  <[email protected]>
 
 	tests: Avoid test failures with -fsanitize=address.
diff --git a/doc/posix-functions/aligned_alloc.texi b/doc/posix-functions/aligned_alloc.texi
index cef30e4339..08db2e9d87 100644
--- a/doc/posix-functions/aligned_alloc.texi
+++ b/doc/posix-functions/aligned_alloc.texi
@@ -30,7 +30,7 @@
 @item
 On some platforms, @code{aligned_alloc} crashes if the requested size is
 not a multiple of the alignment:
-AddressSanitizer (gcc 11.2 or clang 13).
+AddressSanitizer (gcc 11.2 or clang 13), clang with Scudo.
 @end itemize
 
 Portability problems not fixed by Gnulib:
-- 
2.53.0

>From 0b416a8a26dbdaea4f413d4fb0c41ef8ea846e4d Mon Sep 17 00:00:00 2001
From: Bruno Haible <[email protected]>
Date: Wed, 23 Sep 2026 23:43:28 +0200
Subject: [PATCH 3/3] alignalloc: Don't use aligned_alloc if it is buggy.

Reported by <[email protected]> in
<https://lists.gnu.org/archive/html/bug-gnulib/2026-09/msg00097.html>.

* m4/aligned_alloc.m4 (gl_FUNC_ALIGNED_ALLOC_WORKS): New macro,
extracted from gl_FUNC_ALIGNED_ALLOC.
(gl_FUNC_ALIGNED_ALLOC): Use it.
* m4/alignalloc.m4 (gl_ALIGNALLOC): Invoke gl_FUNC_ALIGNED_ALLOC_WORKS
and define HAVE_WORKING_ALIGNED_ALLOC.
* lib/alignalloc.h (ALIGNALLOC_VIA_ALIGNED_ALLOC): Test
HAVE_WORKING_ALIGNED_ALLOC, instead of testing for an address sanitizer.
* modules/alignalloc (Files): Add m4/aligned_alloc.m4.
---
 ChangeLog           | 14 ++++++++++++++
 lib/alignalloc.h    | 17 +----------------
 m4/alignalloc.m4    |  9 ++++++++-
 m4/aligned_alloc.m4 | 37 +++++++++++++++++++++++++------------
 modules/alignalloc  |  1 +
 5 files changed, 49 insertions(+), 29 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 02f3787f01..b02d5c3b4b 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,17 @@
+2026-09-23  Bruno Haible  <[email protected]>
+
+	alignalloc: Don't use aligned_alloc if it is buggy.
+	Reported by <[email protected]> in
+	<https://lists.gnu.org/archive/html/bug-gnulib/2026-09/msg00097.html>.
+	* m4/aligned_alloc.m4 (gl_FUNC_ALIGNED_ALLOC_WORKS): New macro,
+	extracted from gl_FUNC_ALIGNED_ALLOC.
+	(gl_FUNC_ALIGNED_ALLOC): Use it.
+	* m4/alignalloc.m4 (gl_ALIGNALLOC): Invoke gl_FUNC_ALIGNED_ALLOC_WORKS
+	and define HAVE_WORKING_ALIGNED_ALLOC.
+	* lib/alignalloc.h (ALIGNALLOC_VIA_ALIGNED_ALLOC): Test
+	HAVE_WORKING_ALIGNED_ALLOC, instead of testing for an address sanitizer.
+	* modules/alignalloc (Files): Add m4/aligned_alloc.m4.
+
 2026-09-23  Bruno Haible  <[email protected]>
 
 	aligned_alloc: Document "clang -fsanitize=scudo" bug.
diff --git a/lib/alignalloc.h b/lib/alignalloc.h
index f09cd26141..5d772187df 100644
--- a/lib/alignalloc.h
+++ b/lib/alignalloc.h
@@ -43,27 +43,12 @@ extern "C" {
 /* Whether aligned_alloc supports any power-of-two alignment,
    returns a nonnull pointer for size-zero allocations,
    and sets errno on failure.  */
-#if 2 < __GLIBC__ + (16 <= __GLIBC_MINOR__)
+#if (2 < __GLIBC__ + (16 <= __GLIBC_MINOR__)) && HAVE_WORKING_ALIGNED_ALLOC
 # define ALIGNALLOC_VIA_ALIGNED_ALLOC 1
 #else
 # define ALIGNALLOC_VIA_ALIGNED_ALLOC 0
 #endif
 
-/* Work around AddressSanitizer bug.
-   https://gcc.gnu.org/PR104262
-   https://lists.llvm.org/pipermail/llvm-commits/Week-of-Mon-20220124/1001910.html
-   */
-#ifdef __SANITIZE_ADDRESS__
-# undef ALIGNALLOC_VIA_ALIGNED_ALLOC
-# define ALIGNALLOC_VIA_ALIGNED_ALLOC 0
-#endif
-#ifdef __has_feature
-# if __has_feature (address_sanitizer)
-#  undef ALIGNALLOC_VIA_ALIGNED_ALLOC
-#  define ALIGNALLOC_VIA_ALIGNED_ALLOC 0
-# endif
-#endif
-
 #if ALIGNALLOC_VIA_ALIGNED_ALLOC || HAVE_POSIX_MEMALIGN
 
 /* Free storage allocated via alignalloc.  Do nothing if PTR is null.  */
diff --git a/m4/alignalloc.m4 b/m4/alignalloc.m4
index 2749fead64..4ad85919d7 100644
--- a/m4/alignalloc.m4
+++ b/m4/alignalloc.m4
@@ -1,5 +1,5 @@
 # alignalloc.m4
-# serial 1
+# serial 2
 dnl Copyright 2022-2026 Free Software Foundation, Inc.
 dnl This file is free software; the Free Software Foundation
 dnl gives unlimited permission to copy and/or distribute it,
@@ -9,5 +9,12 @@
 AC_DEFUN([gl_ALIGNALLOC],
 [
   AC_REQUIRE([AC_USE_SYSTEM_EXTENSIONS])
+  gl_FUNC_ALIGNED_ALLOC_WORKS
+  case "$gl_cv_func_aligned_alloc_works" in
+    *yes)
+      AC_DEFINE([HAVE_WORKING_ALIGNED_ALLOC], [1],
+        [Define to 1 if aligned_alloc exists and works.])
+      ;;
+  esac
   gl_CHECK_FUNCS_ANDROID([posix_memalign], [[#include <stdlib.h>]])
 ])
diff --git a/m4/aligned_alloc.m4 b/m4/aligned_alloc.m4
index a0b82ecc30..03fd9f0de1 100644
--- a/m4/aligned_alloc.m4
+++ b/m4/aligned_alloc.m4
@@ -1,5 +1,5 @@
 # aligned_alloc.m4
-# serial 9
+# serial 10
 dnl Copyright (C) 2020-2026 Free Software Foundation, Inc.
 dnl This file is free software; the Free Software Foundation
 dnl gives unlimited permission to copy and/or distribute it,
@@ -9,6 +9,26 @@
 AC_DEFUN([gl_FUNC_ALIGNED_ALLOC],
 [
   AC_REQUIRE([gl_STDLIB_H_DEFAULTS])
+
+  gl_FUNC_ALIGNED_ALLOC_WORKS
+  if test $ac_cv_func_aligned_alloc = yes; then
+    dnl The system has aligned_alloc.
+    case "$gl_cv_func_aligned_alloc_works" in
+      *yes) ;;
+      *) REPLACE_ALIGNED_ALLOC=1 ;;
+    esac
+  else
+    dnl The system does not have aligned_alloc.
+    HAVE_ALIGNED_ALLOC=0
+    case "$gl_cv_onwards_func_aligned_alloc" in
+      future*) REPLACE_ALIGNED_ALLOC=1 ;;
+    esac
+  fi
+])
+
+dnl Test whether aligned_alloc is defined and works.
+AC_DEFUN_ONCE([gl_FUNC_ALIGNED_ALLOC_WORKS],
+[
   AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
 
   dnl Persuade glibc and OpenBSD <stdlib.h> to declare aligned_alloc().
@@ -18,8 +38,9 @@ AC_DEFUN([gl_FUNC_ALIGNED_ALLOC]
   if test $ac_cv_func_aligned_alloc = yes; then
     dnl On macOS 11.1 and AIX 7.3.1, aligned_alloc returns NULL when the
     dnl alignment argument is smaller than sizeof (void *).
-    dnl On Solaris 11.4, aligned_alloc returns NULL if the size is not a
-    dnl multiple of the alignment.
+    dnl On Solaris 11.4 or with gcc's ASAN or with clang's ASAN or Scudo,
+    dnl aligned_alloc returns NULL if the size is not a multiple of the
+    dnl alignment.
     dnl On macOS 15, AIX 7.3, Solaris 11.4, aligned_alloc with a zero size
     dnl returns NULL.
     AC_CACHE_CHECK([whether aligned_alloc works for small alignments and sizes],
@@ -64,15 +85,7 @@ AC_DEFUN([gl_FUNC_ALIGNED_ALLOC]
           esac
          ])
       ])
-    case "$gl_cv_func_aligned_alloc_works" in
-      *yes) ;;
-      *) REPLACE_ALIGNED_ALLOC=1 ;;
-    esac
   else
-    dnl The system does not have aligned_alloc.
-    HAVE_ALIGNED_ALLOC=0
-    case "$gl_cv_onwards_func_aligned_alloc" in
-      future*) REPLACE_ALIGNED_ALLOC=1 ;;
-    esac
+    gl_cv_func_aligned_alloc_works=no
   fi
 ])
diff --git a/modules/alignalloc b/modules/alignalloc
index fb64717c7f..7ebfda7820 100644
--- a/modules/alignalloc
+++ b/modules/alignalloc
@@ -5,6 +5,7 @@ Files:
 lib/alignalloc.h
 lib/alignalloc.c
 m4/alignalloc.m4
+m4/aligned_alloc.m4
 
 Depends-on:
 alignasof
-- 
2.53.0

Reply via email to