t_exhaust.c revision 1.3 1 /* $NetBSD: t_exhaust.c,v 1.3 2011/11/04 15:48:10 christos Exp $ */
2
3 /*-
4 * Copyright (c) 2011 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Christos Zoulas.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 #include <sys/cdefs.h>
40 __RCSID("$NetBSD: t_exhaust.c,v 1.3 2011/11/04 15:48:10 christos Exp $");
41
42 #include <stdio.h>
43 #include <regex.h>
44 #include <string.h>
45 #include <stdlib.h>
46 #include <err.h>
47 #include <atf-c.h>
48
49
50 static char *
51 mkstr(const char *str, size_t len)
52 {
53 size_t slen = strlen(str);
54 char *p = malloc(slen * len + 1);
55 ATF_REQUIRE(p != NULL);
56 for (size_t i = 0; i < len; i++)
57 strcpy(&p[i * slen], str);
58 return p;
59 }
60
61 static char *
62 concat(const char *d, const char *s)
63 {
64 size_t dlen = strlen(d);
65 size_t slen = strlen(s);
66 char *p = malloc(dlen + slen + 1);
67
68 ATF_REQUIRE(p != NULL);
69 strcpy(p, d);
70 strcpy(p + dlen, s);
71 return p;
72 }
73
74 static char *
75 p0(size_t len)
76 {
77 char *d, *s1, *s2;
78 s1 = mkstr("\\(", len);
79 s2 = concat(s1, ")");
80 free(s1);
81 d = concat("(", s2);
82 free(s2);
83 return d;
84 }
85
86 static char *
87 p1(size_t len)
88 {
89 char *d, *s1, *s2, *s3;
90 s1 = mkstr("\\(", 60);
91 s2 = mkstr("(.*)", len);
92 s3 = concat(s1, s2);
93 free(s2);
94 free(s1);
95 s1 = concat(s3, ")");
96 free(s3);
97 d = concat("(", s1);
98 free(s1);
99 return d;
100 }
101
102 static char *
103 ps(const char *m, const char *s, size_t len)
104 {
105 char *d, *s1, *s2, *s3;
106 s1 = mkstr(m, len);
107 s2 = mkstr(s, len);
108 s3 = concat(s1, s2);
109 free(s2);
110 free(s1);
111 d = concat("(.?)", s3);
112 free(s3);
113 return d;
114 }
115
116 static char *
117 p2(size_t len)
118 {
119 return ps("((.*){0,255}", ")", len);
120 }
121
122 static char *
123 p3(size_t len)
124 {
125 return ps("(.\\{0,}", ")", len);
126 }
127
128 static char *
129 p4(size_t len)
130 {
131 return ps("((.*){1,255}", ")", len);
132 }
133
134 static char *
135 p5(size_t len)
136 {
137 return ps("(", "){1,100}", len);
138 }
139
140 static char *
141 p6(size_t len)
142 {
143 char *d, *s1, *s2;
144 s1 = mkstr("(?:(.*)|", len);
145 s2 = concat(s1, "(.*)");
146 free(s1);
147 s1 = mkstr(")", len);
148 d = concat(s2, s1);
149 free(s1);
150 free(s2);
151 return d;
152 }
153
154 static const struct {
155 char *(*pattern)(size_t);
156 int type;
157 } tests[] = {
158 { p0, REG_EXTENDED },
159 { p1, REG_EXTENDED },
160 { p2, REG_EXTENDED },
161 { p3, REG_EXTENDED },
162 { p4, REG_EXTENDED },
163 { p5, REG_EXTENDED },
164 { p6, REG_BASIC },
165 };
166
167 ATF_TC(regcomp_too_big);
168
169 ATF_TC_HEAD(regcomp_too_big, tc)
170 {
171
172 atf_tc_set_md_var(tc, "descr", "Check that large patterns don't"
173 " crash, but return a proper error code");
174 }
175
176 ATF_TC_BODY(regcomp_too_big, tc)
177 {
178 regex_t re;
179 int e;
180
181 for (size_t i = 0; i < __arraycount(tests); i++) {
182 char *d = (*tests[i].pattern)(9999);
183 e = regcomp(&re, d, tests[i].type);
184 free(d);
185 if (e) {
186 ATF_REQUIRE_MSG(e == REG_ESPACE,
187 "regcomp returned %d for pattern %zu", e, i);
188 continue;
189 }
190 (void)regexec(&re, "aaaaaaaaaaa", 0, NULL, 0);
191 regfree(&re);
192 }
193 }
194
195 ATF_TP_ADD_TCS(tp)
196 {
197
198 ATF_TP_ADD_TC(tp, regcomp_too_big);
199 return atf_no_error();
200 }
201