]> Savannah Git Hosting - gnulib.git/commitdiff
strnlen: port to Android 5.0 (API 21)
authorPaul Eggert <eggert@cs.ucla.edu>
Mon, 15 Jul 2024 21:14:55 +0000 (14:14 -0700)
committerBruno Haible <bruno@clisp.org>
Sun, 21 Jul 2024 01:18:06 +0000 (03:18 +0200)
This is needed for GNU Emacs, which attempts to port to these
old Android versions.
* m4/strnlen.m4 (AC_FUNC_STRNLEN): Replace if Autoconf 2.72 or
earlier, with code that detects the Android problem with strnlen.
This version works around some further bugs in the test, notably,
misplaced 'volatile' and need for volatile in the AIX 4.3 bug
check too.

ChangeLog
doc/posix-functions/strnlen.texi
m4/strnlen.m4

index e4616bef4a4c88fbbb611f0432ec9623087d5690..fe57fda441f13394d7557c8476f905733600e449 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,14 @@
+2024-07-15  Paul Eggert  <eggert@cs.ucla.edu>
+
+       strnlen: port to Android 5.0 (API 21)
+       This is needed for GNU Emacs, which attempts to port to these
+       old Android versions.
+       * m4/strnlen.m4 (AC_FUNC_STRNLEN): Replace if Autoconf 2.72 or
+       earlier, with code that detects the Android problem with strnlen.
+       This version works around some further bugs in the test, notably,
+       misplaced 'volatile' and need for volatile in the AIX 4.3 bug
+       check too.
+
 2024-07-15  Bruno Haible  <bruno@clisp.org>
 
        manywarnings: Don't enable -Wsystem-headers.
index e305a41c8c22c64af06ad0c1a1f441d664d362b0..554ac12213c67241a11df35d3b0530d5b3f87fb2 100644 (file)
@@ -9,15 +9,15 @@ Gnulib module: strnlen
 Portability problems fixed by Gnulib:
 @itemize
 @item
+On some platforms, calls like @code{strnlen (s, maxlen)} can crash if
+@var{s} is null-terminated but address arithmetic overflows
+(i.e., @code{s + maxlen < s}):
+Android 5.0.
+@item
 This function is missing on some platforms:
 Mac OS X 10.5, FreeBSD 6.0, NetBSD 5.0, OpenBSD 3.8, HP-UX 11, Solaris 10, mingw.
 @end itemize
 
 Portability problems not fixed by Gnulib:
 @itemize
-@item
-On some platforms, calls like @code{strnlen (s, maxlen)} can crash if
-@var{s} is null-terminated but address arithmetic overflows
-(i.e., @code{s + maxlen < s}):
-Android 5.0.
 @end itemize
index b4d2778524e4578a703e3c8a564a211c5a760e2b..83a75c0c3277fa02ab044e97cfc5aa43ff1561a9 100644 (file)
@@ -1,11 +1,60 @@
 # strnlen.m4
-# serial 14
+# serial 15
 dnl Copyright (C) 2002-2003, 2005-2007, 2009-2024 Free Software Foundation,
 dnl Inc.
 dnl This file is free software; the Free Software Foundation
 dnl gives unlimited permission to copy and/or distribute it,
 dnl with or without modifications, as long as this notice is preserved.
 
+m4_version_prereq([2.73], [], [
+# Replace AC_FUNC_STRNLEN from Autoconf 2.72 and earlier,
+# which does not check for Android strnlen bugs.
+
+AC_DEFUN([AC_FUNC_STRNLEN],
+[AC_REQUIRE([AC_USE_SYSTEM_EXTENSIONS])dnl
+AC_CACHE_CHECK([for working strnlen], [ac_cv_func_strnlen_working],
+[AC_RUN_IFELSE(
+   [AC_LANG_PROGRAM(
+      [AC_INCLUDES_DEFAULT
+       [/* Use pstrnlen to test; 'volatile' prevents the compiler
+           from optimizing the strnlen calls away.  */
+        size_t (*volatile pstrnlen) (char const *, size_t) = strnlen;
+        char const s[] = "foobar";
+        int s_len = sizeof s - 1;
+       ]],
+      [[
+        /* AIX 4.3 is buggy: strnlen (S, 1) == 3.  */
+        int i;
+        for (i = 0; i < s_len + 1; ++i)
+          {
+            int expected = i <= s_len ? i : s_len;
+            if (pstrnlen (s, i) != expected)
+              return 1;
+          }
+
+        /* Android 5.0 (API 21) strnlen ("", SIZE_MAX) incorrectly crashes.  */
+        if (pstrnlen ("", -1) != 0)
+          return 1;]])],
+   [ac_cv_func_strnlen_working=yes],
+   [ac_cv_func_strnlen_working=no],
+   [AC_COMPILE_IFELSE(
+      [AC_LANG_PROGRAM([AC_INCLUDES_DEFAULT],
+         [[#if defined _AIX && !defined _AIX51
+            #error "AIX pre 5.1 is buggy"
+           #endif
+           #ifdef __ANDROID__
+            #include <android/api-level.h>
+            #if __ANDROID_API__ < 22
+             #error "Android API < 22 is buggy"
+            #endif
+           #endif
+         ]])],
+      [ac_cv_func_strnlen_working=yes],
+      [ac_cv_func_strnlen_working=no])])])
+test $ac_cv_func_strnlen_working = no && AC_LIBOBJ([strnlen])
+])# AC_FUNC_STRNLEN
+])
+
 AC_DEFUN([gl_FUNC_STRNLEN],
 [
   AC_REQUIRE([gl_STRING_H_DEFAULTS])