1 /* $NetBSD: t_exhaust.c,v 1.8 2017/01/14 00:50:56 christos Exp $ */
4 * Copyright (c) 2011 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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.
39 #include <sys/cdefs.h>
40 __RCSID("$NetBSD: t_exhaust.c,v 1.8 2017/01/14 00:50:56 christos Exp $");
42 #include <sys/resource.h>
51 #define REGEX_MAXSIZE 9999
55 mkstr(const char *str, size_t len)
57 size_t slen = strlen(str);
58 char *p = malloc(slen * len + 1);
59 ATF_REQUIRE(p != NULL);
60 for (size_t i = 0; i < len; i++)
61 strcpy(&p[i * slen], str);
66 concat(const char *d, const char *s)
68 size_t dlen = strlen(d);
69 size_t slen = strlen(s);
70 char *p = malloc(dlen + slen + 1);
72 ATF_REQUIRE(p != NULL);
82 s1 = mkstr("\\(", len);
93 char *d, *s1, *s2, *s3;
94 s1 = mkstr("\\(", 60);
95 s2 = mkstr("(.*)", len);
107 ps(const char *m, const char *s, size_t len)
109 char *d, *s1, *s2, *s3;
115 d = concat("(.?)", s3);
123 return ps("((.*){0,255}", ")", len);
129 return ps("(.\\{0,}", ")", len);
135 return ps("((.*){1,255}", ")", len);
141 return ps("(", "){1,100}", len);
148 s1 = mkstr("(?:(.*)|", len);
149 s2 = concat(s1, "(.*)");
151 s1 = mkstr(")", len);
158 static const struct {
159 char *(*pattern)(size_t);
162 { p0, REG_EXTENDED },
163 { p1, REG_EXTENDED },
164 { p2, REG_EXTENDED },
165 { p3, REG_EXTENDED },
166 { p4, REG_EXTENDED },
167 { p5, REG_EXTENDED },
171 ATF_TC(regcomp_too_big);
173 ATF_TC_HEAD(regcomp_too_big, tc)
176 atf_tc_set_md_var(tc, "descr", "Check that large patterns don't"
177 " crash, but return a proper error code");
179 atf_tc_set_md_var(tc, "timeout", "600");
180 atf_tc_set_md_var(tc, "require.memory", "64M");
183 ATF_TC_BODY(regcomp_too_big, tc)
189 limit.rlim_cur = limit.rlim_max = 64 * 1024 * 1024;
190 ATF_REQUIRE(setrlimit(RLIMIT_VMEM, &limit) != -1);
191 for (size_t i = 0; i < __arraycount(tests); i++) {
192 char *d = (*tests[i].pattern)(REGEX_MAXSIZE);
193 e = regcomp(&re, d, tests[i].type);
196 (void)regerror(e, &re, ebuf, sizeof(ebuf));
197 ATF_REQUIRE_MSG(e == REG_ESPACE,
198 "regcomp returned %d (%s) for pattern %zu [%s]", e, ebuf,
204 (void)regexec(&re, "aaaaaaaaaaa", 0, NULL, 0);
212 ATF_TP_ADD_TC(tp, regcomp_too_big);
213 return atf_no_error();