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/>. */
22 #include "signature.h"
23 SIGNATURE_CHECK (strstr, char *, (char const *, char const *));
30 #include "zerosize-ptr.h"
32 #define ASSERT(expr) \
37 fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
45 main (int argc, char *argv[])
48 /* Declare failure if test takes too long, by using default abort
49 caused by SIGALRM. All known platforms that lack alarm also have
50 a quadratic strstr, and the replacement strstr is known to not
52 signal (SIGALRM, SIG_DFL);
57 const char input[] = "foo";
58 const char *result = strstr (input, "");
59 ASSERT (result == input);
63 const char input[] = "foo";
64 const char *result = strstr (input, "o");
65 ASSERT (result == input + 1);
69 /* On some platforms, the memchr() functions reads past the first
70 occurrence of the byte to be searched, leading to an out-of-bounds
71 read access for strstr().
72 See <http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=521737>.
73 This is a bug in memchr(), see the Austin Group's clarification
74 <http://www.opengroup.org/austin/docs/austin_454.txt>. */
75 const char *fix = "aBaaaaaaaaaaax";
76 char *page_boundary = (char *) zerosize_ptr ();
77 size_t len = strlen (fix) + 1;
78 char *input = page_boundary ? page_boundary - len : malloc (len);
82 result = strstr (input, "B1x");
83 ASSERT (result == NULL);
89 const char input[] = "ABC ABCDAB ABCDABCDABDE";
90 const char *result = strstr (input, "ABCDABD");
91 ASSERT (result == input + 15);
95 const char input[] = "ABC ABCDAB ABCDABCDABDE";
96 const char *result = strstr (input, "ABCDABE");
97 ASSERT (result == NULL);
101 const char input[] = "ABC ABCDAB ABCDABCDABDE";
102 const char *result = strstr (input, "ABCDABCD");
103 ASSERT (result == input + 11);
106 /* Check that a very long haystack is handled quickly if the needle is
107 short and occurs near the beginning. */
109 size_t repeat = 10000;
112 "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
113 "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
114 char *haystack = (char *) malloc (m + 1);
115 if (haystack != NULL)
117 memset (haystack, 'A', m);
121 for (; repeat > 0; repeat--)
123 ASSERT (strstr (haystack, needle) == haystack + 1);
130 /* Check that a very long needle is discarded quickly if the haystack is
133 size_t repeat = 10000;
136 "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
137 "ABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABAB";
138 char *needle = (char *) malloc (m + 1);
141 memset (needle, 'A', m);
144 for (; repeat > 0; repeat--)
146 ASSERT (strstr (haystack, needle) == NULL);
153 /* Check that the asymptotic worst-case complexity is not quadratic. */
156 char *haystack = (char *) malloc (2 * m + 2);
157 char *needle = (char *) malloc (m + 2);
158 if (haystack != NULL && needle != NULL)
162 memset (haystack, 'A', 2 * m);
163 haystack[2 * m] = 'B';
164 haystack[2 * m + 1] = '\0';
166 memset (needle, 'A', m);
168 needle[m + 1] = '\0';
170 result = strstr (haystack, needle);
171 ASSERT (result == haystack + m);
177 /* Sublinear speed is only possible in memmem; strstr must examine
178 every character of haystack to find its length. */