]> Savannah Git Hosting - gnulib.git/commitdiff
aligned_alloc: check for GNU behavior with size 0
authorPaul Eggert <eggert@cs.ucla.edu>
Wed, 30 Oct 2024 18:54:57 +0000 (11:54 -0700)
committerPaul Eggert <eggert@cs.ucla.edu>
Wed, 30 Oct 2024 19:50:24 +0000 (12:50 -0700)
If someone ever needs to distinguish between GNU and merely POSIX
behavior we can split this into two modules, but for now just
make this module act like GNU.
* lib/aligned_alloc.c (aligned_alloc): Treat zero size like GNU.
* m4/aligned_alloc.m4 (gl_FUNC_ALIGNED_ALLOC):
* tests/test-aligned_alloc.c (main):
Test zero size too.

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

index 83143af50f83092d3c6ac7bbdc8666a23e43768d..61296ba7794bf8fb5ab10f20a3d612489f892f23 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,14 @@
+2024-10-30  Paul Eggert  <eggert@cs.ucla.edu>
+
+       aligned_alloc: check for GNU behavior with size 0
+       If someone ever needs to distinguish between GNU and merely POSIX
+       behavior we can split this into two modules, but for now just
+       make this module act like GNU.
+       * lib/aligned_alloc.c (aligned_alloc): Treat zero size like GNU.
+       * m4/aligned_alloc.m4 (gl_FUNC_ALIGNED_ALLOC):
+       * tests/test-aligned_alloc.c (main):
+       Test zero size too.
+
 2024-10-30  Bruno Haible  <bruno@clisp.org>
 
        assert-h, stdbool: Fix compilation error with MSVC 14 (regr. yesterday).
index d7830e39ac2ed522fa21485445b53a3257b47ba3..b815bd053242de08dee9f3da71a7e6070bd94912 100644 (file)
@@ -17,6 +17,10 @@ Portability problems fixed by Gnulib:
 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 size argument is zero:
+AIX 7.3.
 @end itemize
 
 Portability problems not fixed by Gnulib:
index 46c44a5b50cfcf4cacbdaeadca8b06ea7186f471..947ec1257ce71e9a178826c8ef30d744103ce67f 100644 (file)
@@ -23,6 +23,8 @@ 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);
   else
index d7dc2450cadf44ec5b13d1665efb7faddbee72af..1311ea3c97439aa8c0c27a3b31c0a328eee39225 100644 (file)
@@ -1,5 +1,5 @@
 # aligned_alloc.m4
-# serial 6
+# serial 7
 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,14 +18,19 @@ 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 *).
-    AC_CACHE_CHECK([whether aligned_alloc works for small alignments],
+    dnl On AIX 7.3, aligned_alloc with a zero size returns NULL.
+    AC_CACHE_CHECK([whether aligned_alloc works for small alignments and sizes],
       [gl_cv_func_aligned_alloc_works],
       [AC_RUN_IFELSE(
          [AC_LANG_PROGRAM(
             [[#include <stdlib.h>
-            ]],
-            [[void *volatile p = aligned_alloc (2, 18);
-              return p == NULL;
+              /* Use paligned_alloc to test; 'volatile' prevents the compiler
+                 from optimizing the malloc 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;
             ]])
          ],
          [gl_cv_func_aligned_alloc_works=yes],
index caad2c7442f62ad68ae63a51fb17a6fe3c7ae997..e4aba982211c18c280e5e82138cfb45d6f5785b6 100644 (file)
@@ -35,7 +35,10 @@ main (int argc, char *argv[])
 {
 #if HAVE_ALIGNED_ALLOC
   static size_t sizes[] =
-    { 13, 8, 17, 450, 320, 1, 99, 4, 15, 16, 2, 76, 37, 127, 2406, 641, 5781 };
+    {
+      13, 8, 17, 450, 320, 1, 99, 4, 15, 16,
+      2, 76, 37, 127, 2406, 641, 5781, 0
+    };
   void *volatile aligned2_blocks[SIZEOF (sizes)];
   void *volatile aligned4_blocks[SIZEOF (sizes)];
   void *volatile aligned8_blocks[SIZEOF (sizes)];