3 dnl Copyright (C) 2002-2003, 2005-2007, 2009-2024 Free Software Foundation,
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],
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;
26 /* AIX 4.3 is buggy: strnlen (S, 1) == 3. */
28 for (i = 0; i < s_len + 1; ++i)
30 int expected = i <= s_len ? i : s_len;
31 if (pstrnlen (s, i) != expected)
35 /* Android 5.0 (API 21) strnlen ("", SIZE_MAX) incorrectly crashes. */
36 if (pstrnlen ("", -1) != 0)
38 [ac_cv_func_strnlen_working=yes],
39 [ac_cv_func_strnlen_working=no],
41 [AC_LANG_PROGRAM([AC_INCLUDES_DEFAULT],
42 [[#if defined _AIX && !defined _AIX51
43 #error "AIX pre 5.1 is buggy"
46 #include <android/api-level.h>
47 #if __ANDROID_API__ < 22
48 #error "Android API < 22 is buggy"
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])
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
69 m4_pushdef([AC_LIBOBJ], [:])
70 dnl Note: AC_FUNC_STRNLEN does AC_LIBOBJ([strnlen]).
72 m4_popdef([AC_LIBOBJ])
73 if test $ac_cv_func_strnlen_working = no; then
79 # Prerequisites of lib/strnlen.c.
80 AC_DEFUN([gl_PREREQ_STRNLEN], [:])