From 944bec5ef1471ca52a640cd681a45abcdb46a2a3 Mon Sep 17 00:00:00 2001 From: Paul Eggert Date: Mon, 15 Jul 2024 14:14:55 -0700 Subject: [PATCH] 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. --- ChangeLog | 11 +++++++++++ m4/strnlen.m4 | 51 ++++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 61 insertions(+), 1 deletion(-) diff --git a/ChangeLog b/ChangeLog index 05b091fd8e..56c771612c 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,14 @@ +2024-07-15 Paul Eggert + + 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 manywarnings: Don't enable -Wsystem-headers. diff --git a/m4/strnlen.m4 b/m4/strnlen.m4 index 3eac8e629d..269a125a9c 100644 --- a/m4/strnlen.m4 +++ b/m4/strnlen.m4 @@ -1,10 +1,59 @@ -# strnlen.m4 serial 14 +# strnlen.m4 serial 14.1 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 + #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]) -- 2.39.5