strnlen: port to Android 5.0 (API 21)
[gnulib.git] / m4 / strnlen.m4
blob83a75c0c3277fa02ab044e97cfc5aa43ff1561a9
1 # strnlen.m4
2 # serial 15
3 dnl Copyright (C) 2002-2003, 2005-2007, 2009-2024 Free Software Foundation,
4 dnl Inc.
5 dnl This file is free software; the Free Software Foundation
6 dnl gives unlimited permission to copy and/or distribute it,
7 dnl with or without modifications, as long as this notice is preserved.
9 m4_version_prereq([2.73], [], [
10 # Replace AC_FUNC_STRNLEN from Autoconf 2.72 and earlier,
11 # which does not check for Android strnlen bugs.
13 AC_DEFUN([AC_FUNC_STRNLEN],
14 [AC_REQUIRE([AC_USE_SYSTEM_EXTENSIONS])dnl
15 AC_CACHE_CHECK([for working strnlen], [ac_cv_func_strnlen_working],
16 [AC_RUN_IFELSE(
17    [AC_LANG_PROGRAM(
18       [AC_INCLUDES_DEFAULT
19        [/* Use pstrnlen to test; 'volatile' prevents the compiler
20            from optimizing the strnlen calls away.  */
21         size_t (*volatile pstrnlen) (char const *, size_t) = strnlen;
22         char const s[] = "foobar";
23         int s_len = sizeof s - 1;
24        ]],
25       [[
26         /* AIX 4.3 is buggy: strnlen (S, 1) == 3.  */
27         int i;
28         for (i = 0; i < s_len + 1; ++i)
29           {
30             int expected = i <= s_len ? i : s_len;
31             if (pstrnlen (s, i) != expected)
32               return 1;
33           }
35         /* Android 5.0 (API 21) strnlen ("", SIZE_MAX) incorrectly crashes.  */
36         if (pstrnlen ("", -1) != 0)
37           return 1;]])],
38    [ac_cv_func_strnlen_working=yes],
39    [ac_cv_func_strnlen_working=no],
40    [AC_COMPILE_IFELSE(
41       [AC_LANG_PROGRAM([AC_INCLUDES_DEFAULT],
42          [[#if defined _AIX && !defined _AIX51
43             #error "AIX pre 5.1 is buggy"
44            #endif
45            #ifdef __ANDROID__
46             #include <android/api-level.h>
47             #if __ANDROID_API__ < 22
48              #error "Android API < 22 is buggy"
49             #endif
50            #endif
51          ]])],
52       [ac_cv_func_strnlen_working=yes],
53       [ac_cv_func_strnlen_working=no])])])
54 test $ac_cv_func_strnlen_working = no && AC_LIBOBJ([strnlen])
55 ])# AC_FUNC_STRNLEN
58 AC_DEFUN([gl_FUNC_STRNLEN],
60   AC_REQUIRE([gl_STRING_H_DEFAULTS])
62   dnl Persuade glibc <string.h> to declare strnlen().
63   AC_REQUIRE([AC_USE_SYSTEM_EXTENSIONS])
65   AC_CHECK_DECLS_ONCE([strnlen])
66   if test $ac_cv_have_decl_strnlen = no; then
67     HAVE_DECL_STRNLEN=0
68   else
69     m4_pushdef([AC_LIBOBJ], [:])
70     dnl Note: AC_FUNC_STRNLEN does AC_LIBOBJ([strnlen]).
71     AC_FUNC_STRNLEN
72     m4_popdef([AC_LIBOBJ])
73     if test $ac_cv_func_strnlen_working = no; then
74       REPLACE_STRNLEN=1
75     fi
76   fi
79 # Prerequisites of lib/strnlen.c.
80 AC_DEFUN([gl_PREREQ_STRNLEN], [:])