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 /* 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 } } },
130 for (i = 0; i < sizeof (tests) / sizeof (tests[0]); ++i)
132 n = regcomp (&re, tests[i].pattern, tests[i].flags);
136 regerror (n, &re, buf, sizeof (buf));
137 report_error ("%s: regcomp %zd failed: %s", tests[i].pattern, i, buf);
141 if (regexec (&re, tests[i].string, tests[i].nmatch, rm, 0))
143 report_error ("%s: regexec %zd failed", tests[i].pattern, i);
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)
152 if (tests[i].rm[n].rm_so == -1 && tests[i].rm[n].rm_eo == -1)
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);
166 static struct re_pattern_buffer regex;
167 unsigned char folded_chars[UCHAR_MAX + 1];
170 struct re_registers regs;
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);
182 if (setlocale (LC_ALL, "en_US.UTF-8"))
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
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
195 memset (®ex, 0, sizeof regex);
196 s = re_compile_pattern (pat, sizeof pat - 1, ®ex);
198 report_error ("%s: %s", pat, s);
201 memset (®s, 0, sizeof regs);
202 i = re_search (®ex, data, sizeof data - 1,
203 0, sizeof data - 1, ®s);
205 report_error ("re_search '%s' on '%s' returned %d",
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>.
219 static char const pat[] = "[^x]x";
220 static char const data[] =
221 /* <U1000><U103B><U103D><U1014><U103A><U102F><U1015><U103A> */
232 memset (®ex, 0, sizeof regex);
233 s = re_compile_pattern (pat, sizeof pat - 1, ®ex);
235 report_error ("%s: %s", pat, s);
238 memset (®s, 0, sizeof regs);
239 i = re_search (®ex, 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",
250 if (! setlocale (LC_ALL, "C"))
252 report_error ("setlocale \"C\" failed");
257 if (setlocale (LC_ALL, "tr_TR.UTF-8"))
259 if (really_utf8 () && towupper (L'i') == 0x0130 /* U+0130; see below. */)
261 re_set_syntax (RE_SYNTAX_GREP | RE_ICASE);
262 memset (®ex, 0, sizeof regex);
263 static char const pat[] = "i";
264 s = re_compile_pattern (pat, sizeof pat - 1, ®ex);
266 report_error ("%s: %s", pat, s);
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";
275 memset (®s, 0, sizeof regs);
276 i = re_search (®ex, data, sizeof data - 1, 0, sizeof data - 1,
279 report_error ("re_search '%s' on '%s' returned %d",
287 if (! setlocale (LC_ALL, "C"))
289 report_error ("setlocale \"C\" failed");
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 (®ex, 0, sizeof regex);
297 static char const pat_3957[] = "a[^x]b";
298 s = re_compile_pattern (pat_3957, sizeof pat_3957 - 1, ®ex);
300 report_error ("%s: %s", pat_3957, s);
303 /* This should fail, but succeeds for glibc-2.5. */
304 memset (®s, 0, sizeof regs);
305 static char const data[] = "a\nb";
306 i = re_search (®ex, data, sizeof data - 1, 0, sizeof data - 1, ®s);
308 report_error ("re_search '%s' on '%s' returned %d",
315 /* This regular expression is from Spencer ere test number 75
317 re_set_syntax (RE_SYNTAX_POSIX_EGREP);
318 memset (®ex, 0, sizeof regex);
319 for (i = 0; i <= UCHAR_MAX; i++)
321 regex.translate = folded_chars;
322 static char const pat75[] = "a[[:@:>@:]]b\n";
323 s = re_compile_pattern (pat75, sizeof pat75 - 1, ®ex);
324 /* This should fail with _Invalid character class name_ error. */
327 report_error ("re_compile_pattern: failed to reject '%s'", pat75);
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 (®ex, 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, ®ex);
339 report_error ("re_compile_pattern: failed to reject '%s'", pat_b_a);
343 /* This should succeed, but does not for glibc-2.1.3. */
344 memset (®ex, 0, sizeof regex);
345 static char const pat_213[] = "{1";
346 s = re_compile_pattern (pat_213, sizeof pat_213 - 1, ®ex);
348 report_error ("%s: %s", pat_213, s);
352 /* The following example is derived from a problem report
353 against gawk from Jorge Stolfi <stolfi@ic.unicamp.br>. */
354 memset (®ex, 0, sizeof regex);
355 static char const pat_stolfi[] = "[an\371]*n";
356 s = re_compile_pattern (pat_stolfi, sizeof pat_stolfi - 1, ®ex);
358 report_error ("%s: %s", pat_stolfi, s);
359 /* This should match, but does not for glibc-2.2.1. */
362 memset (®s, 0, sizeof regs);
363 static char const data[] = "an";
364 i = re_match (®ex, data, sizeof data - 1, 0, ®s);
366 report_error ("re_match '%s' on '%s' at 2 returned %d",
367 pat_stolfi, data, i);
373 memset (®ex, 0, sizeof regex);
374 static char const pat_x[] = "x";
375 s = re_compile_pattern (pat_x, sizeof pat_x - 1, ®ex);
377 report_error ("%s: %s", pat_x, s);
378 /* glibc-2.2.93 does not work with a negative RANGE argument. */
381 memset (®s, 0, sizeof regs);
382 static char const data[] = "wxy";
383 i = re_search (®ex, data, sizeof data - 1, 2, -2, ®s);
385 report_error ("re_search '%s' on '%s' returned %d", pat_x, data, i);
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 (®ex, 0, sizeof regex);
395 s = re_compile_pattern (pat_x, 1, ®ex);
397 report_error ("%s: %s", pat_x, s);
400 memset (®s, 0, sizeof regs);
401 static char const data[] = "WXY";
402 i = re_search (®ex, data, sizeof data - 1, 0, 3, ®s);
404 report_error ("re_search '%s' on '%s' returned %d", pat_x, data, i);
410 /* glibc bug 11053. */
411 re_set_syntax (RE_SYNTAX_POSIX_BASIC);
412 memset (®ex, 0, sizeof regex);
413 static char const pat_sub2[] = "\\(a*\\)*a*\\1";
414 s = re_compile_pattern (pat_sub2, sizeof pat_sub2 - 1, ®ex);
416 report_error ("%s: %s", pat_sub2, s);
419 memset (®s, 0, sizeof regs);
420 static char const data[] = "a";
421 int datalen = sizeof data - 1;
422 i = re_search (®ex, data, datalen, 0, datalen, ®s);
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]);
439 /* Catch a bug reported by Vin Shelton in
440 https://lists.gnu.org/r/bug-coreutils/2007-06/msg00089.html
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, ®ex);
448 report_error ("%s: %s", pat_shelton, s);
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");
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
462 re_set_syntax (RE_SYNTAX_POSIX_EGREP);
463 memset (®ex, 0, sizeof regex);
464 static char const pat_badback[] = "0|()0|\\1|0";
465 s = re_compile_pattern (pat_badback, sizeof pat_badback, ®ex);
467 s = "failed to report invalid back reference";
468 if (strcmp (s, "Invalid back reference") != 0)
469 report_error ("%s: %s", pat_badback, s);
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");