Fix access after end of search string in regex matcher
[platform/upstream/glibc.git] / posix / bug-regex22.c
1 /* Test re.translate != NULL.
2    Copyright (C) 2004, 2005 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4    Contributed by Jakub Jelinek <jakub@redhat.com>, 2004.
5
6    The GNU C Library is free software; you can redistribute it and/or
7    modify it under the terms of the GNU Lesser General Public
8    License as published by the Free Software Foundation; either
9    version 2.1 of the License, or (at your option) any later version.
10
11    The GNU C Library is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14    Lesser General Public License for more details.
15
16    You should have received a copy of the GNU Lesser General Public
17    License along with the GNU C Library; if not, write to the Free
18    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19    02111-1307 USA.  */
20
21 #include <ctype.h>
22 #include <locale.h>
23 #include <regex.h>
24 #include <stdio.h>
25 #include <string.h>
26
27 int
28 main (void)
29 {
30   struct re_pattern_buffer re;
31   char trans[256];
32   int i, result = 0;
33   const char *s;
34
35   setlocale (LC_ALL, "de_DE.ISO-8859-1");
36
37   for (i = 0; i < 256; ++i)
38     trans[i] = tolower (i);
39
40   re_set_syntax (RE_SYNTAX_POSIX_EGREP);
41
42   memset (&re, 0, sizeof (re));
43   re.translate = (unsigned char *) trans;
44   s = re_compile_pattern ("\\W", 2, &re);
45
46   if (s != NULL)
47     {
48       printf ("failed to compile pattern \"\\W\": %s\n", s);
49       result = 1;
50     }
51   else
52     {
53       int ret = re_search (&re, "abc.de", 6, 0, 6, NULL);
54       if (ret != 3)
55         {
56           printf ("1st re_search returned %d\n", ret);
57           result = 1;
58         }
59
60       ret = re_search (&re, "\xc4\xd6\xae\xf7", 4, 0, 4, NULL);
61       if (ret != 2)
62         {
63           printf ("2nd re_search returned %d\n", ret);
64           result = 1;
65         }
66       re.translate = NULL;
67       regfree (&re);
68     }
69
70   memset (&re, 0, sizeof (re));
71   re.translate = (unsigned char *) trans;
72   s = re_compile_pattern ("\\w", 2, &re);
73
74   if (s != NULL)
75     {
76       printf ("failed to compile pattern \"\\w\": %s\n", s);
77       result = 1;
78     }
79   else
80     {
81       int ret = re_search (&re, ".,!abc", 6, 0, 6, NULL);
82       if (ret != 3)
83         {
84           printf ("3rd re_search returned %d\n", ret);
85           result = 1;
86         }
87
88       ret = re_search (&re, "\xae\xf7\xc4\xd6", 4, 0, 4, NULL);
89       if (ret != 2)
90         {
91           printf ("4th re_search returned %d\n", ret);
92           result = 1;
93         }
94       re.translate = NULL;
95       regfree (&re);
96     }
97
98   memset (&re, 0, sizeof (re));
99   re.translate = (unsigned char *) trans;
100   s = re_compile_pattern ("[[:DIGIT:]]", 11, &re);
101   if (s == NULL)
102     {
103       printf ("compilation of \"[[:DIGIT:]]\" pattern unexpectedly succeeded: %s\n",
104               s);
105       result = 1;
106     }
107
108   memset (&re, 0, sizeof (re));
109   re.translate = (unsigned char *) trans;
110   s = re_compile_pattern ("[[:DIGIT:]]", 2, &re);
111   if (s == NULL)
112     {
113       printf ("compilation of \"[[:DIGIT:]]\" pattern unexpectedly succeeded: %s\n",
114               s);
115       result = 1;
116     }
117
118   return result;
119 }