Check that memchr does not read past the first occurrence of the byte.
[gnulib.git] / tests / test-strstr.c
1 /*
2  * Copyright (C) 2004, 2007, 2008, 2009 Free Software Foundation
3  * Written by Bruno Haible and Eric Blake
4  *
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.
9  *
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.
14  *
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/>.  */
17
18 #include <config.h>
19
20 #include <string.h>
21
22 #include <signal.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <unistd.h>
26
27 #define ASSERT(expr) \
28   do                                                                         \
29     {                                                                        \
30       if (!(expr))                                                           \
31         {                                                                    \
32           fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
33           fflush (stderr);                                                   \
34           abort ();                                                          \
35         }                                                                    \
36     }                                                                        \
37   while (0)
38
39 int
40 main (int argc, char *argv[])
41 {
42 #if HAVE_DECL_ALARM
43   /* Declare failure if test takes too long, by using default abort
44      caused by SIGALRM.  All known platforms that lack alarm also have
45      a quadratic strstr, and the replacement strstr is known to not
46      take too long.  */
47   signal (SIGALRM, SIG_DFL);
48   alarm (50);
49 #endif
50
51   {
52     const char input[] = "foo";
53     const char *result = strstr (input, "");
54     ASSERT (result == input);
55   }
56
57   {
58     const char input[] = "foo";
59     const char *result = strstr (input, "o");
60     ASSERT (result == input + 1);
61   }
62
63   {
64     /* On some platforms, the memchr() functions reads past the first
65        occurrence of the byte to be searched, leading to an out-of-bounds
66        read access for strstr().
67        See <http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=521737>.
68        This is a bug in memchr(), see the Austin Group's clarification
69        <http://www.opengroup.org/austin/docs/austin_454.txt>.  */
70     const char *fix = "aBaaaaaaaaaaax";
71     char *input = malloc (strlen (fix) + 1);
72     const char *result;
73
74     strcpy (input, fix);
75     result = strstr (input, "B1x");
76     ASSERT (result == NULL);
77     free (input);
78   }
79
80   {
81     const char input[] = "ABC ABCDAB ABCDABCDABDE";
82     const char *result = strstr (input, "ABCDABD");
83     ASSERT (result == input + 15);
84   }
85
86   {
87     const char input[] = "ABC ABCDAB ABCDABCDABDE";
88     const char *result = strstr (input, "ABCDABE");
89     ASSERT (result == NULL);
90   }
91
92   {
93     const char input[] = "ABC ABCDAB ABCDABCDABDE";
94     const char *result = strstr (input, "ABCDABCD");
95     ASSERT (result == input + 11);
96   }
97
98   /* Check that a very long haystack is handled quickly if the needle is
99      short and occurs near the beginning.  */
100   {
101     size_t repeat = 10000;
102     size_t m = 1000000;
103     char *needle =
104       "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
105       "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
106     char *haystack = (char *) malloc (m + 1);
107     if (haystack != NULL)
108       {
109         memset (haystack, 'A', m);
110         haystack[0] = 'B';
111         haystack[m] = '\0';
112
113         for (; repeat > 0; repeat--)
114           {
115             ASSERT (strstr (haystack, needle) == haystack + 1);
116           }
117
118         free (haystack);
119       }
120   }
121
122   /* Check that a very long needle is discarded quickly if the haystack is
123      short.  */
124   {
125     size_t repeat = 10000;
126     size_t m = 1000000;
127     char *haystack =
128       "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
129       "ABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABAB";
130     char *needle = (char *) malloc (m + 1);
131     if (needle != NULL)
132       {
133         memset (needle, 'A', m);
134         needle[m] = '\0';
135
136         for (; repeat > 0; repeat--)
137           {
138             ASSERT (strstr (haystack, needle) == NULL);
139           }
140
141         free (needle);
142       }
143   }
144
145   /* Check that the asymptotic worst-case complexity is not quadratic.  */
146   {
147     size_t m = 1000000;
148     char *haystack = (char *) malloc (2 * m + 2);
149     char *needle = (char *) malloc (m + 2);
150     if (haystack != NULL && needle != NULL)
151       {
152         const char *result;
153
154         memset (haystack, 'A', 2 * m);
155         haystack[2 * m] = 'B';
156         haystack[2 * m + 1] = '\0';
157
158         memset (needle, 'A', m);
159         needle[m] = 'B';
160         needle[m + 1] = '\0';
161
162         result = strstr (haystack, needle);
163         ASSERT (result == haystack + m);
164       }
165     free (needle);
166     free (haystack);
167   }
168
169   /* Sublinear speed is only possible in memmem; strstr must examine
170      every character of haystack to find its length.  */
171
172   return 0;
173 }