2 * Copyright (C) 2004, 2007, 2008, 2009 Free Software Foundation
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/>. */
27 #include "zerosize-ptr.h"
29 #define ASSERT(expr) \
34 fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
42 main (int argc, char *argv[])
45 /* Declare failure if test takes too long, by using default abort
46 caused by SIGALRM. All known platforms that lack alarm also have
47 a quadratic strstr, and the replacement strstr is known to not
49 signal (SIGALRM, SIG_DFL);
54 const char input[] = "foo";
55 const char *result = strstr (input, "");
56 ASSERT (result == input);
60 const char input[] = "foo";
61 const char *result = strstr (input, "o");
62 ASSERT (result == input + 1);
66 /* On some platforms, the memchr() functions reads past the first
67 occurrence of the byte to be searched, leading to an out-of-bounds
68 read access for strstr().
69 See <http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=521737>.
70 This is a bug in memchr(), see the Austin Group's clarification
71 <http://www.opengroup.org/austin/docs/austin_454.txt>. */
72 const char *fix = "aBaaaaaaaaaaax";
73 char *page_boundary = (char *) zerosize_ptr ();
74 size_t len = strlen (fix) + 1;
75 char *input = page_boundary ? page_boundary - len : malloc (len);
79 result = strstr (input, "B1x");
80 ASSERT (result == NULL);
86 const char input[] = "ABC ABCDAB ABCDABCDABDE";
87 const char *result = strstr (input, "ABCDABD");
88 ASSERT (result == input + 15);
92 const char input[] = "ABC ABCDAB ABCDABCDABDE";
93 const char *result = strstr (input, "ABCDABE");
94 ASSERT (result == NULL);
98 const char input[] = "ABC ABCDAB ABCDABCDABDE";
99 const char *result = strstr (input, "ABCDABCD");
100 ASSERT (result == input + 11);
103 /* Check that a very long haystack is handled quickly if the needle is
104 short and occurs near the beginning. */
106 size_t repeat = 10000;
109 "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
110 "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
111 char *haystack = (char *) malloc (m + 1);
112 if (haystack != NULL)
114 memset (haystack, 'A', m);
118 for (; repeat > 0; repeat--)
120 ASSERT (strstr (haystack, needle) == haystack + 1);
127 /* Check that a very long needle is discarded quickly if the haystack is
130 size_t repeat = 10000;
133 "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
134 "ABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABAB";
135 char *needle = (char *) malloc (m + 1);
138 memset (needle, 'A', m);
141 for (; repeat > 0; repeat--)
143 ASSERT (strstr (haystack, needle) == NULL);
150 /* Check that the asymptotic worst-case complexity is not quadratic. */
153 char *haystack = (char *) malloc (2 * m + 2);
154 char *needle = (char *) malloc (m + 2);
155 if (haystack != NULL && needle != NULL)
159 memset (haystack, 'A', 2 * m);
160 haystack[2 * m] = 'B';
161 haystack[2 * m + 1] = '\0';
163 memset (needle, 'A', m);
165 needle[m + 1] = '\0';
167 result = strstr (haystack, needle);
168 ASSERT (result == haystack + m);
174 /* Sublinear speed is only possible in memmem; strstr must examine
175 every character of haystack to find its length. */