1 /* Test regular expressions
2 Copyright 1996-2001, 2003-2013 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 <http://www.gnu.org/licenses/>. */
29 #include "localcharset.h"
35 static struct re_pattern_buffer regex;
36 unsigned char folded_chars[UCHAR_MAX + 1];
39 struct re_registers regs;
42 /* Some builds of glibc go into an infinite loop on this test. */
44 signal (SIGALRM, SIG_DFL);
47 if (setlocale (LC_ALL, "en_US.UTF-8"))
50 /* http://sourceware.org/ml/libc-hacker/2006-09/msg00008.html
51 This test needs valgrind to catch the bug on Debian
52 GNU/Linux 3.1 x86, but it might catch the bug better
53 on other platforms and it shouldn't hurt to try the
55 static char const pat[] = "insert into";
56 static char const data[] =
57 "\xFF\0\x12\xA2\xAA\xC4\xB1,K\x12\xC4\xB1*\xACK";
58 re_set_syntax (RE_SYNTAX_GREP | RE_HAT_LISTS_NOT_NEWLINE
60 memset (®ex, 0, sizeof regex);
61 s = re_compile_pattern (pat, sizeof pat - 1, ®ex);
64 else if (re_search (®ex, data, sizeof data - 1,
65 0, sizeof data - 1, ®s)
70 /* Check whether it's really a UTF-8 locale.
71 On mingw, the setlocale call succeeds but returns
72 "English_United States.1252", with locale_charset() returning
74 if (strcmp (locale_charset (), "UTF-8") == 0)
76 /* This test is from glibc bug 15078.
77 The test case is from Andreas Schwab in
78 <http://www.sourceware.org/ml/libc-alpha/2013-01/msg00967.html>.
80 static char const pat[] = "[^x]x";
81 static char const data[] =
82 /* <U1000><U103B><U103D><U1014><U103A><U102F><U1015><U103A> */
93 memset (®ex, 0, sizeof regex);
94 s = re_compile_pattern (pat, sizeof pat - 1, ®ex);
99 i = re_search (®ex, data, sizeof data - 1,
100 0, sizeof data - 1, 0);
101 if (i != 0 && i != 21)
106 if (! setlocale (LC_ALL, "C"))
110 /* This test is from glibc bug 3957, reported by Andrew Mackey. */
111 re_set_syntax (RE_SYNTAX_EGREP | RE_HAT_LISTS_NOT_NEWLINE);
112 memset (®ex, 0, sizeof regex);
113 s = re_compile_pattern ("a[^x]b", 6, ®ex);
116 /* This should fail, but succeeds for glibc-2.5. */
117 else if (re_search (®ex, "a\nb", 3, 0, 3, ®s) != -1)
120 /* This regular expression is from Spencer ere test number 75
122 re_set_syntax (RE_SYNTAX_POSIX_EGREP);
123 memset (®ex, 0, sizeof regex);
124 for (i = 0; i <= UCHAR_MAX; i++)
126 regex.translate = folded_chars;
127 s = re_compile_pattern ("a[[:@:>@:]]b\n", 11, ®ex);
128 /* This should fail with _Invalid character class name_ error. */
132 /* Ensure that [b-a] is diagnosed as invalid, when
133 using RE_NO_EMPTY_RANGES. */
134 re_set_syntax (RE_SYNTAX_POSIX_EGREP | RE_NO_EMPTY_RANGES);
135 memset (®ex, 0, sizeof regex);
136 s = re_compile_pattern ("a[b-a]", 6, ®ex);
140 /* This should succeed, but does not for glibc-2.1.3. */
141 memset (®ex, 0, sizeof regex);
142 s = re_compile_pattern ("{1", 2, ®ex);
146 /* The following example is derived from a problem report
147 against gawk from Jorge Stolfi <stolfi@ic.unicamp.br>. */
148 memset (®ex, 0, sizeof regex);
149 s = re_compile_pattern ("[an\371]*n", 7, ®ex);
152 /* This should match, but does not for glibc-2.2.1. */
153 else if (re_match (®ex, "an", 2, 0, ®s) != 2)
156 memset (®ex, 0, sizeof regex);
157 s = re_compile_pattern ("x", 1, ®ex);
160 /* glibc-2.2.93 does not work with a negative RANGE argument. */
161 else if (re_search (®ex, "wxy", 3, 2, -2, ®s) != 1)
164 /* The version of regex.c in older versions of gnulib
165 ignored RE_ICASE. Detect that problem too. */
166 re_set_syntax (RE_SYNTAX_EMACS | RE_ICASE);
167 memset (®ex, 0, sizeof regex);
168 s = re_compile_pattern ("x", 1, ®ex);
171 else if (re_search (®ex, "WXY", 3, 0, 3, ®s) < 0)
174 /* Catch a bug reported by Vin Shelton in
175 http://lists.gnu.org/archive/html/bug-coreutils/2007-06/msg00089.html
177 re_set_syntax (RE_SYNTAX_POSIX_BASIC
178 & ~RE_CONTEXT_INVALID_DUP
179 & ~RE_NO_EMPTY_RANGES);
180 memset (®ex, 0, sizeof regex);
181 s = re_compile_pattern ("[[:alnum:]_-]\\\\+$", 16, ®ex);
185 /* REG_STARTEND was added to glibc on 2004-01-15.
186 Reject older versions. */
191 /* It would be nice to reject hosts whose regoff_t values are too
192 narrow (including glibc on hosts with 64-bit ptrdiff_t and
193 32-bit int), but we should wait until glibc implements this
194 feature. Otherwise, support for equivalence classes and
195 multibyte collation symbols would always be broken except
196 when compiling --without-included-regex. */
197 if (sizeof (regoff_t) < sizeof (ptrdiff_t)
198 || sizeof (regoff_t) < sizeof (ssize_t))