1 /* Test regular expressions
2 Copyright 1996-2001, 2003-2021 Free Software Foundation, Inc.
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.
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.
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/>. */
33 #include "localcharset.h"
35 static int exit_status;
38 report_error (char const *format, ...)
41 va_start (args, format);
42 fprintf (stderr, "test-regex: ");
43 vfprintf (stderr, format, args);
44 fprintf (stderr, "\n");
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". */
55 return strcmp (locale_charset (), "UTF-8") == 0;
58 /* Tests supposed to match; copied from glibc posix/bug-regex11.c. */
66 /* Test for newline handling in regex. */
67 { "[^~]*~", "\nx~y", 0, 2, { { 0, 3 }, { -1, -1 } } },
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 { "^(.?)(.?)(.?)(.?)(.?)(.?)(.?)(.?)(.).?\\9\\8\\7\\6\\5\\4\\3\\2\\1$|^.?$",
114 "ababababa", REG_EXTENDED, 1, { { 0, 9 } } },
115 { "^(.?)(.?)(.?)(.?)(.?)(.?)(.?)(.?)(.?).?\\9\\8\\7\\6\\5\\4\\3\\2\\1$",
116 "level", REG_NOSUB | REG_EXTENDED, 0, { { -1, -1 } } },
117 { "^(.?)(.?)(.?)(.?)(.?)(.?)(.?)(.?)(.?).?\\9\\8\\7\\6\\5\\4\\3\\2\\1$",
118 "ababababa", REG_EXTENDED, 1, { { 0, 9 } } },
119 /* Test for *+ match. */
120 { "^a*+(.)", "ab", REG_EXTENDED, 2, { { 0, 2 }, { 1, 2 } } },
121 /* Test for ** match. */
122 { "^(a*)*(.)", "ab", REG_EXTENDED, 3, { { 0, 2 }, { 0, 1 }, { 1, 2 } } },
133 for (i = 0; i < sizeof (tests) / sizeof (tests[0]); ++i)
135 n = regcomp (&re, tests[i].pattern, tests[i].flags);
139 regerror (n, &re, buf, sizeof (buf));
140 report_error ("%s: regcomp %zd failed: %s", tests[i].pattern, i, buf);
144 if (regexec (&re, tests[i].string, tests[i].nmatch, rm, 0))
146 report_error ("%s: regexec %zd failed", tests[i].pattern, i);
151 for (n = 0; n < tests[i].nmatch; ++n)
152 if (rm[n].rm_so != tests[i].rm[n].rm_so
153 || rm[n].rm_eo != tests[i].rm[n].rm_eo)
155 if (tests[i].rm[n].rm_so == -1 && tests[i].rm[n].rm_eo == -1)
157 report_error ("%s: regexec %zd match failure rm[%d] %d..%d",
158 tests[i].pattern, i, n,
159 (int) rm[n].rm_so, (int) rm[n].rm_eo);
170 static struct re_pattern_buffer regex;
171 unsigned char folded_chars[UCHAR_MAX + 1];
174 struct re_registers regs;
177 /* In case a bug causes glibc to go into an infinite loop.
178 The tests should take less than 10 s on a reasonably modern CPU. */
179 int alarm_value = 1000;
180 signal (SIGALRM, SIG_DFL);
186 if (setlocale (LC_ALL, "en_US.UTF-8"))
189 /* https://sourceware.org/ml/libc-hacker/2006-09/msg00008.html
190 This test needs valgrind to catch the bug on Debian
191 GNU/Linux 3.1 x86, but it might catch the bug better
192 on other platforms and it shouldn't hurt to try the
194 static char const pat[] = "insert into";
195 static char const data[] =
196 "\xFF\0\x12\xA2\xAA\xC4\xB1,K\x12\xC4\xB1*\xACK";
197 re_set_syntax (RE_SYNTAX_GREP | RE_HAT_LISTS_NOT_NEWLINE
199 memset (®ex, 0, sizeof regex);
200 s = re_compile_pattern (pat, sizeof pat - 1, ®ex);
202 report_error ("%s: %s", pat, s);
205 memset (®s, 0, sizeof regs);
206 i = re_search (®ex, data, sizeof data - 1,
207 0, sizeof data - 1, ®s);
209 report_error ("re_search '%s' on '%s' returned %d",
219 /* This test is from glibc bug 15078.
220 The test case is from Andreas Schwab in
221 <https://sourceware.org/ml/libc-alpha/2013-01/msg00967.html>.
223 static char const pat[] = "[^x]x";
224 static char const data[] =
225 /* <U1000><U103B><U103D><U1014><U103A><U102F><U1015><U103A> */
236 memset (®ex, 0, sizeof regex);
237 s = re_compile_pattern (pat, sizeof pat - 1, ®ex);
239 report_error ("%s: %s", pat, s);
242 memset (®s, 0, sizeof regs);
243 i = re_search (®ex, data, sizeof data - 1,
244 0, sizeof data - 1, 0);
245 if (i != 0 && i != 21)
246 report_error ("re_search '%s' on '%s' returned %d",
254 if (! setlocale (LC_ALL, "C"))
256 report_error ("setlocale \"C\" failed");
261 if (setlocale (LC_ALL, "tr_TR.UTF-8"))
263 if (really_utf8 () && towupper (L'i') == 0x0130 /* U+0130; see below. */)
265 re_set_syntax (RE_SYNTAX_GREP | RE_ICASE);
266 memset (®ex, 0, sizeof regex);
267 static char const pat[] = "i";
268 s = re_compile_pattern (pat, sizeof pat - 1, ®ex);
270 report_error ("%s: %s", pat, s);
273 /* UTF-8 encoding of U+0130 LATIN CAPITAL LETTER I WITH DOT ABOVE.
274 In Turkish, this is the upper-case equivalent of ASCII "i".
275 Older versions of Gnulib failed to match "i" to U+0130 when
276 ignoring case in Turkish <https://bugs.gnu.org/43577>. */
277 static char const data[] = "\xc4\xb0";
279 memset (®s, 0, sizeof regs);
280 i = re_search (®ex, data, sizeof data - 1, 0, sizeof data - 1,
283 report_error ("re_search '%s' on '%s' returned %d",
291 if (! setlocale (LC_ALL, "C"))
293 report_error ("setlocale \"C\" failed");
298 /* This test is from glibc bug 3957, reported by Andrew Mackey. */
299 re_set_syntax (RE_SYNTAX_EGREP | RE_HAT_LISTS_NOT_NEWLINE);
300 memset (®ex, 0, sizeof regex);
301 static char const pat_3957[] = "a[^x]b";
302 s = re_compile_pattern (pat_3957, sizeof pat_3957 - 1, ®ex);
304 report_error ("%s: %s", pat_3957, s);
307 /* This should fail, but succeeds for glibc-2.5. */
308 memset (®s, 0, sizeof regs);
309 static char const data[] = "a\nb";
310 i = re_search (®ex, data, sizeof data - 1, 0, sizeof data - 1, ®s);
312 report_error ("re_search '%s' on '%s' returned %d",
319 /* This regular expression is from Spencer ere test number 75
321 re_set_syntax (RE_SYNTAX_POSIX_EGREP);
322 memset (®ex, 0, sizeof regex);
323 for (i = 0; i <= UCHAR_MAX; i++)
325 regex.translate = folded_chars;
326 static char const pat75[] = "a[[:@:>@:]]b\n";
327 s = re_compile_pattern (pat75, sizeof pat75 - 1, ®ex);
328 /* This should fail with _Invalid character class name_ error. */
331 report_error ("re_compile_pattern: failed to reject '%s'", pat75);
335 /* Ensure that [b-a] is diagnosed as invalid, when
336 using RE_NO_EMPTY_RANGES. */
337 re_set_syntax (RE_SYNTAX_POSIX_EGREP | RE_NO_EMPTY_RANGES);
338 memset (®ex, 0, sizeof regex);
339 static char const pat_b_a[] = "a[b-a]";
340 s = re_compile_pattern (pat_b_a, sizeof pat_b_a - 1, ®ex);
343 report_error ("re_compile_pattern: failed to reject '%s'", pat_b_a);
347 /* This should succeed, but does not for glibc-2.1.3. */
348 memset (®ex, 0, sizeof regex);
349 static char const pat_213[] = "{1";
350 s = re_compile_pattern (pat_213, sizeof pat_213 - 1, ®ex);
352 report_error ("%s: %s", pat_213, s);
356 /* The following example is derived from a problem report
357 against gawk from Jorge Stolfi <stolfi@ic.unicamp.br>. */
358 memset (®ex, 0, sizeof regex);
359 static char const pat_stolfi[] = "[an\371]*n";
360 s = re_compile_pattern (pat_stolfi, sizeof pat_stolfi - 1, ®ex);
362 report_error ("%s: %s", pat_stolfi, s);
363 /* This should match, but does not for glibc-2.2.1. */
366 memset (®s, 0, sizeof regs);
367 static char const data[] = "an";
368 i = re_match (®ex, data, sizeof data - 1, 0, ®s);
370 report_error ("re_match '%s' on '%s' at 2 returned %d",
371 pat_stolfi, data, i);
377 memset (®ex, 0, sizeof regex);
378 static char const pat_x[] = "x";
379 s = re_compile_pattern (pat_x, sizeof pat_x - 1, ®ex);
381 report_error ("%s: %s", pat_x, s);
382 /* glibc-2.2.93 does not work with a negative RANGE argument. */
385 memset (®s, 0, sizeof regs);
386 static char const data[] = "wxy";
387 i = re_search (®ex, data, sizeof data - 1, 2, -2, ®s);
389 report_error ("re_search '%s' on '%s' returned %d", pat_x, data, i);
395 /* The version of regex.c in older versions of gnulib
396 ignored RE_ICASE. Detect that problem too. */
397 re_set_syntax (RE_SYNTAX_EMACS | RE_ICASE);
398 memset (®ex, 0, sizeof regex);
399 s = re_compile_pattern (pat_x, 1, ®ex);
401 report_error ("%s: %s", pat_x, s);
404 memset (®s, 0, sizeof regs);
405 static char const data[] = "WXY";
406 i = re_search (®ex, data, sizeof data - 1, 0, 3, ®s);
408 report_error ("re_search '%s' on '%s' returned %d", pat_x, data, i);
414 /* glibc bug 11053. */
415 re_set_syntax (RE_SYNTAX_POSIX_BASIC);
416 memset (®ex, 0, sizeof regex);
417 static char const pat_sub2[] = "\\(a*\\)*a*\\1";
418 s = re_compile_pattern (pat_sub2, sizeof pat_sub2 - 1, ®ex);
420 report_error ("%s: %s", pat_sub2, s);
423 memset (®s, 0, sizeof regs);
424 static char const data[] = "a";
425 int datalen = sizeof data - 1;
426 i = re_search (®ex, data, datalen, 0, datalen, ®s);
428 report_error ("re_search '%s' on '%s' returned %d", pat_sub2, data, i);
429 else if (regs.num_regs < 2)
430 report_error ("re_search '%s' on '%s' returned only %d registers",
431 pat_sub2, data, (int) regs.num_regs);
432 else if (! (regs.start[0] == 0 && regs.end[0] == 1))
433 report_error ("re_search '%s' on '%s' returned wrong match [%d,%d)",
434 pat_sub2, data, (int) regs.start[0], (int) regs.end[0]);
435 else if (! (regs.start[1] == 0 && regs.end[1] == 0))
436 report_error ("re_search '%s' on '%s' returned wrong submatch [%d,%d)",
437 pat_sub2, data, (int) regs.start[1], (int) regs.end[1]);
443 /* Catch a bug reported by Vin Shelton in
444 https://lists.gnu.org/r/bug-coreutils/2007-06/msg00089.html
446 re_set_syntax (RE_SYNTAX_POSIX_BASIC
447 & ~RE_CONTEXT_INVALID_DUP
448 & ~RE_NO_EMPTY_RANGES);
449 static char const pat_shelton[] = "[[:alnum:]_-]\\\\+$";
450 s = re_compile_pattern (pat_shelton, sizeof pat_shelton - 1, ®ex);
452 report_error ("%s: %s", pat_shelton, s);
456 /* REG_STARTEND was added to glibc on 2004-01-15.
457 Reject older versions. */
458 if (REG_STARTEND == 0)
459 report_error ("REG_STARTEND is zero");
461 /* Matching with the compiled form of this regexp would provoke
462 an assertion failure prior to glibc-2.28:
463 regexec.c:1375: pop_fail_stack: Assertion 'num >= 0' failed
464 With glibc-2.28, compilation fails and reports the invalid
466 re_set_syntax (RE_SYNTAX_POSIX_EGREP);
467 memset (®ex, 0, sizeof regex);
468 static char const pat_badback[] = "0|()0|\\1|0";
469 s = re_compile_pattern (pat_badback, sizeof pat_badback, ®ex);
470 if (!s && re_search (®ex, "x", 1, 0, 1, ®s) != -1)
471 s = "mishandled invalid back reference";
472 if (s && strcmp (s, "Invalid back reference") != 0)
473 report_error ("%s: %s", pat_badback, s);
476 /* It would be nice to reject hosts whose regoff_t values are too
477 narrow (including glibc on hosts with 64-bit ptrdiff_t and
478 32-bit int), but we should wait until glibc implements this
479 feature. Otherwise, support for equivalence classes and
480 multibyte collation symbols would always be broken except
481 when compiling --without-included-regex. */
482 if (sizeof (regoff_t) < sizeof (ptrdiff_t)
483 || sizeof (regoff_t) < sizeof (ssize_t))
484 report_error ("regoff_t values are too narrow");