3 dnl Copyright (C) 2008-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.
8 dnl Check that strstr works.
9 AC_DEFUN([gl_FUNC_STRSTR_SIMPLE],
11 AC_REQUIRE([gl_STRING_H_DEFAULTS])
12 AC_REQUIRE([gl_FUNC_MEMCHR])
13 if test $REPLACE_MEMCHR = 1; then
16 dnl Detect https://sourceware.org/bugzilla/show_bug.cgi?id=12092
17 dnl and https://sourceware.org/bugzilla/show_bug.cgi?id=23637.
18 AC_CACHE_CHECK([whether strstr works],
19 [gl_cv_func_strstr_works_always],
22 #include <string.h> /* for __GNU_LIBRARY__, strstr */
23 #ifdef __GNU_LIBRARY__
25 #if __GLIBC__ == 2 && __GLIBC_MINOR__ == 28
30 #define HAYSTACK "F_BD_CE_BD" P P P P "_C3_88_20" P P P "_C3_A7_20" P
31 #define NEEDLE P P P P P
33 [[return !!strstr (HAYSTACK, NEEDLE);
35 [gl_cv_func_strstr_works_always=yes],
36 [gl_cv_func_strstr_works_always=no],
37 [dnl glibc 2.12 and cygwin 1.7.7 have a known bug. uClibc is not
38 dnl affected, since it uses different source code for strstr than
40 dnl Assume that it works on all other platforms, even if it is not
42 AC_EGREP_CPP([Lucky user],
44 #include <string.h> /* for __GNU_LIBRARY__ */
45 #ifdef __GNU_LIBRARY__
47 #if ((__GLIBC__ == 2 && __GLIBC_MINOR__ > 12) || (__GLIBC__ > 2)) \
51 #elif defined __CYGWIN__
52 #include <cygwin/version.h>
53 #if CYGWIN_VERSION_DLL_COMBINED > CYGWIN_VERSION_DLL_MAKE_COMBINED (1007, 7)
60 [gl_cv_func_strstr_works_always="guessing yes"],
61 [gl_cv_func_strstr_works_always="$gl_cross_guess_normal"])
64 case "$gl_cv_func_strstr_works_always" in
71 ]) # gl_FUNC_STRSTR_SIMPLE
73 dnl Additionally, check that strstr is efficient.
74 AC_DEFUN([gl_FUNC_STRSTR],
76 AC_REQUIRE([gl_FUNC_STRSTR_SIMPLE])
77 if test $REPLACE_STRSTR = 0; then
78 AC_CACHE_CHECK([whether strstr works in linear time],
79 [gl_cv_func_strstr_linear],
80 [AC_RUN_IFELSE([AC_LANG_PROGRAM([[
82 /* z/OS does not deliver signals while strstr() is running (thanks to
83 restrictions on its LE runtime), which prevents us from limiting the
84 running time of this test. */
85 # error "This test does not work properly on z/OS"
87 #include <signal.h> /* for signal */
88 #include <string.h> /* for strstr */
89 #include <stdlib.h> /* for malloc */
90 #include <unistd.h> /* for alarm */
91 static void quit (int sig) { _exit (sig + 128); }
95 char *haystack = (char *) malloc (2 * m + 2);
96 char *needle = (char *) malloc (m + 2);
97 /* Failure to compile this test due to missing alarm is okay,
98 since all such platforms (mingw) also have quadratic strstr. */
99 signal (SIGALRM, quit);
101 /* Check for quadratic performance. */
102 if (haystack && needle)
104 memset (haystack, 'A', 2 * m);
105 haystack[2 * m] = 'B';
106 haystack[2 * m + 1] = 0;
107 memset (needle, 'A', m);
110 if (!strstr (haystack, needle))
113 /* Free allocated memory, in case some sanitizer is watching. */
118 [gl_cv_func_strstr_linear=yes], [gl_cv_func_strstr_linear=no],
119 [dnl Only glibc > 2.12 on processors without SSE 4.2 instructions and
120 dnl cygwin > 1.7.7 are known to have a bug-free strstr that works in
122 AC_EGREP_CPP([Lucky user],
124 #include <features.h>
125 #ifdef __GNU_LIBRARY__
126 #if ((__GLIBC__ == 2 && __GLIBC_MINOR__ > 12) || (__GLIBC__ > 2)) \
127 && !(defined __i386__ || defined __x86_64__) \
128 && !defined __UCLIBC__
133 #include <cygwin/version.h>
134 #if CYGWIN_VERSION_DLL_COMBINED > CYGWIN_VERSION_DLL_MAKE_COMBINED (1007, 7)
139 [gl_cv_func_strstr_linear="guessing yes"],
140 [gl_cv_func_strstr_linear="$gl_cross_guess_normal"])
143 case "$gl_cv_func_strstr_linear" in