1 /* Test re_search with multi-byte characters in EUC-JP.
2    Copyright (C) 2012-2022 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4 
5    The GNU C Library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Lesser General Public
7    License as published by the Free Software Foundation; either
8    version 2.1 of the License, or (at your option) any later version.
9 
10    The GNU C Library is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    Lesser General Public License for more details.
14 
15    You should have received a copy of the GNU Lesser General Public
16    License along with the GNU C Library; if not, see
17    <https://www.gnu.org/licenses/>.  */
18 
19 #define _GNU_SOURCE 1
20 #include <locale.h>
21 #include <regex.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 
26 static int
do_test(void)27 do_test (void)
28 {
29   struct re_pattern_buffer r;
30   struct re_registers s;
31   int e, rc = 0;
32   if (setlocale (LC_CTYPE, "ja_JP.EUC-JP") == NULL)
33     {
34       puts ("setlocale failed");
35       return 1;
36     }
37   memset (&r, 0, sizeof (r));
38   memset (&s, 0, sizeof (s));
39 
40   /* The bug cannot be reproduced without initialized fastmap (it is SBC_MAX
41      value from regex_internal.h).  */
42   r.fastmap = malloc (UCHAR_MAX + 1);
43 
44                      /* 圭 */
45   re_compile_pattern ("\xb7\xbd", 2, &r);
46 
47                 /* aaaaa件a新処, \xb7\xbd constitutes a false match */
48   e = re_search (&r, "\x61\x61\x61\x61\x61\xb7\xef\x61\xbf\xb7\xbd\xe8",
49                  12, 0, 12, &s);
50   if (e != -1)
51     {
52       printf ("bug-regex33.1: false match or error: re_search() returned %d, should return -1\n", e);
53       rc = 1;
54     }
55 
56                 /* aaaa件a新処, \xb7\xbd constitutes a false match,
57                  * this is a reproducer of BZ #13637 */
58   e = re_search (&r, "\x61\x61\x61\x61\xb7\xef\x61\xbf\xb7\xbd\xe8",
59                  11, 0, 11, &s);
60   if (e != -1)
61     {
62       printf ("bug-regex33.2: false match or error: re_search() returned %d, should return -1\n", e);
63       rc = 1;
64     }
65 
66                 /* aaa件a新処, \xb7\xbd constitutes a false match,
67                  * this is a reproducer of BZ #13637 */
68   e = re_search (&r, "\x61\x61\x61\xb7\xef\x61\xbf\xb7\xbd\xe8",
69                  10, 0, 10, &s);
70   if (e != -1)
71     {
72       printf ("bug-regex33.3: false match or error: re_search() returned %d, should return -1\n", e);
73       rc = 1;
74     }
75 
76                 /* aa件a新処, \xb7\xbd constitutes a false match */
77   e = re_search (&r, "\x61\x61\xb7\xef\x61\xbf\xb7\xbd\xe8",
78                  9, 0, 9, &s);
79   if (e != -1)
80     {
81       printf ("bug-regex33.4: false match or error: re_search() returned %d, should return -1\n", e);
82       rc = 1;
83     }
84 
85                 /* a件a新処, \xb7\xbd constitutes a false match */
86   e = re_search (&r, "\x61\xb7\xef\x61\xbf\xb7\xbd\xe8",
87                  8, 0, 8, &s);
88   if (e != -1)
89     {
90       printf ("bug-regex33.5: false match or error: re_search() returned %d, should return -1\n", e);
91       rc = 1;
92     }
93 
94                 /* 新処圭新処, \xb7\xbd here really matches 圭, but second occurrence is a false match,
95                  * this is a reproducer of bug-regex25 and BZ #13637 */
96   e = re_search (&r, "\xbf\xb7\xbd\xe8\xb7\xbd\xbf\xb7\xbd\xe8",
97                  10, 0, 10, &s);
98   if (e != 4)
99     {
100       printf ("bug-regex33.6: no match or false match: re_search() returned %d, should return 4\n", e);
101       rc = 1;
102     }
103 
104                 /* 新処圭新, \xb7\xbd here really matches 圭,
105                  * this is a reproducer of bug-regex25 */
106   e = re_search (&r, "\xbf\xb7\xbd\xe8\xb7\xbd\xbf\xb7",
107                  9, 0, 9, &s);
108   if (e != 4)
109     {
110       printf ("bug-regex33.7: no match or false match: re_search() returned %d, should return 4\n", e);
111       rc = 1;
112     }
113 
114   return rc;
115 }
116 
117 #define TEST_FUNCTION do_test ()
118 #include "../test-skeleton.c"
119