2 * Copyright (C) 2004, 2007-2011 Free Software Foundation, Inc.
3 * Written by Bruno Haible and Eric Blake
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>. */
22 #include "signature.h"
23 SIGNATURE_CHECK (memmem, void *, (void const *, size_t, void const *, size_t));
29 #include "zerosize-ptr.h"
39 main (int argc, char *argv[])
42 /* Declare failure if test takes too long, by using default abort
43 caused by SIGALRM. All known platforms that lack alarm also lack
44 memmem, and the replacement memmem is known to not take too
46 signal (SIGALRM, SIG_DFL);
51 const char input[] = "foo";
52 const char *result = memmem (input, strlen (input), "", 0);
53 ASSERT (result == input);
57 const char input[] = "foo";
58 const char *result = memmem (input, strlen (input), "o", 1);
59 ASSERT (result == input + 1);
63 const char input[] = "ABC ABCDAB ABCDABCDABDE";
64 const char *result = memmem (input, strlen (input), "ABCDABD", 7);
65 ASSERT (result == input + 15);
69 const char input[] = "ABC ABCDAB ABCDABCDABDE";
70 const char *result = memmem (input, strlen (input), "ABCDABE", 7);
71 ASSERT (result == NULL);
75 const char input[] = "ABC ABCDAB ABCDABCDABDE";
76 const char *result = memmem (input, strlen (input), "ABCDABCD", 8);
77 ASSERT (result == input + 11);
80 /* Check that length 0 does not dereference the pointer. */
82 const char *result = memmem (zerosize_ptr (), 0, "foo", 3);
83 ASSERT (result == NULL);
87 const char input[] = "foo";
88 const char *result = memmem (input, strlen (input), null_ptr (), 0);
89 ASSERT (result == input);
92 /* Check that a long periodic needle does not cause false positives. */
94 const char input[] = ("F_BD_CE_BD_EF_BF_BD_EF_BF_BD_EF_BF_BD_EF_BF_BD"
95 "_C3_88_20_EF_BF_BD_EF_BF_BD_EF_BF_BD"
96 "_C3_A7_20_EF_BF_BD");
97 const char need[] = "_EF_BF_BD_EF_BF_BD_EF_BF_BD_EF_BF_BD_EF_BF_BD";
98 const char *result = memmem (input, strlen (input), need, strlen (need));
99 ASSERT (result == NULL);
102 const char input[] = ("F_BD_CE_BD_EF_BF_BD_EF_BF_BD_EF_BF_BD_EF_BF_BD"
103 "_C3_88_20_EF_BF_BD_EF_BF_BD_EF_BF_BD"
104 "_C3_A7_20_EF_BF_BD_DA_B5_C2_A6_20"
105 "_EF_BF_BD_EF_BF_BD_EF_BF_BD_EF_BF_BD_EF_BF_BD");
106 const char need[] = "_EF_BF_BD_EF_BF_BD_EF_BF_BD_EF_BF_BD_EF_BF_BD";
107 const char *result = memmem (input, strlen (input), need, strlen (need));
108 ASSERT (result == input + 115);
111 /* Check that a very long haystack is handled quickly if the needle is
112 short and occurs near the beginning. */
114 size_t repeat = 10000;
117 "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
118 "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
119 size_t n = strlen (needle);
120 char *haystack = (char *) malloc (m + 1);
121 if (haystack != NULL)
123 memset (haystack, 'A', m);
126 for (; repeat > 0; repeat--)
128 ASSERT (memmem (haystack, m, needle, n) == haystack + 1);
135 /* Check that a very long needle is discarded quickly if the haystack is
138 size_t repeat = 10000;
140 const char *haystack =
141 "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
142 "ABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABAB";
143 size_t n = strlen (haystack);
144 char *needle = (char *) malloc (m + 1);
147 memset (needle, 'A', m);
149 for (; repeat > 0; repeat--)
151 ASSERT (memmem (haystack, n, needle, m) == NULL);
158 /* Check that the asymptotic worst-case complexity is not quadratic. */
161 char *haystack = (char *) malloc (2 * m + 1);
162 char *needle = (char *) malloc (m + 1);
163 if (haystack != NULL && needle != NULL)
167 memset (haystack, 'A', 2 * m);
168 haystack[2 * m] = 'B';
170 memset (needle, 'A', m);
173 result = memmem (haystack, 2 * m + 1, needle, m + 1);
174 ASSERT (result == haystack + m);
180 /* Check that long needles not present in a haystack can be handled
181 with sublinear speed. */
183 size_t repeat = 10000;
186 char *haystack = (char *) malloc (m);
187 char *needle = (char *) malloc (n);
188 if (haystack != NULL && needle != NULL)
192 memset (haystack, 'A', m);
193 memset (needle, 'B', n);
195 for (; repeat > 0; repeat--)
197 result = memmem (haystack, m, needle, n);
198 ASSERT (result == NULL);
206 /* Ensure that with a barely periodic "short" needle, memmem's
207 search does not mistakenly skip just past the match point.
208 This use of memmem would mistakenly return NULL before
210 const char *haystack =
212 "with_build_libsubdir\n"
213 "with_local_prefix\n"
214 "with_gxx_include_dir\n"
215 "with_cpp_install_dir\n"
216 "enable_generated_files_in_srcdir\n"
219 "with_demangler_in_ld\n"
223 "enable_werror_always\n"
226 "enable_gather_detailed_mem_stats\n"
227 "enable_build_with_cxx\n"
230 "enable___cxa_atexit\n"
231 "enable_decimal_float\n"
232 "enable_fixed_point\n"
238 "with_build_sysroot\n"
244 "with_multilib_list\n";
245 const char *needle = "\n"
247 const char* p = memmem (haystack, strlen (haystack),
248 needle, strlen (needle));
249 ASSERT (p - haystack == 114);
253 /* Same bug, shorter trigger. */
254 const char *haystack = "..wi.d.";
255 const char *needle = ".d.";
256 const char* p = memmem (haystack, strlen (haystack),
257 needle, strlen (needle));
258 ASSERT (p - haystack == 4);
262 /* Like the above, but trigger the flaw in two_way_long_needle
263 by using a needle of length LONG_NEEDLE_THRESHOLD (32) or greater.
264 Rather than trying to find the right alignment manually, I've
265 arbitrarily chosen the following needle and template for the
266 haystack, and ensure that for each placement of the needle in
267 that haystack, memmem finds it. */
268 const char *needle = "\nwith_gnu_ld-extend-to-len-32-b\n";
271 "with_build_libsubdir\n"
272 "with_local_prefix\n"
273 "with_gxx_include_dir\n"
274 "with_cpp_install_dir\n"
276 "..............................\n"
277 "with_FGHIJKLMNOPQRSTUVWXYZ\n"
278 "with_567890123456789\n"
279 "with_multilib_list\n";
280 size_t h_len = strlen (h);
281 char *haystack = malloc (h_len + 1);
284 for (i = 0; i < h_len - strlen (needle); i++)
287 memcpy (haystack, h, h_len + 1);
288 memcpy (haystack + i, needle, strlen (needle) + 1);
289 p = memmem (haystack, strlen (haystack), needle, strlen (needle));
291 ASSERT (p - haystack == i);