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