Debian Codegraph @

m4/memchr.m4

raw
1 # memchr.m4
2 # serial 21
3 dnl Copyright (C) 2002-2004, 2009-2026 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/PR10162
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 #endif
37 ]], [[
38 int result = 0;
39 char *fence = NULL;
40 #if HAVE_SYS_MMAN_H && HAVE_MPROTECT
41 {
42 long int pagesize = sysconf (_SC_PAGESIZE);
43 char *two_pages =
44 (char *) mmap (NULL, 2 * pagesize, PROT_READ | PROT_WRITE,
45 MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
46 if (two_pages != (char *)(-1)
47 && mprotect (two_pages + pagesize, pagesize, PROT_NONE) == 0)
48 fence = two_pages + pagesize;
49 }
50 #endif
51 if (fence)
52 {
53 /* Test against bugs on glibc systems. */
54 if (memchr (fence, 0, 0))
55 result |= 1;
56 strcpy (fence - 9, "12345678");
57 if (memchr (fence - 9, 0, 79) != fence - 1)
58 result |= 2;
59 if (memchr (fence - 1, 0, 3) != fence - 1)
60 result |= 4;
61 /* Test against bug on AIX 7.2. */
62 if (memchr (fence - 4, '6', 16) != fence - 4)
63 result |= 8;
64 }
65 /* Test against bug on Android 4.3. */
66 {
67 char input[3];
68 input[0] = 'a';
69 input[1] = 'b';
70 input[2] = 'c';
71 if (memchr (input, 0x789abc00 | 'b', 3) != input + 1)
72 result |= 16;
73 }
74 return result;
75 ]])],
76 [gl_cv_func_memchr_works=yes],
77 [gl_cv_func_memchr_works=no],
78 [case "$host_os" in
79 # Guess no on Android.
80 linux*-android*) gl_cv_func_memchr_works="guessing no" ;;
81 # Guess yes on native Windows.
82 mingw* | windows*) gl_cv_func_memchr_works="guessing yes" ;;
83 # If we don't know, obey --enable-cross-guesses.
84 *) gl_cv_func_memchr_works="$gl_cross_guess_normal" ;;
85 esac
86 ])
87 ])
88 case "$gl_cv_func_memchr_works" in
89 *yes) ;;
90 *) REPLACE_MEMCHR=1 ;;
91 esac
92 ])
93
94 # Prerequisites of lib/memchr.c.
95 AC_DEFUN([gl_PREREQ_MEMCHR], [
96 AC_CHECK_HEADERS([bp-sym.h])
97 ])