1 /* Test regular expressions
2 Copyright 1996-2001, 2003-2018 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/>. */
30 #include "localcharset.h"
36 static struct re_pattern_buffer regex;
37 unsigned char folded_chars[UCHAR_MAX + 1];
40 struct re_registers regs;
43 /* Some builds of glibc go into an infinite loop on this test. */
45 signal (SIGALRM, SIG_DFL);
48 if (setlocale (LC_ALL, "en_US.UTF-8"))
51 /* https://sourceware.org/ml/libc-hacker/2006-09/msg00008.html
52 This test needs valgrind to catch the bug on Debian
53 GNU/Linux 3.1 x86, but it might catch the bug better
54 on other platforms and it shouldn't hurt to try the
56 static char const pat[] = "insert into";
57 static char const data[] =
58 "\xFF\0\x12\xA2\xAA\xC4\xB1,K\x12\xC4\xB1*\xACK";
59 re_set_syntax (RE_SYNTAX_GREP | RE_HAT_LISTS_NOT_NEWLINE
61 memset (®ex, 0, sizeof regex);
62 s = re_compile_pattern (pat, sizeof pat - 1, ®ex);
67 memset (®s, 0, sizeof regs);
68 if (re_search (®ex, data, sizeof data - 1,
69 0, sizeof data - 1, ®s)
78 /* Check whether it's really a UTF-8 locale.
79 On mingw, the setlocale call succeeds but returns
80 "English_United States.1252", with locale_charset() returning
82 if (strcmp (locale_charset (), "UTF-8") == 0)
84 /* This test is from glibc bug 15078.
85 The test case is from Andreas Schwab in
86 <https://sourceware.org/ml/libc-alpha/2013-01/msg00967.html>.
88 static char const pat[] = "[^x]x";
89 static char const data[] =
90 /* <U1000><U103B><U103D><U1014><U103A><U102F><U1015><U103A> */
101 memset (®ex, 0, sizeof regex);
102 s = re_compile_pattern (pat, sizeof pat - 1, ®ex);
107 memset (®s, 0, sizeof regs);
108 i = re_search (®ex, data, sizeof data - 1,
109 0, sizeof data - 1, 0);
110 if (i != 0 && i != 21)
118 if (! setlocale (LC_ALL, "C"))
122 /* This test is from glibc bug 3957, reported by Andrew Mackey. */
123 re_set_syntax (RE_SYNTAX_EGREP | RE_HAT_LISTS_NOT_NEWLINE);
124 memset (®ex, 0, sizeof regex);
125 s = re_compile_pattern ("a[^x]b", 6, ®ex);
128 /* This should fail, but succeeds for glibc-2.5. */
131 memset (®s, 0, sizeof regs);
132 if (re_search (®ex, "a\nb", 3, 0, 3, ®s) != -1)
139 /* This regular expression is from Spencer ere test number 75
141 re_set_syntax (RE_SYNTAX_POSIX_EGREP);
142 memset (®ex, 0, sizeof regex);
143 for (i = 0; i <= UCHAR_MAX; i++)
145 regex.translate = folded_chars;
146 s = re_compile_pattern ("a[[:@:>@:]]b\n", 11, ®ex);
147 /* This should fail with _Invalid character class name_ error. */
154 /* Ensure that [b-a] is diagnosed as invalid, when
155 using RE_NO_EMPTY_RANGES. */
156 re_set_syntax (RE_SYNTAX_POSIX_EGREP | RE_NO_EMPTY_RANGES);
157 memset (®ex, 0, sizeof regex);
158 s = re_compile_pattern ("a[b-a]", 6, ®ex);
165 /* This should succeed, but does not for glibc-2.1.3. */
166 memset (®ex, 0, sizeof regex);
167 s = re_compile_pattern ("{1", 2, ®ex);
173 /* The following example is derived from a problem report
174 against gawk from Jorge Stolfi <stolfi@ic.unicamp.br>. */
175 memset (®ex, 0, sizeof regex);
176 s = re_compile_pattern ("[an\371]*n", 7, ®ex);
179 /* This should match, but does not for glibc-2.2.1. */
182 memset (®s, 0, sizeof regs);
183 if (re_match (®ex, "an", 2, 0, ®s) != 2)
190 memset (®ex, 0, sizeof regex);
191 s = re_compile_pattern ("x", 1, ®ex);
194 /* glibc-2.2.93 does not work with a negative RANGE argument. */
197 memset (®s, 0, sizeof regs);
198 if (re_search (®ex, "wxy", 3, 2, -2, ®s) != 1)
205 /* The version of regex.c in older versions of gnulib
206 ignored RE_ICASE. Detect that problem too. */
207 re_set_syntax (RE_SYNTAX_EMACS | RE_ICASE);
208 memset (®ex, 0, sizeof regex);
209 s = re_compile_pattern ("x", 1, ®ex);
214 memset (®s, 0, sizeof regs);
215 if (re_search (®ex, "WXY", 3, 0, 3, ®s) < 0)
222 /* Catch a bug reported by Vin Shelton in
223 https://lists.gnu.org/r/bug-coreutils/2007-06/msg00089.html
225 re_set_syntax (RE_SYNTAX_POSIX_BASIC
226 & ~RE_CONTEXT_INVALID_DUP
227 & ~RE_NO_EMPTY_RANGES);
228 memset (®ex, 0, sizeof regex);
229 s = re_compile_pattern ("[[:alnum:]_-]\\\\+$", 16, ®ex);
235 /* REG_STARTEND was added to glibc on 2004-01-15.
236 Reject older versions. */
240 /* Matching with the compiled form of this regexp would provoke
241 an assertion failure prior to glibc-2.28:
242 regexec.c:1375: pop_fail_stack: Assertion 'num >= 0' failed
243 With glibc-2.28, compilation fails and reports the invalid
245 re_set_syntax (RE_SYNTAX_POSIX_EGREP);
246 memset (®ex, 0, sizeof regex);
247 s = re_compile_pattern ("0|()0|\\1|0", 10, ®ex);
248 if (!s || strcmp (s, "Invalid back reference"))
252 /* It would be nice to reject hosts whose regoff_t values are too
253 narrow (including glibc on hosts with 64-bit ptrdiff_t and
254 32-bit int), but we should wait until glibc implements this
255 feature. Otherwise, support for equivalence classes and
256 multibyte collation symbols would always be broken except
257 when compiling --without-included-regex. */
258 if (sizeof (regoff_t) < sizeof (ptrdiff_t)
259 || sizeof (regoff_t) < sizeof (ssize_t))