]> Savannah Git Hosting - gnulib.git/commitdiff
aligned_alloc: Fix test failures on macOS, Solaris (regr. 2024-10-30).
authorBruno Haible <bruno@clisp.org>
Mon, 4 Nov 2024 16:50:09 +0000 (17:50 +0100)
committerBruno Haible <bruno@clisp.org>
Mon, 4 Nov 2024 16:50:09 +0000 (17:50 +0100)
* m4/aligned_alloc.m4 (gl_FUNC_ALIGNED_ALLOC): Check for the Solaris
bug. Let the test program return a bit mask. Update cross-compilation
guesses.
* lib/aligned_alloc.c: Include <errno.h>.
(aligned_alloc): Fail if the alignment is not a power of 2. Work around
the Solaris bug.
* modules/aligned_alloc (Depends-on): Add malloc-posix.
* doc/posix-functions/aligned_alloc.texi: Mention the Solaris bug.

ChangeLog
doc/posix-functions/aligned_alloc.texi
lib/aligned_alloc.c
m4/aligned_alloc.m4
modules/aligned_alloc

index 8d0ab77a9721fffabc4d23ecad95a5afd29f677a..727df3dcb0c4d9182ca8d02fb39e37eab3dc4ef5 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,15 @@
+2024-11-04  Bruno Haible  <bruno@clisp.org>
+
+       aligned_alloc: Fix test failures on macOS, Solaris (regr. 2024-10-30).
+       * m4/aligned_alloc.m4 (gl_FUNC_ALIGNED_ALLOC): Check for the Solaris
+       bug. Let the test program return a bit mask. Update cross-compilation
+       guesses.
+       * lib/aligned_alloc.c: Include <errno.h>.
+       (aligned_alloc): Fail if the alignment is not a power of 2. Work around
+       the Solaris bug.
+       * modules/aligned_alloc (Depends-on): Add malloc-posix.
+       * doc/posix-functions/aligned_alloc.texi: Mention the Solaris bug.
+
 2024-11-04  Bruno Haible  <bruno@clisp.org>
 
        tests: Add comments.
index b815bd053242de08dee9f3da71a7e6070bd94912..121c0601c472afd1ee2769b07a2d909696ac8f18 100644 (file)
@@ -18,26 +18,31 @@ This function fails if the alignment argument is smaller than
 @code{sizeof (void *)} on some platforms:
 macOS 14, AIX 7.3.1.
 
+@item
+This function returns a null pointer if the requested size is
+not a multiple of the alignment:
+Solaris 11.4.
+
 @item
 This function returns a null pointer if the size argument is zero:
-AIX 7.3.
-@end itemize
+macOS 15, AIX 7.3, Solaris 11.4.
 
-Portability problems not fixed by Gnulib:
-@itemize
 @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).
+@end itemize
+
+Portability problems not fixed by Gnulib:
+@itemize
+@item
+This function is missing on many older platforms:
+glibc 2.15, macOS 10.14, FreeBSD 6.4, NetBSD 7.1, OpenBSD 6.0, Minix 3.1.8, AIX 7.1, HP-UX 11.31, Solaris 11.3, Cygwin 1.7.x, mingw, MSVC 14, Android 8.1.
 
 @item
 If the alignment and size are absurdly large, this function crashes:
 @c https://sourceware.org/bugzilla/show_bug.cgi?id=32301
 glibc 2.40.
-
-@item
-This function is missing on many older platforms:
-glibc 2.15, macOS 10.14, FreeBSD 6.4, NetBSD 7.1, OpenBSD 6.0, Minix 3.1.8, AIX 7.1, HP-UX 11.31, Solaris 11.3, Cygwin 1.7.x, mingw, MSVC 14, Android 8.1.
 @end itemize
 
 Gnulib has partial substitutes for @code{aligned_alloc}
index 947ec1257ce71e9a178826c8ef30d744103ce67f..a9f43ec0c1e3ab830e9ab8af77572421a292a027 100644 (file)
 /* Specification.  */
 #include <stdlib.h>
 
+#include <errno.h>
+
 void *
 aligned_alloc (size_t alignment, size_t size)
 #undef aligned_alloc
 {
-  if (size == 0)
-    size = 1;
-  if (alignment >= sizeof (void *))
-    return aligned_alloc (alignment, size);
+  if (alignment != 0 && (alignment & (alignment - 1)) == 0)
+    {
+      /* alignment is a power of 2.  */
+      if (size == 0)
+        size = 1;
+      if (alignment >= sizeof (void *))
+        {
+          /* Make size a multiple of alignment.  */
+          size = (size + (alignment - 1)) & -alignment;
+          if (size > 0)
+            return aligned_alloc (alignment, size);
+          else
+            {
+              errno = ENOMEM;
+              return NULL;
+            }
+        }
+      else
+        return malloc (size);
+    }
   else
-    return malloc (size);
+    {
+      errno = EINVAL;
+      return NULL;
+    }
 }
index 2d4a377e19609e14ef62b1dd97c2f0377296499f..0d3e35135ef762b57ba4ea1956ce63072ea744e1 100644 (file)
@@ -1,5 +1,5 @@
 # aligned_alloc.m4
-# serial 8
+# serial 9
 dnl Copyright (C) 2020-2024 Free Software Foundation, Inc.
 dnl This file is free software; the Free Software Foundation
 dnl gives unlimited permission to copy and/or distribute it,
@@ -18,7 +18,10 @@ 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 AIX 7.3, aligned_alloc with a zero size returns NULL.
+    dnl On Solaris 11.4, aligned_alloc returns NULL if the size is not a
+    dnl multiple of the 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],
       [gl_cv_func_aligned_alloc_works],
       [AC_RUN_IFELSE(
@@ -28,20 +31,36 @@ AC_DEFUN([gl_FUNC_ALIGNED_ALLOC],
                  from optimizing the aligned_alloc call away.  */
               void *(*volatile paligned_alloc) (size_t, size_t)
                 = aligned_alloc;]],
-            [[void *p = paligned_alloc (2, 18);
-              void *q = paligned_alloc (sizeof (void *), 0);
-              return p == NULL || q == NULL;
+            [[int result = 0;
+              {
+                void *p = paligned_alloc (2, 18);
+                if (p == NULL)
+                  result |= 1;
+              }
+              {
+                void *p = paligned_alloc (sizeof (void *), sizeof (void *) + 1);
+                if (p == NULL)
+                  result |= 2;
+              }
+              {
+                void *p = paligned_alloc (sizeof (void *), 0);
+                if (p == NULL)
+                  result |= 4;
+              }
+              return result;
             ]])
          ],
          [gl_cv_func_aligned_alloc_works=yes],
          [gl_cv_func_aligned_alloc_works=no],
          [case "$host_os" in
-                     # Guess no on AIX.
-            aix*)    gl_cv_func_aligned_alloc_works="guessing no" ;;
-                     # Guess no on macOS.
-            darwin*) gl_cv_func_aligned_alloc_works="guessing no" ;;
-                     # If we don't know, obey --enable-cross-guesses.
-            *)       gl_cv_func_aligned_alloc_works="$gl_cross_guess_normal" ;;
+                      # Guess no on macOS.
+            darwin*)  gl_cv_func_aligned_alloc_works="guessing no" ;;
+                      # Guess no on AIX.
+            aix*)     gl_cv_func_aligned_alloc_works="guessing no" ;;
+                      # Guess no on Solaris.
+            solaris*) gl_cv_func_aligned_alloc_works="guessing no" ;;
+                      # If we don't know, obey --enable-cross-guesses.
+            *)        gl_cv_func_aligned_alloc_works="$gl_cross_guess_normal" ;;
           esac
          ])
       ])
index e04115ec9e62551d47730cc4ca79c743858f7864..ab7f3ffba0ab6a42378bf91324709f5c705cd29b 100644 (file)
@@ -8,6 +8,7 @@ m4/aligned_alloc.m4
 Depends-on:
 extensions
 stdlib
+malloc-posix    [test $REPLACE_ALIGNED_ALLOC = 1]
 
 configure.ac:
 gl_FUNC_ALIGNED_ALLOC