Back to index

glibc  2.9
bug-regex20.c
Go to the documentation of this file.
00001 /* Test for UTF-8 regular expression optimizations.
00002    Copyright (C) 2003 Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004    Contributed by Jakub Jelinek <jakub@redhat.com>, 2003.
00005 
00006    The GNU C Library is free software; you can redistribute it and/or
00007    modify it under the terms of the GNU Lesser General Public
00008    License as published by the Free Software Foundation; either
00009    version 2.1 of the License, or (at your option) any later version.
00010 
00011    The GNU C Library is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014    Lesser General Public License for more details.
00015 
00016    You should have received a copy of the GNU Lesser General Public
00017    License along with the GNU C Library; if not, write to the Free
00018    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00019    02111-1307 USA.  */
00020 
00021 #include <sys/types.h>
00022 #include <mcheck.h>
00023 #include <regex.h>
00024 #include <stdio.h>
00025 #include <stdlib.h>
00026 #include <string.h>
00027 #include <locale.h>
00028 
00029 #define RE_NO_INTERNAL_PROTOTYPES 1
00030 #include "regex_internal.h"
00031 
00032 #define BRE RE_SYNTAX_POSIX_BASIC
00033 #define ERE RE_SYNTAX_POSIX_EXTENDED
00034 
00035 static struct
00036 {
00037   int syntax;
00038   const char *pattern;
00039   const char *string;
00040   int res, optimize;
00041 } tests[] = {
00042   /* \xc3\x84        LATIN CAPITAL LETTER A WITH DIAERESIS
00043      \xc3\x96        LATIN CAPITAL LETTER O WITH DIAERESIS
00044      \xc3\xa4        LATIN SMALL LETTER A WITH DIAERESIS
00045      \xc3\xb6        LATIN SMALL LETTER O WITH DIAERESIS
00046      \xe2\x80\x94    EM DASH  */
00047   /* Should be optimized.  */
00048   {BRE, "foo", "b\xc3\xa4rfoob\xc3\xa4z", 4, 1},
00049   {BRE, "b\xc3\xa4z", "b\xc3\xa4rfoob\xc3\xa4z", 7, 1},
00050   {BRE, "b\xc3\xa4*z", "b\xc3\xa4rfoob\xc3\xa4z", 7, 1},
00051   {BRE, "b\xc3\xa4*z", "b\xc3\xa4rfoobz", 7, 1},
00052   {BRE, "b\xc3\xa4\\+z", "b\xc3\xa4rfoob\xc3\xa4\xc3\xa4z", 7, 1},
00053   {BRE, "b\xc3\xa4\\?z", "b\xc3\xa4rfoob\xc3\xa4z", 7, 1},
00054   {BRE, "b\xc3\xa4\\{1,2\\}z", "b\xc3\xa4rfoob\xc3\xa4z", 7, 1},
00055   {BRE, "^x\\|xy*z$", "\xc3\xb6xyyz", 2, 1},
00056   {BRE, "^x\\\\y\\{6\\}z\\+", "x\\yyyyyyzz\xc3\xb6", 0, 1},
00057   {BRE, "^x\\\\y\\{2,36\\}z\\+", "x\\yzz\xc3\xb6", -1, 1},
00058   {BRE, "^x\\\\y\\{,3\\}z\\+", "x\\yyyzz\xc3\xb6", 0, 1},
00059   {BRE, "^x\\|x\xc3\xa4*z$", "\xc3\xb6x\xc3\xa4\xc3\xa4z", 2, 1},
00060   {BRE, "^x\\\\\xc3\x84\\{6\\}z\\+",
00061    "x\\\xc3\x84\xc3\x84\xc3\x84\xc3\x84\xc3\x84\xc3\x84zz\xc3\xb6", 0, 1},
00062   {BRE, "^x\\\\\xc3\x84\\{2,36\\}z\\+", "x\\\xc3\x84zz\xc3\xb6", -1, 1},
00063   {BRE, "^x\\\\\xc3\x84\\{,3\\}z\\+",
00064    "x\\\xc3\x84\xc3\x84\xc3\x84zz\xc3\xb6", 0, 1},
00065   {BRE, "x[C]y", "axCy", 1, 1},
00066   {BRE, "x[ABC]y", "axCy", 1, 1},
00067   {BRE, "\\`x\\|z\\'", "x\xe2\x80\x94", 0, 1},
00068   {BRE, "\\(xy\\)z\\1a\\1", "\xe2\x80\x94xyzxyaxy\xc3\x84", 3, 1},
00069   {BRE, "xy\\?z", "\xc3\x84xz\xc3\xb6", 2, 1},
00070   {BRE, "\\`\xc3\x84\\|z\\'", "\xc3\x84\xe2\x80\x94", 0, 1},
00071   {BRE, "\\(x\xc3\x84\\)z\\1\x61\\1",
00072    "\xe2\x80\x94x\xc3\x84zx\xc3\x84\x61x\xc3\x84\xc3\x96", 3, 1},
00073   {BRE, "x\xc3\x96\\?z", "\xc3\x84xz\xc3\xb6", 2, 1},
00074   {BRE, "x.y", "ax\xe2\x80\x94yz", 1, 1},
00075   {BRE, "x.*z", "\xc3\x84xz", 2, 1},
00076   {BRE, "x.*z", "\xc3\x84x\xe2\x80\x94z", 2, 1},
00077   {BRE, "x.*z", "\xc3\x84x\xe2\x80\x94y\xf1\x90\x80\x90z", 2, 1},
00078   {BRE, "x.*z", "\xc3\x84x\xe2\x80\x94\xc3\x94\xf1\x90\x80\x90z", 2, 1},
00079   {BRE, "x.\\?z", "axz", 1, 1},
00080   {BRE, "x.\\?z", "axyz", 1, 1},
00081   {BRE, "x.\\?z", "ax\xc3\x84z", 1, 1},
00082   {BRE, "x.\\?z", "ax\xe2\x80\x94z", 1, 1},
00083   {BRE, "x.\\?z", "ax\xf0\x9d\x80\x80z", 1, 1},
00084   {BRE, "x.\\?z", "ax\xf9\x81\x82\x83\x84z", 1, 1},
00085   {BRE, "x.\\?z", "ax\xfd\xbf\xbf\xbf\xbf\xbfz", 1, 1},
00086   {BRE, ".", "y", 0, 1},
00087   {BRE, ".", "\xc3\x84", 0, 1},
00088   {BRE, ".", "\xe2\x80\x94", 0, 1},
00089   {BRE, ".", "\xf0\x9d\x80\x80", 0, 1},
00090   {BRE, ".", "\xf9\x81\x82\x83\x84", 0, 1},
00091   {BRE, ".", "\xfd\xbf\xbf\xbf\xbf\xbf", 0, 1},
00092   {BRE, "x.\\?z", "axyyz", -1, 1},
00093   {BRE, "x.\\?z", "ax\xc3\x84\xc3\x96z", -1, 1},
00094   {BRE, "x.\\?z", "ax\xe2\x80\x94\xc3\xa4z", -1, 1},
00095   {BRE, "x.\\?z", "ax\xf0\x9d\x80\x80yz", -1, 1},
00096   {BRE, "x.\\?z", "ax\xf9\x81\x82\x83\x84\xf0\x9d\x80\x81z", -1, 1},
00097   {BRE, "x.\\?z", "ax\xfd\xbf\xbf\xbf\xbf\xbf\xc3\x96z", -1, 1},
00098   {BRE, "x.\\+z", "\xe2\x80\x94xz", -1, 1},
00099   {BRE, "x.\\+z", "\xe2\x80\x94xyz", 3, 1},
00100   {BRE, "x.\\+z", "\xe2\x80\x94x\xc3\x84y\xe2\x80\x94z", 3, 1},
00101   {BRE, "x.\\+z", "\xe2\x80\x94x\xe2\x80\x94z", 3, 1},
00102   {BRE, "x.\\+z", "\xe2\x80\x94x\xf0\x9d\x80\x80\xc3\x84z", 3, 1},
00103   {BRE, "x.\\+z", "\xe2\x80\x94x.~\xe2\x80\x94\xf9\x81\x82\x83\x84z", 3, 1},
00104   {BRE, "x.\\+z", "\xe2\x80\x94x\xfd\xbf\xbf\xbf\xbf\xbfz", 3, 1},
00105   {BRE, "x.\\{1,2\\}z", "\xe2\x80\x94xz", -1, 1},
00106   {BRE, "x.\\{1,2\\}z", "\xe2\x80\x94x\xc3\x96y\xc3\xa4z", -1, 1},
00107   {BRE, "x.\\{1,2\\}z", "\xe2\x80\x94xyz", 3, 1},
00108   {BRE, "x.\\{1,2\\}z", "\xe2\x80\x94x\xc3\x84\xe2\x80\x94z", 3, 1},
00109   {BRE, "x.\\{1,2\\}z", "\xe2\x80\x94x\xe2\x80\x94z", 3, 1},
00110   {BRE, "x.\\{1,2\\}z", "\xe2\x80\x94x\xf0\x9d\x80\x80\xc3\x84z", 3, 1},
00111   {BRE, "x.\\{1,2\\}z", "\xe2\x80\x94x~\xe2\x80\x94z", 3, 1},
00112   {BRE, "x.\\{1,2\\}z", "\xe2\x80\x94x\xfd\xbf\xbf\xbf\xbf\xbfz", 3, 1},
00113   {BRE, "x\\(.w\\|\xc3\x86\\)\\?z", "axz", 1, 1},
00114   {BRE, "x\\(.w\\|\xc3\x86\\)\\?z", "ax\xfd\xbf\xbf\xbf\xbf\xbfwz", 1, 1},
00115   {BRE, "x\\(.w\\|\xc3\x86\\)\\?z", "ax\xc3\x86z", 1, 1},
00116   {BRE, "x\\(.w\\|\xc3\x86\\)\\?z", "ax\xe2\x80\x96wz", 1, 1},
00117   {ERE, "foo", "b\xc3\xa4rfoob\xc3\xa4z", 4, 1},
00118   {ERE, "^x|xy*z$", "\xc3\xb6xyyz", 2, 1},
00119   {ERE, "^x\\\\y{6}z+", "x\\yyyyyyzz\xc3\xb6", 0, 1},
00120   {ERE, "^x\\\\y{2,36}z+", "x\\yzz\xc3\xb6", -1, 1},
00121   {ERE, "^x\\\\y{,3}z+", "x\\yyyzz\xc3\xb6", 0, 1},
00122   {ERE, "x[C]y", "axCy", 1, 1},
00123   {ERE, "x[ABC]y", "axCy", 1, 1},
00124   {ERE, "\\`x|z\\'", "x\xe2\x80\x94", 0, 1},
00125   {ERE, "(xy)z\\1a\\1", "\xe2\x80\x94xyzxyaxy\xc3\x84", 3, 1},
00126   {ERE, "xy?z", "\xc3\x84xz\xc3\xb6", 2, 1},
00127   {ERE, "x.y", "ax\xe2\x80\x94yz", 1, 1},
00128   {ERE, "x.*z", "\xc3\x84xz", 2, 1},
00129   {ERE, "x.*z", "\xc3\x84x\xe2\x80\x94z", 2, 1},
00130   {ERE, "x.*z", "\xc3\x84x\xe2\x80\x94y\xf1\x90\x80\x90z", 2, 1},
00131   {ERE, "x.*z", "\xc3\x84x\xe2\x80\x94\xc3\x94\xf1\x90\x80\x90z", 2, 1},
00132   {ERE, "x.?z", "axz", 1, 1},
00133   {ERE, "x.?z", "axyz", 1, 1},
00134   {ERE, "x.?z", "ax\xc3\x84z", 1, 1},
00135   {ERE, "x.?z", "ax\xe2\x80\x94z", 1, 1},
00136   {ERE, "x.?z", "ax\xf0\x9d\x80\x80z", 1, 1},
00137   {ERE, "x.?z", "ax\xf9\x81\x82\x83\x84z", 1, 1},
00138   {ERE, "x.?z", "ax\xfd\xbf\xbf\xbf\xbf\xbfz", 1, 1},
00139   {ERE, "x.?z", "axyyz", -1, 1},
00140   {ERE, "x.?z", "ax\xc3\x84\xc3\x96z", -1, 1},
00141   {ERE, "x.?z", "ax\xe2\x80\x94\xc3\xa4z", -1, 1},
00142   {ERE, "x.?z", "ax\xf0\x9d\x80\x80yz", -1, 1},
00143   {ERE, "x.?z", "ax\xf9\x81\x82\x83\x84\xf0\x9d\x80\x81z", -1, 1},
00144   {ERE, "x.?z", "ax\xfd\xbf\xbf\xbf\xbf\xbf\xc3\x96z", -1, 1},
00145   {ERE, "x.+z", "\xe2\x80\x94xz", -1, 1},
00146   {ERE, "x.+z", "\xe2\x80\x94xyz", 3, 1},
00147   {ERE, "x.+z", "\xe2\x80\x94x\xc3\x84y\xe2\x80\x94z", 3, 1},
00148   {ERE, "x.+z", "\xe2\x80\x94x\xe2\x80\x94z", 3, 1},
00149   {ERE, "x.+z", "\xe2\x80\x94x\xf0\x9d\x80\x80\xc3\x84z", 3, 1},
00150   {ERE, "x.+z", "\xe2\x80\x94x.~\xe2\x80\x94\xf9\x81\x82\x83\x84z", 3, 1},
00151   {ERE, "x.+z", "\xe2\x80\x94x\xfd\xbf\xbf\xbf\xbf\xbfz", 3, 1},
00152   {ERE, "x.{1,2}z", "\xe2\x80\x94xz", -1, 1},
00153   {ERE, "x.{1,2}z", "\xe2\x80\x94x\xc3\x96y\xc3\xa4z", -1, 1},
00154   {ERE, "x.{1,2}z", "\xe2\x80\x94xyz", 3, 1},
00155   {ERE, "x.{1,2}z", "\xe2\x80\x94x\xc3\x84\xe2\x80\x94z", 3, 1},
00156   {ERE, "x.{1,2}z", "\xe2\x80\x94x\xe2\x80\x94z", 3, 1},
00157   {ERE, "x.{1,2}z", "\xe2\x80\x94x\xf0\x9d\x80\x80\xc3\x84z", 3, 1},
00158   {ERE, "x.{1,2}z", "\xe2\x80\x94x~\xe2\x80\x94z", 3, 1},
00159   {ERE, "x.{1,2}z", "\xe2\x80\x94x\xfd\xbf\xbf\xbf\xbf\xbfz", 3, 1},
00160   {ERE, "x(.w|\xc3\x86)?z", "axz", 1, 1},
00161   {ERE, "x(.w|\xc3\x86)?z", "ax\xfd\xbf\xbf\xbf\xbf\xbfwz", 1, 1},
00162   {ERE, "x(.w|\xc3\x86)?z", "ax\xc3\x86z", 1, 1},
00163   {ERE, "x(.w|\xc3\x86)?z", "ax\xe2\x80\x96wz", 1, 1},
00164   /* Should not be optimized.  */
00165   {BRE, "x[\xc3\x84\xc3\xa4]y", "ax\xc3\xa4y", 1, 0},
00166   {BRE, "x[A-Z,]y", "axCy", 1, 0},
00167   {BRE, "x[^y]z", "ax\xe2\x80\x94z", 1, 0},
00168   {BRE, "x[[:alnum:]]z", "ax\xc3\x96z", 1, 0},
00169   {BRE, "x[[=A=]]z", "axAz", 1, 0},
00170   {BRE, "x[[=\xc3\x84=]]z", "ax\xc3\x84z", 1, 0},
00171   {BRE, "\\<g", "\xe2\x80\x94g", 3, 0},
00172   {BRE, "\\bg\\b", "\xe2\x80\x94g", 3, 0},
00173   {BRE, "\\Bg\\B", "\xc3\xa4g\xc3\xa4", 2, 0},
00174   {BRE, "a\\wz", "a\xc3\x84z", 0, 0},
00175   {BRE, "x\\Wz", "\xc3\x96x\xe2\x80\x94z", 2, 0},
00176   {ERE, "x[\xc3\x84\xc3\xa4]y", "ax\xc3\xa4y", 1, 0},
00177   {ERE, "x[A-Z,]y", "axCy", 1, 0},
00178   {ERE, "x[^y]z", "ax\xe2\x80\x94z", 1, 0},
00179   {ERE, "x[[:alnum:]]z", "ax\xc3\x96z", 1, 0},
00180   {ERE, "x[[=A=]]z", "axAz", 1, 0},
00181   {ERE, "x[[=\xc3\x84=]]z", "ax\xc3\x84z", 1, 0},
00182   {ERE, "\\<g", "\xe2\x80\x94g", 3, 0},
00183   {ERE, "\\bg\\b", "\xe2\x80\x94g", 3, 0},
00184   {ERE, "\\Bg\\B", "\xc3\xa4g\xc3\xa4", 2, 0},
00185   {ERE, "a\\wz", "a\xc3\x84z", 0, 0},
00186   {ERE, "x\\Wz", "\xc3\x96x\xe2\x80\x94z", 2, 0},
00187 };
00188 
00189 int
00190 main (void)
00191 {
00192   struct re_pattern_buffer regbuf;
00193   const char *err;
00194   size_t i;
00195   int ret = 0;
00196 
00197   mtrace ();
00198 
00199   setlocale (LC_ALL, "de_DE.UTF-8");
00200   for (i = 0; i < sizeof (tests) / sizeof (tests[0]); ++i)
00201     {
00202       int res, optimized;
00203 
00204       re_set_syntax (tests[i].syntax);
00205       memset (&regbuf, '\0', sizeof (regbuf));
00206       err = re_compile_pattern (tests[i].pattern, strlen (tests[i].pattern),
00207                                 &regbuf);
00208       if (err != NULL)
00209        {
00210          printf ("re_compile_pattern failed: %s\n", err);
00211          ret = 1;
00212          continue;
00213        }
00214 
00215       /* Check if re_search will be done as multi-byte or single-byte.  */
00216       optimized = ((re_dfa_t *) regbuf.buffer)->mb_cur_max == 1;
00217       if (optimized != tests[i].optimize)
00218         {
00219           printf ("pattern %zd %soptimized while it should%s be\n",
00220                 i, optimized ? "" : "not ", tests[i].optimize ? "" : " not");
00221          ret = 1;
00222         }
00223 
00224       int str_len = strlen (tests[i].string);
00225       res = re_search (&regbuf, tests[i].string, str_len, 0, str_len, NULL);
00226       if (res != tests[i].res)
00227        {
00228          printf ("re_search %zd failed: %d\n", i, res);
00229          ret = 1;
00230          regfree (&regbuf);
00231          continue;
00232        }
00233 
00234       res = re_search (&regbuf, tests[i].string, str_len, str_len, -str_len,
00235                      NULL);
00236       if (res != tests[i].res)
00237        {
00238          printf ("backward re_search %zd failed: %d\n", i, res);
00239          ret = 1;
00240          regfree (&regbuf);
00241          continue;
00242        }
00243       regfree (&regbuf);
00244 
00245       re_set_syntax (tests[i].syntax | RE_ICASE);
00246       memset (&regbuf, '\0', sizeof (regbuf));
00247       err = re_compile_pattern (tests[i].pattern, strlen (tests[i].pattern),
00248                                 &regbuf);
00249       if (err != NULL)
00250        {
00251          printf ("re_compile_pattern failed: %s\n", err);
00252          ret = 1;
00253          continue;
00254        }
00255 
00256       /* Check if re_search will be done as multi-byte or single-byte.  */
00257       optimized = ((re_dfa_t *) regbuf.buffer)->mb_cur_max == 1;
00258       if (optimized)
00259         {
00260           printf ("pattern %zd optimized while it should not be when case insensitive\n",
00261                 i);
00262          ret = 1;
00263         }
00264 
00265       res = re_search (&regbuf, tests[i].string, str_len, 0, str_len, NULL);
00266       if (res != tests[i].res)
00267        {
00268          printf ("ICASE re_search %zd failed: %d\n", i, res);
00269          ret = 1;
00270          regfree (&regbuf);
00271          continue;
00272        }
00273 
00274       res = re_search (&regbuf, tests[i].string, str_len, str_len, -str_len,
00275                      NULL);
00276       if (res != tests[i].res)
00277        {
00278          printf ("ICASE backward re_search %zd failed: %d\n", i, res);
00279          ret = 1;
00280          regfree (&regbuf);
00281          continue;
00282        }
00283       regfree (&regbuf);
00284     }
00285 
00286   return ret;
00287 }