2 dnl Copyright (C) 2002-2004, 2009-2018 Free Software Foundation, Inc.
3 dnl This file is free software; the Free Software Foundation
4 dnl gives unlimited permission to copy and/or distribute it,
5 dnl with or without modifications, as long as this notice is preserved.
7 AC_DEFUN_ONCE([gl_FUNC_STRNCAT],
9 AC_REQUIRE([gl_HEADER_STRING_H_DEFAULTS])
10 AC_REQUIRE([AC_PROG_CC])
11 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
13 dnl Check for prerequisites for memory fence checks.
15 AC_CHECK_HEADERS_ONCE([sys/mman.h])
16 AC_CHECK_FUNCS_ONCE([mprotect])
18 dnl Detect bug in Solaris 8..10 on SPARC and Solaris 11.0 on x86:
19 dnl strncat should not dereference more than n bytes, but always dereferences
20 dnl n+1 bytes if the first n bytes don't contain a NUL byte.
21 dnl Assume that strncat works on platforms that lack mprotect.
22 AC_CACHE_CHECK([whether strncat works], [gl_cv_func_strncat_works],
23 [AC_RUN_IFELSE([AC_LANG_PROGRAM([[
28 # include <sys/types.h>
29 # include <sys/mman.h>
36 #if HAVE_SYS_MMAN_H && HAVE_MPROTECT
37 # if HAVE_MAP_ANONYMOUS
38 const int flags = MAP_ANONYMOUS | MAP_PRIVATE;
40 # else /* !HAVE_MAP_ANONYMOUS */
41 const int flags = MAP_FILE | MAP_PRIVATE;
42 int fd = open ("/dev/zero", O_RDONLY, 0666);
46 int pagesize = getpagesize ();
48 (char *) mmap (NULL, 2 * pagesize, PROT_READ | PROT_WRITE,
50 if (two_pages != (char *)(-1)
51 && mprotect (two_pages + pagesize, pagesize, PROT_NONE) == 0)
52 fence = two_pages + pagesize;
71 if (strncat (dest + 1, fence - 3, 3) != dest + 1)
76 || dest[2] != '7' || dest[3] != '2' || dest[4] != '9'
83 ]])], [gl_cv_func_strncat_works=yes], [gl_cv_func_strncat_works=no],
86 # Guess no on Solaris.
87 solaris*) gl_cv_func_strncat_works="guessing no" ;;
88 # Guess yes on native Windows.
89 mingw*) gl_cv_func_strncat_works="guessing yes" ;;
90 # Guess yes otherwise.
91 *) gl_cv_func_strncat_works="guessing yes" ;;
95 case "$gl_cv_func_strncat_works" in
97 *) REPLACE_STRNCAT=1 ;;
101 # Prerequisites of lib/strncat.c.
102 AC_DEFUN([gl_PREREQ_STRNCAT], [