From bf799ba1fb48d9706aa7aa6138604a07577d8a61 Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Tue, 5 Nov 2024 21:10:20 +0100 Subject: [PATCH] strndup: Guarantee N3322 functionality. * m4/strndup.m4 (gl_FUNC_STRNDUP): Check against the AIX bug. * doc/posix-functions/strndup.texi: Mention the AIX bug. --- ChangeLog | 6 ++++++ doc/posix-functions/strndup.texi | 4 ++++ m4/strndup.m4 | 32 +++++++++++++++++--------------- 3 files changed, 27 insertions(+), 15 deletions(-) diff --git a/ChangeLog b/ChangeLog index d0514e69b0..06ccb74f9a 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +2024-11-05 Bruno Haible + + strndup: Guarantee N3322 functionality. + * m4/strndup.m4 (gl_FUNC_STRNDUP): Check against the AIX bug. + * doc/posix-functions/strndup.texi: Mention the AIX bug. + 2024-11-05 Bruno Haible strncpy tests: Verify N3322 functionality. diff --git a/doc/posix-functions/strndup.texi b/doc/posix-functions/strndup.texi index 556e0602f1..4baabe2f5b 100644 --- a/doc/posix-functions/strndup.texi +++ b/doc/posix-functions/strndup.texi @@ -15,6 +15,10 @@ Mac OS X 10.5, FreeBSD 6.0, NetBSD 3.0, OpenBSD 3.8, Minix 3.1.8, HP-UX 11, Sola @item This function does not NUL-terminate the result on some platforms: AIX 5.1. +@item +This function does not support zero-length operations on NULL pointers +on some platforms: +AIX 7.3. @end itemize Portability problems not fixed by Gnulib: diff --git a/m4/strndup.m4 b/m4/strndup.m4 index 7e5aa08698..9c8b212fdd 100644 --- a/m4/strndup.m4 +++ b/m4/strndup.m4 @@ -1,5 +1,5 @@ # strndup.m4 -# serial 23 +# serial 24 dnl Copyright (C) 2002-2003, 2005-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, @@ -21,7 +21,8 @@ AC_DEFUN([gl_FUNC_STRNDUP], if test $ac_cv_func_strndup = yes; then HAVE_STRNDUP=1 - # AIX 4.3.3, AIX 5.1 have a function that fails to add the terminating '\0'. + dnl AIX 5.1 has a function that fails to add the terminating '\0'. + dnl AIX 7.3 has a function that does not support a zero length. AC_CACHE_CHECK([for working strndup], [gl_cv_func_strndup_works], [AC_RUN_IFELSE([ AC_LANG_PROGRAM([[#include @@ -33,23 +34,24 @@ AC_DEFUN([gl_FUNC_STRNDUP], #endif char *strndup (const char *, size_t); #endif - int result; - char *s; - s = strndup ("some longer string", 15); - free (s); - s = strndup ("shorter string", 13); - result = s[13] != '\0'; - free (s); + int result = 0; + { + char *s = strndup ("some longer string", 15); + free (s); + s = strndup ("shorter string", 13); + if (s[13] != '\0') + result |= 1; + free (s); + } + if (strndup (NULL, 0) == NULL) + result |= 2; return result;]])], [gl_cv_func_strndup_works=yes], [gl_cv_func_strndup_works=no], - [ -changequote(,)dnl - case $host_os in - aix | aix[3-6]*) gl_cv_func_strndup_works="guessing no";; - *) gl_cv_func_strndup_works="guessing yes";; + [case $host_os in + aix*) gl_cv_func_strndup_works="guessing no";; + *) gl_cv_func_strndup_works="guessing yes";; esac -changequote([,])dnl ])]) case $gl_cv_func_strndup_works in *no) REPLACE_STRNDUP=1 ;; -- 2.39.5