1# memchr.m4 serial 13 2dnl Copyright (C) 2002-2004, 2009-2019 Free Software Foundation, Inc. 3dnl This file is free software; the Free Software Foundation 4dnl gives unlimited permission to copy and/or distribute it, 5dnl with or without modifications, as long as this notice is preserved. 6 7AC_DEFUN_ONCE([gl_FUNC_MEMCHR], 8[ 9 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles 10 11 dnl Check for prerequisites for memory fence checks. 12 gl_FUNC_MMAP_ANON 13 AC_CHECK_HEADERS_ONCE([sys/mman.h]) 14 AC_CHECK_FUNCS_ONCE([mprotect]) 15 16 AC_REQUIRE([gl_HEADER_STRING_H_DEFAULTS]) 17 m4_ifdef([gl_FUNC_MEMCHR_OBSOLETE], [ 18 dnl These days, we assume memchr is present. But if support for old 19 dnl platforms is desired: 20 AC_CHECK_FUNCS_ONCE([memchr]) 21 if test $ac_cv_func_memchr = no; then 22 HAVE_MEMCHR=0 23 fi 24 ]) 25 if test $HAVE_MEMCHR = 1; then 26 # Detect platform-specific bugs in some versions of glibc: 27 # memchr should not dereference anything with length 0 28 # https://bugzilla.redhat.com/show_bug.cgi?id=499689 29 # memchr should not dereference overestimated length after a match 30 # https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=521737 31 # https://sourceware.org/bugzilla/show_bug.cgi?id=10162 32 # Assume that memchr works on platforms that lack mprotect. 33 AC_CACHE_CHECK([whether memchr works], [gl_cv_func_memchr_works], 34 [AC_RUN_IFELSE([AC_LANG_PROGRAM([[ 35#include <string.h> 36#if HAVE_SYS_MMAN_H 37# include <fcntl.h> 38# include <unistd.h> 39# include <sys/types.h> 40# include <sys/mman.h> 41# ifndef MAP_FILE 42# define MAP_FILE 0 43# endif 44#endif 45]], [[ 46 int result = 0; 47 char *fence = NULL; 48#if HAVE_SYS_MMAN_H && HAVE_MPROTECT 49# if HAVE_MAP_ANONYMOUS 50 const int flags = MAP_ANONYMOUS | MAP_PRIVATE; 51 const int fd = -1; 52# else /* !HAVE_MAP_ANONYMOUS */ 53 const int flags = MAP_FILE | MAP_PRIVATE; 54 int fd = open ("/dev/zero", O_RDONLY, 0666); 55 if (fd >= 0) 56# endif 57 { 58 int pagesize = getpagesize (); 59 char *two_pages = 60 (char *) mmap (NULL, 2 * pagesize, PROT_READ | PROT_WRITE, 61 flags, fd, 0); 62 if (two_pages != (char *)(-1) 63 && mprotect (two_pages + pagesize, pagesize, PROT_NONE) == 0) 64 fence = two_pages + pagesize; 65 } 66#endif 67 if (fence) 68 { 69 if (memchr (fence, 0, 0)) 70 result |= 1; 71 strcpy (fence - 9, "12345678"); 72 if (memchr (fence - 9, 0, 79) != fence - 1) 73 result |= 2; 74 if (memchr (fence - 1, 0, 3) != fence - 1) 75 result |= 4; 76 } 77 return result; 78]])], 79 [gl_cv_func_memchr_works=yes], 80 [gl_cv_func_memchr_works=no], 81 [case "$host_os" in 82 # Guess yes on native Windows. 83 mingw*) gl_cv_func_memchr_works="guessing yes" ;; 84 # Be pessimistic for now. 85 *) gl_cv_func_memchr_works="guessing no" ;; 86 esac 87 ]) 88 ]) 89 case "$gl_cv_func_memchr_works" in 90 *yes) ;; 91 *) REPLACE_MEMCHR=1 ;; 92 esac 93 fi 94]) 95 96# Prerequisites of lib/memchr.c. 97AC_DEFUN([gl_PREREQ_MEMCHR], [ 98 AC_CHECK_HEADERS([bp-sym.h]) 99]) 100