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