Debian Codegraph @

m4/memchr.m4

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