2 dnl Copyright (C) 2002-2004, 2009-2020 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>
37 #if HAVE_SYS_MMAN_H && HAVE_MPROTECT
38 # if HAVE_MAP_ANONYMOUS
39 const int flags = MAP_ANONYMOUS | MAP_PRIVATE;
41 # else /* !HAVE_MAP_ANONYMOUS */
42 const int flags = MAP_FILE | MAP_PRIVATE;
43 int fd = open ("/dev/zero", O_RDONLY, 0666);
47 int pagesize = getpagesize ();
49 (char *) mmap (NULL, 2 * pagesize, PROT_READ | PROT_WRITE,
51 if (two_pages != (char *)(-1)
52 && mprotect (two_pages + pagesize, pagesize, PROT_NONE) == 0)
53 fence = two_pages + pagesize;
72 if (strncat (dest + 1, fence - 3, 3) != dest + 1)
77 || dest[2] != '7' || dest[3] != '2' || dest[4] != '9'
84 ]])], [gl_cv_func_strncat_works=yes], [gl_cv_func_strncat_works=no],
87 # Guess no on Solaris.
88 solaris*) gl_cv_func_strncat_works="guessing no" ;;
89 # Guess yes on native Windows.
90 mingw*) gl_cv_func_strncat_works="guessing yes" ;;
91 # Guess yes otherwise.
92 *) gl_cv_func_strncat_works="guessing yes" ;;
96 case "$gl_cv_func_strncat_works" in
98 *) REPLACE_STRNCAT=1 ;;
102 # Prerequisites of lib/strncat.c.
103 AC_DEFUN([gl_PREREQ_STRNCAT], [