Imported Upstream version 1.4.19
[platform/upstream/m4.git] / tests / test-regex.c
1 /* Test regular expressions
2    Copyright 1996-2001, 2003-2021 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 <https://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 <stdarg.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <wctype.h>
28 #if HAVE_DECL_ALARM
29 # include <unistd.h>
30 # include <signal.h>
31 #endif
32
33 #include "localcharset.h"
34
35 static int exit_status;
36
37 static void
38 report_error (char const *format, ...)
39 {
40   va_list args;
41   va_start (args, format);
42   fprintf (stderr, "test-regex: ");
43   vfprintf (stderr, format, args);
44   fprintf (stderr, "\n");
45   va_end (args);
46   exit_status = 1;
47 }
48
49 /* Check whether it's really a UTF-8 locale.
50    On mingw, setlocale (LC_ALL, "en_US.UTF-8") succeeds but returns
51    "English_United States.1252", with locale_charset () returning "CP1252".  */
52 static int
53 really_utf8 (void)
54 {
55   return strcmp (locale_charset (), "UTF-8") == 0;
56 }
57
58 /* Tests supposed to match; copied from glibc posix/bug-regex11.c.  */
59 static struct
60 {
61   const char *pattern;
62   const char *string;
63   int flags, nmatch;
64   regmatch_t rm[5];
65 } const tests[] = {
66   /* Test for newline handling in regex.  */
67   { "[^~]*~", "\nx~y", 0, 2, { { 0, 3 }, { -1, -1 } } },
68   /* Other tests.  */
69   { "a(.*)b", "a b", REG_EXTENDED, 2, { { 0, 3 }, { 1, 2 } } },
70   { ".*|\\([KIO]\\)\\([^|]*\\).*|?[KIO]", "10~.~|P|K0|I10|O16|?KSb", 0, 3,
71     { { 0, 21 }, { 15, 16 }, { 16, 18 } } },
72   { ".*|\\([KIO]\\)\\([^|]*\\).*|?\\1", "10~.~|P|K0|I10|O16|?KSb", 0, 3,
73     { { 0, 21 }, { 8, 9 }, { 9, 10 } } },
74   { "^\\(a*\\)\\1\\{9\\}\\(a\\{0,9\\}\\)\\([0-9]*;.*[^a]\\2\\([0-9]\\)\\)",
75     "a1;;0a1aa2aaa3aaaa4aaaaa5aaaaaa6aaaaaaa7aaaaaaaa8aaaaaaaaa9aa2aa1a0", 0,
76     5, { { 0, 67 }, { 0, 0 }, { 0, 1 }, { 1, 67 }, { 66, 67 } } },
77   /* Test for BRE expression anchoring.  POSIX says just that this may match;
78      in glibc regex it always matched, so avoid changing it.  */
79   { "\\(^\\|foo\\)bar", "bar", 0, 2, { { 0, 3 }, { -1, -1 } } },
80   { "\\(foo\\|^\\)bar", "bar", 0, 2, { { 0, 3 }, { -1, -1 } } },
81   /* In ERE this must be treated as an anchor.  */
82   { "(^|foo)bar", "bar", REG_EXTENDED, 2, { { 0, 3 }, { -1, -1 } } },
83   { "(foo|^)bar", "bar", REG_EXTENDED, 2, { { 0, 3 }, { -1, -1 } } },
84   /* Here ^ cannot be treated as an anchor according to POSIX.  */
85   { "(^|foo)bar", "(^|foo)bar", 0, 2, { { 0, 10 }, { -1, -1 } } },
86   { "(foo|^)bar", "(foo|^)bar", 0, 2, { { 0, 10 }, { -1, -1 } } },
87   /* More tests on backreferences.  */
88   { "()\\1", "x", REG_EXTENDED, 2, { { 0, 0 }, { 0, 0 } } },
89   { "()x\\1", "x", REG_EXTENDED, 2, { { 0, 1 }, { 0, 0 } } },
90   { "()\\1*\\1*", "", REG_EXTENDED, 2, { { 0, 0 }, { 0, 0 } } },
91   { "([0-9]).*\\1(a*)", "7;7a6", REG_EXTENDED, 3, { { 0, 4 }, { 0, 1 }, { 3, 4 } } },
92   { "([0-9]).*\\1(a*)", "7;7a", REG_EXTENDED, 3, { { 0, 4 }, { 0, 1 }, { 3, 4 } } },
93   { "(b)()c\\1", "bcb", REG_EXTENDED, 3, { { 0, 3 }, { 0, 1 }, { 1, 1 } } },
94   { "()(b)c\\2", "bcb", REG_EXTENDED, 3, { { 0, 3 }, { 0, 0 }, { 0, 1 } } },
95   { "a(b)()c\\1", "abcb", REG_EXTENDED, 3, { { 0, 4 }, { 1, 2 }, { 2, 2 } } },
96   { "a()(b)c\\2", "abcb", REG_EXTENDED, 3, { { 0, 4 }, { 1, 1 }, { 1, 2 } } },
97   { "()(b)\\1c\\2", "bcb", REG_EXTENDED, 3, { { 0, 3 }, { 0, 0 }, { 0, 1 } } },
98   { "(b())\\2\\1", "bbbb", REG_EXTENDED, 3, { { 0, 2 }, { 0, 1 }, { 1, 1 } } },
99   { "a()(b)\\1c\\2", "abcb", REG_EXTENDED, 3, { { 0, 4 }, { 1, 1 }, { 1, 2 } } },
100   { "a()d(b)\\1c\\2", "adbcb", REG_EXTENDED, 3, { { 0, 5 }, { 1, 1 }, { 2, 3 } } },
101   { "a(b())\\2\\1", "abbbb", REG_EXTENDED, 3, { { 0, 3 }, { 1, 2 }, { 2, 2 } } },
102   { "(bb())\\2\\1", "bbbb", REG_EXTENDED, 3, { { 0, 4 }, { 0, 2 }, { 2, 2 } } },
103   { "^([^,]*),\\1,\\1$", "a,a,a", REG_EXTENDED, 2, { { 0, 5 }, { 0, 1 } } },
104   { "^([^,]*),\\1,\\1$", "ab,ab,ab", REG_EXTENDED, 2, { { 0, 8 }, { 0, 2 } } },
105   { "^([^,]*),\\1,\\1,\\1$", "abc,abc,abc,abc", REG_EXTENDED, 2,
106     { { 0, 15 }, { 0, 3 } } },
107   { "^(.?)(.?)(.?)(.?)(.?).?\\5\\4\\3\\2\\1$",
108     "level", REG_NOSUB | REG_EXTENDED, 0, { { -1, -1 } } },
109   { "^(.?)(.?)(.?)(.?)(.?)(.?)(.?)(.?)(.).?\\9\\8\\7\\6\\5\\4\\3\\2\\1$|^.?$",
110     "level", REG_NOSUB | REG_EXTENDED, 0, { { -1, -1 } } },
111   { "^(.?)(.?)(.?)(.?)(.?)(.?)(.?)(.?)(.).?\\9\\8\\7\\6\\5\\4\\3\\2\\1$|^.?$",
112     "abcdedcba", REG_EXTENDED, 1, { { 0, 9 } } },
113   /* XXX Not used since they fail so far.  */
114   { "^(.?)(.?)(.?)(.?)(.?)(.?)(.?)(.?)(.).?\\9\\8\\7\\6\\5\\4\\3\\2\\1$|^.?$",
115     "ababababa", REG_EXTENDED, 1, { { 0, 9 } } },
116   { "^(.?)(.?)(.?)(.?)(.?)(.?)(.?)(.?)(.?).?\\9\\8\\7\\6\\5\\4\\3\\2\\1$",
117     "level", REG_NOSUB | REG_EXTENDED, 0, { { -1, -1 } } },
118   { "^(.?)(.?)(.?)(.?)(.?)(.?)(.?)(.?)(.?).?\\9\\8\\7\\6\\5\\4\\3\\2\\1$",
119     "ababababa", REG_EXTENDED, 1, { { 0, 9 } } },
120 };
121
122 static void
123 bug_regex11 (void)
124 {
125   regex_t re;
126   regmatch_t rm[5];
127   size_t i;
128   int n;
129
130   for (i = 0; i < sizeof (tests) / sizeof (tests[0]); ++i)
131     {
132       n = regcomp (&re, tests[i].pattern, tests[i].flags);
133       if (n != 0)
134         {
135           char buf[500];
136           regerror (n, &re, buf, sizeof (buf));
137           report_error ("%s: regcomp %zd failed: %s", tests[i].pattern, i, buf);
138           continue;
139         }
140
141       if (regexec (&re, tests[i].string, tests[i].nmatch, rm, 0))
142         {
143           report_error ("%s: regexec %zd failed", tests[i].pattern, i);
144           regfree (&re);
145           continue;
146         }
147
148       for (n = 0; n < tests[i].nmatch; ++n)
149         if (rm[n].rm_so != tests[i].rm[n].rm_so
150               || rm[n].rm_eo != tests[i].rm[n].rm_eo)
151           {
152             if (tests[i].rm[n].rm_so == -1 && tests[i].rm[n].rm_eo == -1)
153               break;
154             report_error ("%s: regexec %zd match failure rm[%d] %d..%d",
155                           tests[i].pattern, i, n, rm[n].rm_so, rm[n].rm_eo);
156             break;
157           }
158
159       regfree (&re);
160     }
161 }
162
163 int
164 main (void)
165 {
166   static struct re_pattern_buffer regex;
167   unsigned char folded_chars[UCHAR_MAX + 1];
168   int i;
169   const char *s;
170   struct re_registers regs;
171
172 #if HAVE_DECL_ALARM
173   /* In case a bug causes glibc to go into an infinite loop.
174      The tests should take less than 10 s on a reasonably modern CPU.  */
175   int alarm_value = 1000;
176   signal (SIGALRM, SIG_DFL);
177   alarm (alarm_value);
178 #endif
179
180   bug_regex11 ();
181
182   if (setlocale (LC_ALL, "en_US.UTF-8"))
183     {
184       {
185         /* https://sourceware.org/ml/libc-hacker/2006-09/msg00008.html
186            This test needs valgrind to catch the bug on Debian
187            GNU/Linux 3.1 x86, but it might catch the bug better
188            on other platforms and it shouldn't hurt to try the
189            test here.  */
190         static char const pat[] = "insert into";
191         static char const data[] =
192           "\xFF\0\x12\xA2\xAA\xC4\xB1,K\x12\xC4\xB1*\xACK";
193         re_set_syntax (RE_SYNTAX_GREP | RE_HAT_LISTS_NOT_NEWLINE
194                        | RE_ICASE);
195         memset (&regex, 0, sizeof regex);
196         s = re_compile_pattern (pat, sizeof pat - 1, &regex);
197         if (s)
198           report_error ("%s: %s", pat, s);
199         else
200           {
201             memset (&regs, 0, sizeof regs);
202             i = re_search (&regex, data, sizeof data - 1,
203                            0, sizeof data - 1, &regs);
204             if (i != -1)
205               report_error ("re_search '%s' on '%s' returned %d",
206                             pat, data, i);
207             regfree (&regex);
208             free (regs.start);
209             free (regs.end);
210           }
211       }
212
213       if (really_utf8 ())
214         {
215           /* This test is from glibc bug 15078.
216              The test case is from Andreas Schwab in
217              <https://sourceware.org/ml/libc-alpha/2013-01/msg00967.html>.
218           */
219           static char const pat[] = "[^x]x";
220           static char const data[] =
221             /* <U1000><U103B><U103D><U1014><U103A><U102F><U1015><U103A> */
222             "\xe1\x80\x80"
223             "\xe1\x80\xbb"
224             "\xe1\x80\xbd"
225             "\xe1\x80\x94"
226             "\xe1\x80\xba"
227             "\xe1\x80\xaf"
228             "\xe1\x80\x95"
229             "\xe1\x80\xba"
230             "x";
231           re_set_syntax (0);
232           memset (&regex, 0, sizeof regex);
233           s = re_compile_pattern (pat, sizeof pat - 1, &regex);
234           if (s)
235             report_error ("%s: %s", pat, s);
236           else
237             {
238               memset (&regs, 0, sizeof regs);
239               i = re_search (&regex, data, sizeof data - 1,
240                              0, sizeof data - 1, 0);
241               if (i != 0 && i != 21)
242                 report_error ("re_search '%s' on '%s' returned %d",
243                               pat, data, i);
244               regfree (&regex);
245               free (regs.start);
246               free (regs.end);
247             }
248         }
249
250       if (! setlocale (LC_ALL, "C"))
251         {
252           report_error ("setlocale \"C\" failed");
253           return exit_status;
254         }
255     }
256
257   if (setlocale (LC_ALL, "tr_TR.UTF-8"))
258     {
259       if (really_utf8 () && towupper (L'i') == 0x0130 /* U+0130; see below.  */)
260         {
261           re_set_syntax (RE_SYNTAX_GREP | RE_ICASE);
262           memset (&regex, 0, sizeof regex);
263           static char const pat[] = "i";
264           s = re_compile_pattern (pat, sizeof pat - 1, &regex);
265           if (s)
266             report_error ("%s: %s", pat, s);
267           else
268             {
269               /* UTF-8 encoding of U+0130 LATIN CAPITAL LETTER I WITH DOT ABOVE.
270                  In Turkish, this is the upper-case equivalent of ASCII "i".
271                  Older versions of Gnulib failed to match "i" to U+0130 when
272                  ignoring case in Turkish <https://bugs.gnu.org/43577>.  */
273               static char const data[] = "\xc4\xb0";
274
275               memset (&regs, 0, sizeof regs);
276               i = re_search (&regex, data, sizeof data - 1, 0, sizeof data - 1,
277                              &regs);
278               if (i != 0)
279                 report_error ("re_search '%s' on '%s' returned %d",
280                               pat, data, i);
281               regfree (&regex);
282               free (regs.start);
283               free (regs.end);
284             }
285         }
286
287       if (! setlocale (LC_ALL, "C"))
288         {
289           report_error ("setlocale \"C\" failed");
290           return exit_status;
291         }
292     }
293
294   /* This test is from glibc bug 3957, reported by Andrew Mackey.  */
295   re_set_syntax (RE_SYNTAX_EGREP | RE_HAT_LISTS_NOT_NEWLINE);
296   memset (&regex, 0, sizeof regex);
297   static char const pat_3957[] = "a[^x]b";
298   s = re_compile_pattern (pat_3957, sizeof pat_3957 - 1, &regex);
299   if (s)
300     report_error ("%s: %s", pat_3957, s);
301   else
302     {
303       /* This should fail, but succeeds for glibc-2.5.  */
304       memset (&regs, 0, sizeof regs);
305       static char const data[] = "a\nb";
306       i = re_search (&regex, data, sizeof data - 1, 0, sizeof data - 1, &regs);
307       if (i != -1)
308         report_error ("re_search '%s' on '%s' returned %d",
309                       pat_3957, data, i);
310       regfree (&regex);
311       free (regs.start);
312       free (regs.end);
313     }
314
315   /* This regular expression is from Spencer ere test number 75
316      in grep-2.3.  */
317   re_set_syntax (RE_SYNTAX_POSIX_EGREP);
318   memset (&regex, 0, sizeof regex);
319   for (i = 0; i <= UCHAR_MAX; i++)
320     folded_chars[i] = i;
321   regex.translate = folded_chars;
322   static char const pat75[] = "a[[:@:>@:]]b\n";
323   s = re_compile_pattern (pat75, sizeof pat75 - 1, &regex);
324   /* This should fail with _Invalid character class name_ error.  */
325   if (!s)
326     {
327       report_error ("re_compile_pattern: failed to reject '%s'", pat75);
328       regfree (&regex);
329     }
330
331   /* Ensure that [b-a] is diagnosed as invalid, when
332      using RE_NO_EMPTY_RANGES. */
333   re_set_syntax (RE_SYNTAX_POSIX_EGREP | RE_NO_EMPTY_RANGES);
334   memset (&regex, 0, sizeof regex);
335   static char const pat_b_a[] = "a[b-a]";
336   s = re_compile_pattern (pat_b_a, sizeof pat_b_a - 1, &regex);
337   if (s == 0)
338     {
339       report_error ("re_compile_pattern: failed to reject '%s'", pat_b_a);
340       regfree (&regex);
341     }
342
343   /* This should succeed, but does not for glibc-2.1.3.  */
344   memset (&regex, 0, sizeof regex);
345   static char const pat_213[] = "{1";
346   s = re_compile_pattern (pat_213, sizeof pat_213 - 1, &regex);
347   if (s)
348     report_error ("%s: %s", pat_213, s);
349   else
350     regfree (&regex);
351
352   /* The following example is derived from a problem report
353      against gawk from Jorge Stolfi <stolfi@ic.unicamp.br>.  */
354   memset (&regex, 0, sizeof regex);
355   static char const pat_stolfi[] = "[an\371]*n";
356   s = re_compile_pattern (pat_stolfi, sizeof pat_stolfi - 1, &regex);
357   if (s)
358     report_error ("%s: %s", pat_stolfi, s);
359   /* This should match, but does not for glibc-2.2.1.  */
360   else
361     {
362       memset (&regs, 0, sizeof regs);
363       static char const data[] = "an";
364       i = re_match (&regex, data, sizeof data - 1, 0, &regs);
365       if (i != 2)
366         report_error ("re_match '%s' on '%s' at 2 returned %d",
367                       pat_stolfi, data, i);
368       regfree (&regex);
369       free (regs.start);
370       free (regs.end);
371     }
372
373   memset (&regex, 0, sizeof regex);
374   static char const pat_x[] = "x";
375   s = re_compile_pattern (pat_x, sizeof pat_x - 1, &regex);
376   if (s)
377     report_error ("%s: %s", pat_x, s);
378   /* glibc-2.2.93 does not work with a negative RANGE argument.  */
379   else
380     {
381       memset (&regs, 0, sizeof regs);
382       static char const data[] = "wxy";
383       i = re_search (&regex, data, sizeof data - 1, 2, -2, &regs);
384       if (i != 1)
385         report_error ("re_search '%s' on '%s' returned %d", pat_x, data, i);
386       regfree (&regex);
387       free (regs.start);
388       free (regs.end);
389     }
390
391   /* The version of regex.c in older versions of gnulib
392      ignored RE_ICASE.  Detect that problem too.  */
393   re_set_syntax (RE_SYNTAX_EMACS | RE_ICASE);
394   memset (&regex, 0, sizeof regex);
395   s = re_compile_pattern (pat_x, 1, &regex);
396   if (s)
397     report_error ("%s: %s", pat_x, s);
398   else
399     {
400       memset (&regs, 0, sizeof regs);
401       static char const data[] = "WXY";
402       i = re_search (&regex, data, sizeof data - 1, 0, 3, &regs);
403       if (i < 0)
404         report_error ("re_search '%s' on '%s' returned %d", pat_x, data, i);
405       regfree (&regex);
406       free (regs.start);
407       free (regs.end);
408     }
409
410   /* glibc bug 11053.  */
411   re_set_syntax (RE_SYNTAX_POSIX_BASIC);
412   memset (&regex, 0, sizeof regex);
413   static char const pat_sub2[] = "\\(a*\\)*a*\\1";
414   s = re_compile_pattern (pat_sub2, sizeof pat_sub2 - 1, &regex);
415   if (s)
416     report_error ("%s: %s", pat_sub2, s);
417   else
418     {
419       memset (&regs, 0, sizeof regs);
420       static char const data[] = "a";
421       int datalen = sizeof data - 1;
422       i = re_search (&regex, data, datalen, 0, datalen, &regs);
423       if (i != 0)
424         report_error ("re_search '%s' on '%s' returned %d", pat_sub2, data, i);
425       else if (regs.num_regs < 2)
426         report_error ("re_search '%s' on '%s' returned only %d registers",
427                       pat_sub2, data, (int) regs.num_regs);
428       else if (! (regs.start[0] == 0 && regs.end[0] == 1))
429         report_error ("re_search '%s' on '%s' returned wrong match [%d,%d)",
430                       pat_sub2, data, (int) regs.start[0], (int) regs.end[0]);
431       else if (! (regs.start[1] == 0 && regs.end[1] == 0))
432         report_error ("re_search '%s' on '%s' returned wrong submatch [%d,%d)",
433                       pat_sub2, data, regs.start[1], regs.end[1]);
434       regfree (&regex);
435       free (regs.start);
436       free (regs.end);
437     }
438
439   /* Catch a bug reported by Vin Shelton in
440      https://lists.gnu.org/r/bug-coreutils/2007-06/msg00089.html
441      */
442   re_set_syntax (RE_SYNTAX_POSIX_BASIC
443                  & ~RE_CONTEXT_INVALID_DUP
444                  & ~RE_NO_EMPTY_RANGES);
445   static char const pat_shelton[] = "[[:alnum:]_-]\\\\+$";
446   s = re_compile_pattern (pat_shelton, sizeof pat_shelton - 1, &regex);
447   if (s)
448     report_error ("%s: %s", pat_shelton, s);
449   else
450     regfree (&regex);
451
452   /* REG_STARTEND was added to glibc on 2004-01-15.
453      Reject older versions.  */
454   if (REG_STARTEND == 0)
455     report_error ("REG_STARTEND is zero");
456
457   /* Matching with the compiled form of this regexp would provoke
458      an assertion failure prior to glibc-2.28:
459        regexec.c:1375: pop_fail_stack: Assertion 'num >= 0' failed
460      With glibc-2.28, compilation fails and reports the invalid
461      back reference.  */
462   re_set_syntax (RE_SYNTAX_POSIX_EGREP);
463   memset (&regex, 0, sizeof regex);
464   static char const pat_badback[] = "0|()0|\\1|0";
465   s = re_compile_pattern (pat_badback, sizeof pat_badback, &regex);
466   if (!s)
467     s = "failed to report invalid back reference";
468   if (strcmp (s, "Invalid back reference") != 0)
469     report_error ("%s: %s", pat_badback, s);
470
471 #if 0
472   /* It would be nice to reject hosts whose regoff_t values are too
473      narrow (including glibc on hosts with 64-bit ptrdiff_t and
474      32-bit int), but we should wait until glibc implements this
475      feature.  Otherwise, support for equivalence classes and
476      multibyte collation symbols would always be broken except
477      when compiling --without-included-regex.   */
478   if (sizeof (regoff_t) < sizeof (ptrdiff_t)
479       || sizeof (regoff_t) < sizeof (ssize_t))
480     report_error ("regoff_t values are too narrow");
481 #endif
482
483   return exit_status;
484 }