This source file includes following definitions.
- report_error
- really_utf8
- bug_regex11
- main
   1 
   2 
   3 
   4 
   5 
   6 
   7 
   8 
   9 
  10 
  11 
  12 
  13 
  14 
  15 
  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 
  50 
  51 
  52 static int
  53 really_utf8 (void)
     
  54 {
  55   return strcmp (locale_charset (), "UTF-8") == 0;
  56 }
  57 
  58 
  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   
  67   { "[^~]*~", "\nx~y", 0, 2, { { 0, 3 }, { -1, -1 } } },
  68   
  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   
  78 
  79   { "\\(^\\|foo\\)bar", "bar", 0, 2, { { 0, 3 }, { -1, -1 } } },
  80   { "\\(foo\\|^\\)bar", "bar", 0, 2, { { 0, 3 }, { -1, -1 } } },
  81   
  82   { "(^|foo)bar", "bar", REG_EXTENDED, 2, { { 0, 3 }, { -1, -1 } } },
  83   { "(foo|^)bar", "bar", REG_EXTENDED, 2, { { 0, 3 }, { -1, -1 } } },
  84   
  85   { "(^|foo)bar", "(^|foo)bar", 0, 2, { { 0, 10 }, { -1, -1 } } },
  86   { "(foo|^)bar", "(foo|^)bar", 0, 2, { { 0, 10 }, { -1, -1 } } },
  87   
  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   
 120   { "^a*+(.)", "ab", REG_EXTENDED, 2, { { 0, 2 }, { 1, 2 } } },
 121   
 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   
 178 
 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         
 190 
 191 
 192 
 193 
 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           
 220 
 221 
 222 
 223           static char const pat[] = "[^x]x";
 224           static char const data[] =
 225             
 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 )
 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               
 274 
 275 
 276 
 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   
 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       
 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   
 320 
 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   
 329   if (!s)
 330     {
 331       report_error ("re_compile_pattern: failed to reject '%s'", pat75);
 332       regfree (®ex);
 333     }
 334 
 335   
 336 
 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   
 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   
 357 
 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   
 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   
 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   
 396 
 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   
 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   
 444 
 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   
 457 
 458   if (REG_STARTEND == 0)
 459     report_error ("REG_STARTEND is zero");
 460 
 461   
 462 
 463 
 464 
 465 
 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   
 477 
 478 
 479 
 480 
 481 
 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 }