2 * Copyright (C) 2004, 2007-2014 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 int alarm_value = 100;
47 signal (SIGALRM, SIG_DFL);
52 const char input[] = "foo";
53 const char *result = memmem (input, strlen (input), "", 0);
54 ASSERT (result == input);
58 const char input[] = "foo";
59 const char *result = memmem (input, strlen (input), "o", 1);
60 ASSERT (result == input + 1);
64 const char input[] = "ABC ABCDAB ABCDABCDABDE";
65 const char *result = memmem (input, strlen (input), "ABCDABD", 7);
66 ASSERT (result == input + 15);
70 const char input[] = "ABC ABCDAB ABCDABCDABDE";
71 const char *result = memmem (input, strlen (input), "ABCDABE", 7);
72 ASSERT (result == NULL);
76 const char input[] = "ABC ABCDAB ABCDABCDABDE";
77 const char *result = memmem (input, strlen (input), "ABCDABCD", 8);
78 ASSERT (result == input + 11);
81 /* Check that length 0 does not dereference the pointer. */
83 const char *result = memmem (zerosize_ptr (), 0, "foo", 3);
84 ASSERT (result == NULL);
88 const char input[] = "foo";
89 const char *result = memmem (input, strlen (input), null_ptr (), 0);
90 ASSERT (result == input);
93 /* Check that a long periodic needle does not cause false positives. */
95 const char input[] = ("F_BD_CE_BD_EF_BF_BD_EF_BF_BD_EF_BF_BD_EF_BF_BD"
96 "_C3_88_20_EF_BF_BD_EF_BF_BD_EF_BF_BD"
97 "_C3_A7_20_EF_BF_BD");
98 const char need[] = "_EF_BF_BD_EF_BF_BD_EF_BF_BD_EF_BF_BD_EF_BF_BD";
99 const char *result = memmem (input, strlen (input), need, strlen (need));
100 ASSERT (result == NULL);
103 const char input[] = ("F_BD_CE_BD_EF_BF_BD_EF_BF_BD_EF_BF_BD_EF_BF_BD"
104 "_C3_88_20_EF_BF_BD_EF_BF_BD_EF_BF_BD"
105 "_C3_A7_20_EF_BF_BD_DA_B5_C2_A6_20"
106 "_EF_BF_BD_EF_BF_BD_EF_BF_BD_EF_BF_BD_EF_BF_BD");
107 const char need[] = "_EF_BF_BD_EF_BF_BD_EF_BF_BD_EF_BF_BD_EF_BF_BD";
108 const char *result = memmem (input, strlen (input), need, strlen (need));
109 ASSERT (result == input + 115);
112 /* Check that a very long haystack is handled quickly if the needle is
113 short and occurs near the beginning. */
115 size_t repeat = 10000;
118 "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
119 "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
120 size_t n = strlen (needle);
121 char *haystack = (char *) malloc (m + 1);
122 if (haystack != NULL)
124 memset (haystack, 'A', m);
127 for (; repeat > 0; repeat--)
129 ASSERT (memmem (haystack, m, needle, n) == haystack + 1);
136 /* Check that a very long needle is discarded quickly if the haystack is
139 size_t repeat = 10000;
141 const char *haystack =
142 "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
143 "ABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABAB";
144 size_t n = strlen (haystack);
145 char *needle = (char *) malloc (m + 1);
148 memset (needle, 'A', m);
150 for (; repeat > 0; repeat--)
152 ASSERT (memmem (haystack, n, needle, m) == NULL);
159 /* Check that the asymptotic worst-case complexity is not quadratic. */
162 char *haystack = (char *) malloc (2 * m + 1);
163 char *needle = (char *) malloc (m + 1);
164 if (haystack != NULL && needle != NULL)
168 memset (haystack, 'A', 2 * m);
169 haystack[2 * m] = 'B';
171 memset (needle, 'A', m);
174 result = memmem (haystack, 2 * m + 1, needle, m + 1);
175 ASSERT (result == haystack + m);
181 /* Check that long needles not present in a haystack can be handled
182 with sublinear speed. */
184 size_t repeat = 10000;
187 char *haystack = (char *) malloc (m);
188 char *needle = (char *) malloc (n);
189 if (haystack != NULL && needle != NULL)
193 memset (haystack, 'A', m);
194 memset (needle, 'B', n);
196 for (; repeat > 0; repeat--)
198 result = memmem (haystack, m, needle, n);
199 ASSERT (result == NULL);
207 /* Ensure that with a barely periodic "short" needle, memmem's
208 search does not mistakenly skip just past the match point.
209 This use of memmem would mistakenly return NULL before
211 const char *haystack =
213 "with_build_libsubdir\n"
214 "with_local_prefix\n"
215 "with_gxx_include_dir\n"
216 "with_cpp_install_dir\n"
217 "enable_generated_files_in_srcdir\n"
220 "with_demangler_in_ld\n"
224 "enable_werror_always\n"
227 "enable_gather_detailed_mem_stats\n"
228 "enable_build_with_cxx\n"
231 "enable___cxa_atexit\n"
232 "enable_decimal_float\n"
233 "enable_fixed_point\n"
239 "with_build_sysroot\n"
245 "with_multilib_list\n";
246 const char *needle = "\n"
248 const char* p = memmem (haystack, strlen (haystack),
249 needle, strlen (needle));
250 ASSERT (p - haystack == 114);
254 /* Same bug, shorter trigger. */
255 const char *haystack = "..wi.d.";
256 const char *needle = ".d.";
257 const char* p = memmem (haystack, strlen (haystack),
258 needle, strlen (needle));
259 ASSERT (p - haystack == 4);
263 /* Like the above, but trigger the flaw in two_way_long_needle
264 by using a needle of length LONG_NEEDLE_THRESHOLD (32) or greater.
265 Rather than trying to find the right alignment manually, I've
266 arbitrarily chosen the following needle and template for the
267 haystack, and ensure that for each placement of the needle in
268 that haystack, memmem finds it. */
269 const char *needle = "\nwith_gnu_ld-extend-to-len-32-b\n";
272 "with_build_libsubdir\n"
273 "with_local_prefix\n"
274 "with_gxx_include_dir\n"
275 "with_cpp_install_dir\n"
277 "..............................\n"
278 "with_FGHIJKLMNOPQRSTUVWXYZ\n"
279 "with_567890123456789\n"
280 "with_multilib_list\n";
281 size_t h_len = strlen (h);
282 char *haystack = malloc (h_len + 1);
285 for (i = 0; i < h_len - strlen (needle); i++)
288 memcpy (haystack, h, h_len + 1);
289 memcpy (haystack + i, needle, strlen (needle) + 1);
290 p = memmem (haystack, strlen (haystack), needle, strlen (needle));
292 ASSERT (p - haystack == i);