regex-tests, regex: fix bug: memset undeclared
[gnulib.git] / tests / test-regex.c
1 /* Test regular expressions
2    Copyright 1996-2001, 2003-2013 Free Software Foundation, Inc.
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
16
17 #include <config.h>
18
19 #include "regex.h"
20
21 #include <locale.h>
22 #include <limits.h>
23 #include <string.h>
24 #if HAVE_ALARM
25 # include <unistd.h>
26 # include <signal.h>
27 #endif
28
29 int
30 main (void)
31 {
32   int result = 0;
33   static struct re_pattern_buffer regex;
34   unsigned char folded_chars[UCHAR_MAX + 1];
35   int i;
36   const char *s;
37   struct re_registers regs;
38
39 #if HAVE_ALARM
40   /* Some builds of glibc go into an infinite loop on this test.  */
41   int alarm_value = 2;
42   signal (SIGALRM, SIG_DFL);
43   alarm (alarm_value);
44 #endif
45   if (setlocale (LC_ALL, "en_US.UTF-8"))
46     {
47       {
48         /* http://sourceware.org/ml/libc-hacker/2006-09/msg00008.html
49            This test needs valgrind to catch the bug on Debian
50            GNU/Linux 3.1 x86, but it might catch the bug better
51            on other platforms and it shouldn't hurt to try the
52            test here.  */
53         static char const pat[] = "insert into";
54         static char const data[] =
55           "\xFF\0\x12\xA2\xAA\xC4\xB1,K\x12\xC4\xB1*\xACK";
56         re_set_syntax (RE_SYNTAX_GREP | RE_HAT_LISTS_NOT_NEWLINE
57                        | RE_ICASE);
58         memset (&regex, 0, sizeof regex);
59         s = re_compile_pattern (pat, sizeof pat - 1, &regex);
60         if (s)
61           result |= 1;
62         else if (re_search (&regex, data, sizeof data - 1,
63                             0, sizeof data - 1, &regs)
64                  != -1)
65           result |= 1;
66       }
67
68       {
69         /* This test is from glibc bug 15078.
70            The test case is from Andreas Schwab in
71            <http://www.sourceware.org/ml/libc-alpha/2013-01/msg00967.html>.
72            */
73         static char const pat[] = "[^x]x";
74         static char const data[] =
75           "\xe1\x80\x80\xe1\x80\xbb\xe1\x80\xbd\xe1\x80\x94\xe1\x80"
76           "\xba\xe1\x80\xaf\xe1\x80\x95\xe1\x80\xbax";
77         re_set_syntax (0);
78         memset (&regex, 0, sizeof regex);
79         s = re_compile_pattern (pat, sizeof pat - 1, &regex);
80         if (s)
81           result |= 1;
82         else if (re_search (&regex, data, sizeof data - 1,
83                             0, sizeof data - 1, 0)
84                  != 21)
85           result |= 1;
86       }
87
88       if (! setlocale (LC_ALL, "C"))
89         return 1;
90     }
91
92   /* This test is from glibc bug 3957, reported by Andrew Mackey.  */
93   re_set_syntax (RE_SYNTAX_EGREP | RE_HAT_LISTS_NOT_NEWLINE);
94   memset (&regex, 0, sizeof regex);
95   s = re_compile_pattern ("a[^x]b", 6, &regex);
96   if (s)
97     result |= 2;
98   /* This should fail, but succeeds for glibc-2.5.  */
99   else if (re_search (&regex, "a\nb", 3, 0, 3, &regs) != -1)
100     result |= 2;
101
102   /* This regular expression is from Spencer ere test number 75
103      in grep-2.3.  */
104   re_set_syntax (RE_SYNTAX_POSIX_EGREP);
105   memset (&regex, 0, sizeof regex);
106   for (i = 0; i <= UCHAR_MAX; i++)
107     folded_chars[i] = i;
108   regex.translate = folded_chars;
109   s = re_compile_pattern ("a[[:@:>@:]]b\n", 11, &regex);
110   /* This should fail with _Invalid character class name_ error.  */
111   if (!s)
112     result |= 4;
113
114   /* Ensure that [b-a] is diagnosed as invalid, when
115      using RE_NO_EMPTY_RANGES. */
116   re_set_syntax (RE_SYNTAX_POSIX_EGREP | RE_NO_EMPTY_RANGES);
117   memset (&regex, 0, sizeof regex);
118   s = re_compile_pattern ("a[b-a]", 6, &regex);
119   if (s == 0)
120     result |= 8;
121
122   /* This should succeed, but does not for glibc-2.1.3.  */
123   memset (&regex, 0, sizeof regex);
124   s = re_compile_pattern ("{1", 2, &regex);
125   if (s)
126     result |= 8;
127
128   /* The following example is derived from a problem report
129      against gawk from Jorge Stolfi <stolfi@ic.unicamp.br>.  */
130   memset (&regex, 0, sizeof regex);
131   s = re_compile_pattern ("[an\371]*n", 7, &regex);
132   if (s)
133     result |= 8;
134   /* This should match, but does not for glibc-2.2.1.  */
135   else if (re_match (&regex, "an", 2, 0, &regs) != 2)
136     result |= 8;
137
138   memset (&regex, 0, sizeof regex);
139   s = re_compile_pattern ("x", 1, &regex);
140   if (s)
141     result |= 8;
142   /* glibc-2.2.93 does not work with a negative RANGE argument.  */
143   else if (re_search (&regex, "wxy", 3, 2, -2, &regs) != 1)
144     result |= 8;
145
146   /* The version of regex.c in older versions of gnulib
147      ignored RE_ICASE.  Detect that problem too.  */
148   re_set_syntax (RE_SYNTAX_EMACS | RE_ICASE);
149   memset (&regex, 0, sizeof regex);
150   s = re_compile_pattern ("x", 1, &regex);
151   if (s)
152     result |= 16;
153   else if (re_search (&regex, "WXY", 3, 0, 3, &regs) < 0)
154     result |= 16;
155
156   /* Catch a bug reported by Vin Shelton in
157      http://lists.gnu.org/archive/html/bug-coreutils/2007-06/msg00089.html
158      */
159   re_set_syntax (RE_SYNTAX_POSIX_BASIC
160                  & ~RE_CONTEXT_INVALID_DUP
161                  & ~RE_NO_EMPTY_RANGES);
162   memset (&regex, 0, sizeof regex);
163   s = re_compile_pattern ("[[:alnum:]_-]\\\\+$", 16, &regex);
164   if (s)
165     result |= 32;
166
167   /* REG_STARTEND was added to glibc on 2004-01-15.
168      Reject older versions.  */
169   if (! REG_STARTEND)
170     result |= 64;
171
172 #if 0
173   /* It would be nice to reject hosts whose regoff_t values are too
174      narrow (including glibc on hosts with 64-bit ptrdiff_t and
175      32-bit int), but we should wait until glibc implements this
176      feature.  Otherwise, support for equivalence classes and
177      multibyte collation symbols would always be broken except
178      when compiling --without-included-regex.   */
179   if (sizeof (regoff_t) < sizeof (ptrdiff_t)
180       || sizeof (regoff_t) < sizeof (ssize_t))
181     result |= 64;
182 #endif
183
184   return result;
185 }