xref: /openbsd-src/regress/lib/libc/regex/t_exhaust.c (revision 5b46ea682d5edf3946b66939b27728f5542c88af)
1 /*	$OpenBSD: t_exhaust.c,v 1.4 2024/07/15 10:11:56 anton 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 <stdio.h>
41 #include <regex.h>
42 #include <string.h>
43 #include <stdlib.h>
44 #include <err.h>
45 //#include <atf-c.h>
46 
47 
48 static char *
mkstr(const char * str,size_t len)49 mkstr(const char *str, size_t len)
50 {
51 	size_t i, slen = strlen(str);
52 	char *p = malloc(slen * len + 1);
53 	if (p == NULL)
54 		err(1, "malloc");
55 	for (i = 0; i < len; i++)
56 		strlcpy(&p[i * slen], str, slen * len + 1 - (i * slen));
57 	return p;
58 }
59 
60 static char *
concat(const char * d,const char * s)61 concat(const char *d, const char *s)
62 {
63 	size_t dlen = strlen(d);
64 	size_t slen = strlen(s);
65 	char *p = malloc(dlen + slen + 1);
66 	strlcpy(p, d, dlen + slen + 1);
67 	strlcat(p, s, dlen + slen + 1);
68 	return p;
69 }
70 
71 static char *
p0(size_t len)72 p0(size_t len)
73 {
74 	char *d, *s1, *s2;
75 	s1 = mkstr("\\(", len);
76 	s2 = concat(s1, ")");
77 	free(s1);
78 	d = concat("(", s2);
79 	free(s2);
80 	return d;
81 }
82 
83 static char *
p1(size_t len)84 p1(size_t len)
85 {
86 	char *d, *s1, *s2, *s3;
87 	s1 = mkstr("\\(", 60);
88 	s2 = mkstr("(.*)", len);
89 	s3 = concat(s1, s2);
90 	free(s2);
91 	free(s1);
92 	s1 = concat(s3, ")");
93 	free(s3);
94 	d = concat("(", s1);
95 	free(s1);
96 	return d;
97 }
98 
99 static char *
ps(const char * m,const char * s,size_t len)100 ps(const char *m, const char *s, size_t len)
101 {
102 	char *d, *s1, *s2, *s3;
103 	s1 = mkstr(m, len);
104 	s2 = mkstr(s, len);
105 	s3 = concat(s1, s2);
106 	free(s2);
107 	free(s1);
108 	d = concat("(.?)", s3);
109 	free(s3);
110 	return d;
111 }
112 
113 static char *
p2(size_t len)114 p2(size_t len)
115 {
116 	return ps("((.*){0,255}", ")", len);
117 }
118 
119 static char *
p3(size_t len)120 p3(size_t len)
121 {
122 	return ps("(.\\{0,}", ")", len);
123 }
124 
125 static char *
p4(size_t len)126 p4(size_t len)
127 {
128 	return ps("((.*){1,255}", ")", len);
129 }
130 
131 static char *
p5(size_t len)132 p5(size_t len)
133 {
134 	return ps("(", "){1,100}", len);
135 }
136 
137 static char *
p6(size_t len)138 p6(size_t len)
139 {
140 	char *d, *s1, *s2;
141 	s1 = mkstr("(?:(.*)|", len);
142 	s2 = concat(s1, "(.*)");
143 	free(s1);
144 	s1 = mkstr(")", len);
145 	d = concat(s2, s1);
146 	free(s1);
147 	free(s2);
148 	return d;
149 }
150 
151 static char *(*patterns[])(size_t) = {
152 	p0,
153 	p1,
154 	p2,
155 	p3,
156 	p4,
157 	p5,
158 	p6,
159 };
160 
161 int
main(void)162 main(void)
163 {
164 	regex_t re;
165 	int e, ret = 0;
166 	size_t i;
167 
168 	for (i = 0; i < sizeof(patterns) / sizeof(patterns[0]); i++) {
169 		char *d = (*patterns[i])(9999);
170 		e = regcomp(&re, d, i == 6 ? REG_BASIC : REG_EXTENDED);
171 		free(d);
172 		if (e) {
173 			if (e != REG_ESPACE) {
174 				printf("regcomp returned %d for pattern %zu", e, i);
175 				ret = 1;
176 			}
177 			continue;
178 		}
179 		(void)regexec(&re, "aaaaaaaa", 0, NULL, 0);
180 		regfree(&re);
181 	}
182 	return ret;
183 }
184