Avoid SIGFPE in wordexp [BZ #18100]
[glibc.git] / posix / tst-boost.c
blobe3dfd3a4fc09826fc4ec517d10729dd74448319e
1 /* Regular expression tests.
2 Copyright (C) 2003-2015 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Jakub Jelinek <jakub@redhat.com>, 2003.
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.
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.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
18 <http://www.gnu.org/licenses/>. */
20 #include <sys/types.h>
21 #include <mcheck.h>
22 #include <regex.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
27 void
28 frob_escapes (char *src, int pattern)
30 char *dst;
32 for (dst = src; *src != '\0'; dst++, src++)
34 if (*src == '\\')
36 switch (src[1])
38 case 't':
39 src++;
40 *dst = '\t';
41 continue;
42 case 'n':
43 src++;
44 *dst = '\n';
45 continue;
46 case 'r':
47 src++;
48 *dst = '\r';
49 continue;
50 case '\\':
51 case '^':
52 case '{':
53 case '|':
54 case '}':
55 if (!pattern)
57 src++;
58 *dst = *src;
59 continue;
61 break;
64 if (src != dst)
65 *dst = *src;
67 *dst = '\0';
70 int
71 main (int argc, char **argv)
73 int ret = 0, n;
74 char *line = NULL;
75 size_t line_len = 0;
76 ssize_t len;
77 FILE *f;
78 char *pattern, *string;
79 int flags = REG_EXTENDED;
80 int eflags = 0;
81 regex_t re;
82 regmatch_t rm[20];
84 mtrace ();
86 if (argc < 2)
88 fprintf (stderr, "Missing test filename\n");
89 return 1;
92 f = fopen (argv[1], "r");
93 if (f == NULL)
95 fprintf (stderr, "Couldn't open %s\n", argv[1]);
96 return 1;
99 while ((len = getline (&line, &line_len, f)) > 0)
101 char *p, *q;
102 int i;
104 if (line[len - 1] == '\n')
105 line[--len] = '\0';
107 puts (line);
109 if (line[0] == ';')
110 continue;
112 if (line[0] == '\0')
113 continue;
115 if (line[0] == '-')
117 if (strstr (line, "REG_BASIC"))
118 flags = 0;
119 else
120 flags = REG_EXTENDED;
121 if (strstr (line, "REG_ICASE"))
122 flags |= REG_ICASE;
123 if (strstr (line, "REG_NEWLINE"))
124 flags |= REG_NEWLINE;
125 eflags = 0;
126 if (strstr (line, "REG_NOTBOL"))
127 eflags |= REG_NOTBOL;
128 if (strstr (line, "REG_NOTEOL"))
129 eflags |= REG_NOTEOL;
130 continue;
133 pattern = line + strspn (line, " \t");
134 if (*pattern == '\0')
135 continue;
136 p = pattern + strcspn (pattern, " \t");
137 if (*p == '\0')
138 continue;
139 *p++ = '\0';
141 string = p + strspn (p, " \t");
142 if (*string == '\0')
143 continue;
144 if (*string == '"')
146 string++;
147 p = strchr (string, '"');
148 if (p == NULL)
149 continue;
150 *p++ = '\0';
152 else
154 p = string + strcspn (string, " \t");
155 if (*string == '!')
156 string = NULL;
157 else if (*p == '\0')
158 continue;
159 else
160 *p++ = '\0';
163 frob_escapes (pattern, 1);
164 if (string != NULL)
165 frob_escapes (string, 0);
167 n = regcomp (&re, pattern, flags);
168 if (n != 0)
170 if (string != NULL)
172 char buf[500];
173 regerror (n, &re, buf, sizeof (buf));
174 printf ("FAIL regcomp unexpectedly failed: %s\n",
175 buf);
176 ret = 1;
178 continue;
180 else if (string == NULL)
182 regfree (&re);
183 puts ("FAIL regcomp unpexpectedly succeeded");
184 ret = 1;
185 continue;
188 if (regexec (&re, string, 20, rm, eflags))
190 for (i = 0; i < 20; ++i)
192 rm[i].rm_so = -1;
193 rm[i].rm_eo = -1;
197 regfree (&re);
199 for (i = 0; i < 20 && *p != '\0'; ++i)
201 int rm_so, rm_eo;
203 rm_so = strtol (p, &q, 10);
204 if (p == q)
205 break;
206 p = q;
208 rm_eo = strtol (p, &q, 10);
209 if (p == q)
210 break;
211 p = q;
213 if (rm[i].rm_so != rm_so || rm[i].rm_eo != rm_eo)
215 printf ("FAIL rm[%d] %d..%d != expected %d..%d\n",
216 i, rm[i].rm_so, rm[i].rm_eo, rm_so, rm_eo);
217 ret = 1;
218 break;
223 free (line);
224 fclose (f);
225 return ret;