| 1 | /* Test regular expressions
|
|---|
| 2 | Copyright 1996-2001, 2003-2022 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 | { "^(.?)(.?)(.?)(.?)(.?)(.?)(.?)(.?)(.).?\\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 } } },
|
|---|
| 123 | };
|
|---|
| 124 |
|
|---|
| 125 | static void
|
|---|
| 126 | bug_regex11 (void)
|
|---|
| 127 | {
|
|---|
| 128 | regex_t re;
|
|---|
| 129 | regmatch_t rm[5];
|
|---|
| 130 | size_t i;
|
|---|
| 131 | int n;
|
|---|
| 132 |
|
|---|
| 133 | for (i = 0; i < sizeof (tests) / sizeof (tests[0]); ++i)
|
|---|
| 134 | {
|
|---|
| 135 | n = regcomp (&re, tests[i].pattern, tests[i].flags);
|
|---|
| 136 | if (n != 0)
|
|---|
| 137 | {
|
|---|
| 138 | char buf[500];
|
|---|
| 139 | regerror (n, &re, buf, sizeof (buf));
|
|---|
| 140 | report_error ("%s: regcomp %zd failed: %s", tests[i].pattern, i, buf);
|
|---|
| 141 | continue;
|
|---|
| 142 | }
|
|---|
| 143 |
|
|---|
| 144 | if (regexec (&re, tests[i].string, tests[i].nmatch, rm, 0))
|
|---|
| 145 | {
|
|---|
| 146 | report_error ("%s: regexec %zd failed", tests[i].pattern, i);
|
|---|
| 147 | regfree (&re);
|
|---|
| 148 | continue;
|
|---|
| 149 | }
|
|---|
| 150 |
|
|---|
| 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)
|
|---|
| 154 | {
|
|---|
| 155 | if (tests[i].rm[n].rm_so == -1 && tests[i].rm[n].rm_eo == -1)
|
|---|
| 156 | break;
|
|---|
| 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);
|
|---|
| 160 | break;
|
|---|
| 161 | }
|
|---|
| 162 |
|
|---|
| 163 | regfree (&re);
|
|---|
| 164 | }
|
|---|
| 165 | }
|
|---|
| 166 |
|
|---|
| 167 | int
|
|---|
| 168 | main (void)
|
|---|
| 169 | {
|
|---|
| 170 | static struct re_pattern_buffer regex;
|
|---|
| 171 | unsigned char folded_chars[UCHAR_MAX + 1];
|
|---|
| 172 | int i;
|
|---|
| 173 | const char *s;
|
|---|
| 174 | struct re_registers regs;
|
|---|
| 175 |
|
|---|
| 176 | #if HAVE_DECL_ALARM
|
|---|
| 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);
|
|---|
| 181 | alarm (alarm_value);
|
|---|
| 182 | #endif
|
|---|
| 183 |
|
|---|
| 184 | bug_regex11 ();
|
|---|
| 185 |
|
|---|
| 186 | if (setlocale (LC_ALL, "en_US.UTF-8"))
|
|---|
| 187 | {
|
|---|
| 188 | {
|
|---|
| 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
|
|---|
| 193 | test here. */
|
|---|
| 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
|
|---|
| 198 | | RE_ICASE);
|
|---|
| 199 | memset (®ex, 0, sizeof regex);
|
|---|
| 200 | s = re_compile_pattern (pat, sizeof pat - 1, ®ex);
|
|---|
| 201 | if (s)
|
|---|
| 202 | report_error ("%s: %s", pat, s);
|
|---|
| 203 | else
|
|---|
| 204 | {
|
|---|
| 205 | memset (®s, 0, sizeof regs);
|
|---|
| 206 | i = re_search (®ex, data, sizeof data - 1,
|
|---|
| 207 | 0, sizeof data - 1, ®s);
|
|---|
| 208 | if (i != -1)
|
|---|
| 209 | report_error ("re_search '%s' on '%s' returned %d",
|
|---|
| 210 | pat, data, i);
|
|---|
| 211 | regfree (®ex);
|
|---|
| 212 | free (regs.start);
|
|---|
| 213 | free (regs.end);
|
|---|
| 214 | }
|
|---|
| 215 | }
|
|---|
| 216 |
|
|---|
| 217 | if (really_utf8 ())
|
|---|
| 218 | {
|
|---|
| 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>.
|
|---|
| 222 | */
|
|---|
| 223 | static char const pat[] = "[^x]x";
|
|---|
| 224 | static char const data[] =
|
|---|
| 225 | /* <U1000><U103B><U103D><U1014><U103A><U102F><U1015><U103A> */
|
|---|
| 226 | "\xe1\x80\x80"
|
|---|
| 227 | "\xe1\x80\xbb"
|
|---|
| 228 | "\xe1\x80\xbd"
|
|---|
| 229 | "\xe1\x80\x94"
|
|---|
| 230 | "\xe1\x80\xba"
|
|---|
| 231 | "\xe1\x80\xaf"
|
|---|
| 232 | "\xe1\x80\x95"
|
|---|
| 233 | "\xe1\x80\xba"
|
|---|
| 234 | "x";
|
|---|
| 235 | re_set_syntax (0);
|
|---|
| 236 | memset (®ex, 0, sizeof regex);
|
|---|
| 237 | s = re_compile_pattern (pat, sizeof pat - 1, ®ex);
|
|---|
| 238 | if (s)
|
|---|
| 239 | report_error ("%s: %s", pat, s);
|
|---|
| 240 | else
|
|---|
| 241 | {
|
|---|
| 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",
|
|---|
| 247 | pat, data, i);
|
|---|
| 248 | regfree (®ex);
|
|---|
| 249 | free (regs.start);
|
|---|
| 250 | free (regs.end);
|
|---|
| 251 | }
|
|---|
| 252 | }
|
|---|
| 253 |
|
|---|
| 254 | if (! setlocale (LC_ALL, "C"))
|
|---|
| 255 | {
|
|---|
| 256 | report_error ("setlocale \"C\" failed");
|
|---|
| 257 | return exit_status;
|
|---|
| 258 | }
|
|---|
| 259 | }
|
|---|
| 260 |
|
|---|
| 261 | if (setlocale (LC_ALL, "tr_TR.UTF-8"))
|
|---|
| 262 | {
|
|---|
| 263 | if (really_utf8 () && towupper (L'i') == 0x0130 /* U+0130; see below. */)
|
|---|
| 264 | {
|
|---|
| 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);
|
|---|
| 269 | if (s)
|
|---|
| 270 | report_error ("%s: %s", pat, s);
|
|---|
| 271 | else
|
|---|
| 272 | {
|
|---|
| 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";
|
|---|
| 278 |
|
|---|
| 279 | memset (®s, 0, sizeof regs);
|
|---|
| 280 | i = re_search (®ex, data, sizeof data - 1, 0, sizeof data - 1,
|
|---|
| 281 | ®s);
|
|---|
| 282 | if (i != 0)
|
|---|
| 283 | report_error ("re_search '%s' on '%s' returned %d",
|
|---|
| 284 | pat, data, i);
|
|---|
| 285 | regfree (®ex);
|
|---|
| 286 | free (regs.start);
|
|---|
| 287 | free (regs.end);
|
|---|
| 288 | }
|
|---|
| 289 | }
|
|---|
| 290 |
|
|---|
| 291 | if (! setlocale (LC_ALL, "C"))
|
|---|
| 292 | {
|
|---|
| 293 | report_error ("setlocale \"C\" failed");
|
|---|
| 294 | return exit_status;
|
|---|
| 295 | }
|
|---|
| 296 | }
|
|---|
| 297 |
|
|---|
| 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);
|
|---|
| 303 | if (s)
|
|---|
| 304 | report_error ("%s: %s", pat_3957, s);
|
|---|
| 305 | else
|
|---|
| 306 | {
|
|---|
| 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);
|
|---|
| 311 | if (i != -1)
|
|---|
| 312 | report_error ("re_search '%s' on '%s' returned %d",
|
|---|
| 313 | pat_3957, data, i);
|
|---|
| 314 | regfree (®ex);
|
|---|
| 315 | free (regs.start);
|
|---|
| 316 | free (regs.end);
|
|---|
| 317 | }
|
|---|
| 318 |
|
|---|
| 319 | /* This regular expression is from Spencer ere test number 75
|
|---|
| 320 | in grep-2.3. */
|
|---|
| 321 | re_set_syntax (RE_SYNTAX_POSIX_EGREP);
|
|---|
| 322 | memset (®ex, 0, sizeof regex);
|
|---|
| 323 | for (i = 0; i <= UCHAR_MAX; i++)
|
|---|
| 324 | folded_chars[i] = 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. */
|
|---|
| 329 | if (!s)
|
|---|
| 330 | {
|
|---|
| 331 | report_error ("re_compile_pattern: failed to reject '%s'", pat75);
|
|---|
| 332 | regfree (®ex);
|
|---|
| 333 | }
|
|---|
| 334 |
|
|---|
| 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);
|
|---|
| 341 | if (s == 0)
|
|---|
| 342 | {
|
|---|
| 343 | report_error ("re_compile_pattern: failed to reject '%s'", pat_b_a);
|
|---|
| 344 | regfree (®ex);
|
|---|
| 345 | }
|
|---|
| 346 |
|
|---|
| 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);
|
|---|
| 351 | if (s)
|
|---|
| 352 | report_error ("%s: %s", pat_213, s);
|
|---|
| 353 | else
|
|---|
| 354 | regfree (®ex);
|
|---|
| 355 |
|
|---|
| 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);
|
|---|
| 361 | if (s)
|
|---|
| 362 | report_error ("%s: %s", pat_stolfi, s);
|
|---|
| 363 | /* This should match, but does not for glibc-2.2.1. */
|
|---|
| 364 | else
|
|---|
| 365 | {
|
|---|
| 366 | memset (®s, 0, sizeof regs);
|
|---|
| 367 | static char const data[] = "an";
|
|---|
| 368 | i = re_match (®ex, data, sizeof data - 1, 0, ®s);
|
|---|
| 369 | if (i != 2)
|
|---|
| 370 | report_error ("re_match '%s' on '%s' at 2 returned %d",
|
|---|
| 371 | pat_stolfi, data, i);
|
|---|
| 372 | regfree (®ex);
|
|---|
| 373 | free (regs.start);
|
|---|
| 374 | free (regs.end);
|
|---|
| 375 | }
|
|---|
| 376 |
|
|---|
| 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);
|
|---|
| 380 | if (s)
|
|---|
| 381 | report_error ("%s: %s", pat_x, s);
|
|---|
| 382 | /* glibc-2.2.93 does not work with a negative RANGE argument. */
|
|---|
| 383 | else
|
|---|
| 384 | {
|
|---|
| 385 | memset (®s, 0, sizeof regs);
|
|---|
| 386 | static char const data[] = "wxy";
|
|---|
| 387 | i = re_search (®ex, data, sizeof data - 1, 2, -2, ®s);
|
|---|
| 388 | if (i != 1)
|
|---|
| 389 | report_error ("re_search '%s' on '%s' returned %d", pat_x, data, i);
|
|---|
| 390 | regfree (®ex);
|
|---|
| 391 | free (regs.start);
|
|---|
| 392 | free (regs.end);
|
|---|
| 393 | }
|
|---|
| 394 |
|
|---|
| 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);
|
|---|
| 400 | if (s)
|
|---|
| 401 | report_error ("%s: %s", pat_x, s);
|
|---|
| 402 | else
|
|---|
| 403 | {
|
|---|
| 404 | memset (®s, 0, sizeof regs);
|
|---|
| 405 | static char const data[] = "WXY";
|
|---|
| 406 | i = re_search (®ex, data, sizeof data - 1, 0, 3, ®s);
|
|---|
| 407 | if (i < 0)
|
|---|
| 408 | report_error ("re_search '%s' on '%s' returned %d", pat_x, data, i);
|
|---|
| 409 | regfree (®ex);
|
|---|
| 410 | free (regs.start);
|
|---|
| 411 | free (regs.end);
|
|---|
| 412 | }
|
|---|
| 413 |
|
|---|
| 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);
|
|---|
| 419 | if (s)
|
|---|
| 420 | report_error ("%s: %s", pat_sub2, s);
|
|---|
| 421 | else
|
|---|
| 422 | {
|
|---|
| 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);
|
|---|
| 427 | if (i != 0)
|
|---|
| 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]);
|
|---|
| 438 | regfree (®ex);
|
|---|
| 439 | free (regs.start);
|
|---|
| 440 | free (regs.end);
|
|---|
| 441 | }
|
|---|
| 442 |
|
|---|
| 443 | /* Catch a bug reported by Vin Shelton in
|
|---|
| 444 | https://lists.gnu.org/r/bug-coreutils/2007-06/msg00089.html
|
|---|
| 445 | */
|
|---|
| 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);
|
|---|
| 451 | if (s)
|
|---|
| 452 | report_error ("%s: %s", pat_shelton, s);
|
|---|
| 453 | else
|
|---|
| 454 | regfree (®ex);
|
|---|
| 455 |
|
|---|
| 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");
|
|---|
| 460 |
|
|---|
| 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
|
|---|
| 465 | back reference. */
|
|---|
| 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);
|
|---|
| 474 |
|
|---|
| 475 | #if 0
|
|---|
| 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");
|
|---|
| 485 | #endif
|
|---|
| 486 |
|
|---|
| 487 | return exit_status;
|
|---|
| 488 | }
|
|---|