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 | 11 +++++++++++ doc/posix-functions/aligned_alloc.texi | 4 ++++ lib/aligned_alloc.c | 2 ++ m4/aligned_alloc.m4 | 15 ++++++++++----- tests/test-aligned_alloc.c | 5 ++++- 5 files changed, 31 insertions(+), 6 deletions(-)
diff --git a/ChangeLog b/ChangeLog index 83143af50f..61296ba779 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,14 @@ +2024-10-30 Paul Eggert <egg...@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 <br...@clisp.org> assert-h, stdbool: Fix compilation error with MSVC 14 (regr. yesterday). diff --git a/doc/posix-functions/aligned_alloc.texi b/doc/posix-functions/aligned_alloc.texi index d7830e39ac..b815bd0532 100644 --- a/doc/posix-functions/aligned_alloc.texi +++ b/doc/posix-functions/aligned_alloc.texi @@ -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: diff --git a/lib/aligned_alloc.c b/lib/aligned_alloc.c index 46c44a5b50..947ec1257c 100644 --- a/lib/aligned_alloc.c +++ b/lib/aligned_alloc.c @@ -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 diff --git a/m4/aligned_alloc.m4 b/m4/aligned_alloc.m4 index d7dc2450ca..1311ea3c97 100644 --- a/m4/aligned_alloc.m4 +++ b/m4/aligned_alloc.m4 @@ -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], diff --git a/tests/test-aligned_alloc.c b/tests/test-aligned_alloc.c index caad2c7442..e4aba98221 100644 --- a/tests/test-aligned_alloc.c +++ b/tests/test-aligned_alloc.c @@ -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)]; -- 2.43.0