ignore-value: handle pointer types, too
[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 #include "zerosize-ptr.h"
28
29 #define ASSERT(expr) \
30   do                                                                         \
31     {                                                                        \
32       if (!(expr))                                                           \
33         {                                                                    \
34           fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
35           fflush (stderr);                                                   \
36           abort ();                                                          \
37         }                                                                    \
38     }                                                                        \
39   while (0)
40
41 int
42 main (int argc, char *argv[])
43 {
44 #if HAVE_DECL_ALARM
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
48      take too long.  */
49   signal (SIGALRM, SIG_DFL);
50   alarm (50);
51 #endif
52
53   {
54     const char input[] = "foo";
55     const char *result = strstr (input, "");
56     ASSERT (result == input);
57   }
58
59   {
60     const char input[] = "foo";
61     const char *result = strstr (input, "o");
62     ASSERT (result == input + 1);
63   }
64
65   {
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);
76     const char *result;
77
78     strcpy (input, fix);
79     result = strstr (input, "B1x");
80     ASSERT (result == NULL);
81     if (!page_boundary)
82       free (input);
83   }
84
85   {
86     const char input[] = "ABC ABCDAB ABCDABCDABDE";
87     const char *result = strstr (input, "ABCDABD");
88     ASSERT (result == input + 15);
89   }
90
91   {
92     const char input[] = "ABC ABCDAB ABCDABCDABDE";
93     const char *result = strstr (input, "ABCDABE");
94     ASSERT (result == NULL);
95   }
96
97   {
98     const char input[] = "ABC ABCDAB ABCDABCDABDE";
99     const char *result = strstr (input, "ABCDABCD");
100     ASSERT (result == input + 11);
101   }
102
103   /* Check that a very long haystack is handled quickly if the needle is
104      short and occurs near the beginning.  */
105   {
106     size_t repeat = 10000;
107     size_t m = 1000000;
108     char *needle =
109       "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
110       "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
111     char *haystack = (char *) malloc (m + 1);
112     if (haystack != NULL)
113       {
114         memset (haystack, 'A', m);
115         haystack[0] = 'B';
116         haystack[m] = '\0';
117
118         for (; repeat > 0; repeat--)
119           {
120             ASSERT (strstr (haystack, needle) == haystack + 1);
121           }
122
123         free (haystack);
124       }
125   }
126
127   /* Check that a very long needle is discarded quickly if the haystack is
128      short.  */
129   {
130     size_t repeat = 10000;
131     size_t m = 1000000;
132     char *haystack =
133       "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
134       "ABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABAB";
135     char *needle = (char *) malloc (m + 1);
136     if (needle != NULL)
137       {
138         memset (needle, 'A', m);
139         needle[m] = '\0';
140
141         for (; repeat > 0; repeat--)
142           {
143             ASSERT (strstr (haystack, needle) == NULL);
144           }
145
146         free (needle);
147       }
148   }
149
150   /* Check that the asymptotic worst-case complexity is not quadratic.  */
151   {
152     size_t m = 1000000;
153     char *haystack = (char *) malloc (2 * m + 2);
154     char *needle = (char *) malloc (m + 2);
155     if (haystack != NULL && needle != NULL)
156       {
157         const char *result;
158
159         memset (haystack, 'A', 2 * m);
160         haystack[2 * m] = 'B';
161         haystack[2 * m + 1] = '\0';
162
163         memset (needle, 'A', m);
164         needle[m] = 'B';
165         needle[m + 1] = '\0';
166
167         result = strstr (haystack, needle);
168         ASSERT (result == haystack + m);
169       }
170     free (needle);
171     free (haystack);
172   }
173
174   /* Sublinear speed is only possible in memmem; strstr must examine
175      every character of haystack to find its length.  */
176
177   return 0;
178 }