| 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. |
| 8 |
dnl This file is offered as-is, without any warranty. |
| 9 |
|
| 10 |
m4_version_prereq([2.73], [], [ |
| 11 |
# Replace AC_FUNC_STRNLEN from Autoconf 2.72 and earlier, |
| 12 |
# which does not check for Android strnlen bugs. |
| 13 |
|
| 14 |
AC_DEFUN([AC_FUNC_STRNLEN], |
| 15 |
[AC_REQUIRE([AC_USE_SYSTEM_EXTENSIONS])dnl |
| 16 |
AC_CACHE_CHECK([for working strnlen], [ac_cv_func_strnlen_working], |
| 17 |
[AC_RUN_IFELSE( |
| 18 |
[AC_LANG_PROGRAM( |
| 19 |
[AC_INCLUDES_DEFAULT |
| 20 |
[/* Use pstrnlen to test; 'volatile' prevents the compiler |
| 21 |
from optimizing the strnlen calls away. */ |
| 22 |
size_t (*volatile pstrnlen) (char const *, size_t) = strnlen; |
| 23 |
char const s[] = "foobar"; |
| 24 |
int s_len = sizeof s - 1; |
| 25 |
]], |
| 26 |
[[ |
| 27 |
/* AIX 4.3 is buggy: strnlen (S, 1) == 3. */ |
| 28 |
int i; |
| 29 |
for (i = 0; i < s_len + 1; ++i) |
| 30 |
{ |
| 31 |
int expected = i <= s_len ? i : s_len; |
| 32 |
if (pstrnlen (s, i) != expected) |
| 33 |
return 1; |
| 34 |
} |
| 35 |
|
| 36 |
/* Android 5.0 (API 21) strnlen ("", SIZE_MAX) incorrectly crashes. */ |
| 37 |
if (pstrnlen ("", -1) != 0) |
| 38 |
return 1;]])], |
| 39 |
[ac_cv_func_strnlen_working=yes], |
| 40 |
[ac_cv_func_strnlen_working=no], |
| 41 |
[AC_COMPILE_IFELSE( |
| 42 |
[AC_LANG_PROGRAM([AC_INCLUDES_DEFAULT], |
| 43 |
[[#if defined _AIX && !defined _AIX51 |
| 44 |
#error "AIX pre 5.1 is buggy" |
| 45 |
#endif |
| 46 |
#ifdef __ANDROID__ |
| 47 |
#include <android/api-level.h> |
| 48 |
#if __ANDROID_API__ < 22 |
| 49 |
#error "Android API < 22 is buggy" |
| 50 |
#endif |
| 51 |
#endif |
| 52 |
]])], |
| 53 |
[ac_cv_func_strnlen_working=yes], |
| 54 |
[ac_cv_func_strnlen_working=no])])]) |
| 55 |
test $ac_cv_func_strnlen_working = no && AC_LIBOBJ([strnlen]) |
| 56 |
])# AC_FUNC_STRNLEN |
| 57 |
]) |
| 58 |
|
| 59 |
AC_DEFUN([gl_FUNC_STRNLEN], |
| 60 |
[ |
| 61 |
AC_REQUIRE([gl_STRING_H_DEFAULTS]) |
| 62 |
|
| 63 |
dnl Persuade glibc <string.h> to declare strnlen(). |
| 64 |
AC_REQUIRE([AC_USE_SYSTEM_EXTENSIONS]) |
| 65 |
|
| 66 |
AC_CHECK_DECLS_ONCE([strnlen]) |
| 67 |
if test $ac_cv_have_decl_strnlen = no; then |
| 68 |
HAVE_DECL_STRNLEN=0 |
| 69 |
else |
| 70 |
m4_pushdef([AC_LIBOBJ], [:]) |
| 71 |
dnl Note: AC_FUNC_STRNLEN does AC_LIBOBJ([strnlen]). |
| 72 |
AC_FUNC_STRNLEN |
| 73 |
m4_popdef([AC_LIBOBJ]) |
| 74 |
if test $ac_cv_func_strnlen_working = no; then |
| 75 |
REPLACE_STRNLEN=1 |
| 76 |
fi |
| 77 |
fi |
| 78 |
]) |
| 79 |
|
| 80 |
# Prerequisites of lib/strnlen.c. |
| 81 |
AC_DEFUN([gl_PREREQ_STRNLEN], [:]) |