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