xref: /openbsd-src/regress/lib/libc/regex/t_exhaust.c (revision 5b46ea682d5edf3946b66939b27728f5542c88af)
1*5b46ea68Santon /*	$OpenBSD: t_exhaust.c,v 1.4 2024/07/15 10:11:56 anton Exp $	*/
21338765aSotto /*	$NetBSD: t_exhaust.c,v 1.2 2011/10/21 00:41:34 christos Exp $	*/
31338765aSotto 
41338765aSotto /*-
51338765aSotto  * Copyright (c) 2011 The NetBSD Foundation, Inc.
61338765aSotto  * All rights reserved.
71338765aSotto  *
81338765aSotto  * This code is derived from software contributed to The NetBSD Foundation
91338765aSotto  * by Christos Zoulas.
101338765aSotto  *
111338765aSotto  * Redistribution and use in source and binary forms, with or without
121338765aSotto  * modification, are permitted provided that the following conditions
131338765aSotto  * are met:
141338765aSotto  * 1. Redistributions of source code must retain the above copyright
151338765aSotto  *    notice, this list of conditions and the following disclaimer.
161338765aSotto  * 2. Redistributions in binary form must reproduce the above copyright
171338765aSotto  *    notice, this list of conditions and the following disclaimer in the
181338765aSotto  *    documentation and/or other materials provided with the distribution.
191338765aSotto  * 3. All advertising materials mentioning features or use of this software
201338765aSotto  *    must display the following acknowledgement:
211338765aSotto  *        This product includes software developed by the NetBSD
221338765aSotto  *        Foundation, Inc. and its contributors.
231338765aSotto  * 4. Neither the name of The NetBSD Foundation nor the names of its
241338765aSotto  *    contributors may be used to endorse or promote products derived
251338765aSotto  *    from this software without specific prior written permission.
261338765aSotto  *
271338765aSotto  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
281338765aSotto  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
291338765aSotto  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
301338765aSotto  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
311338765aSotto  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
321338765aSotto  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
331338765aSotto  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
341338765aSotto  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
351338765aSotto  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
361338765aSotto  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
371338765aSotto  * POSSIBILITY OF SUCH DAMAGE.
381338765aSotto  */
391338765aSotto 
401338765aSotto #include <stdio.h>
411338765aSotto #include <regex.h>
421338765aSotto #include <string.h>
431338765aSotto #include <stdlib.h>
441338765aSotto #include <err.h>
451338765aSotto //#include <atf-c.h>
461338765aSotto 
471338765aSotto 
481338765aSotto static char *
mkstr(const char * str,size_t len)491338765aSotto mkstr(const char *str, size_t len)
501338765aSotto {
511338765aSotto 	size_t i, slen = strlen(str);
521338765aSotto 	char *p = malloc(slen * len + 1);
531338765aSotto 	if (p == NULL)
541338765aSotto 		err(1, "malloc");
551338765aSotto 	for (i = 0; i < len; i++)
561338765aSotto 		strlcpy(&p[i * slen], str, slen * len + 1 - (i * slen));
571338765aSotto 	return p;
581338765aSotto }
591338765aSotto 
601338765aSotto static char *
concat(const char * d,const char * s)611338765aSotto concat(const char *d, const char *s)
621338765aSotto {
631338765aSotto 	size_t dlen = strlen(d);
641338765aSotto 	size_t slen = strlen(s);
651338765aSotto 	char *p = malloc(dlen + slen + 1);
661338765aSotto 	strlcpy(p, d, dlen + slen + 1);
671338765aSotto 	strlcat(p, s, dlen + slen + 1);
681338765aSotto 	return p;
691338765aSotto }
701338765aSotto 
711338765aSotto static char *
p0(size_t len)721338765aSotto p0(size_t len)
731338765aSotto {
741338765aSotto 	char *d, *s1, *s2;
751338765aSotto 	s1 = mkstr("\\(", len);
761338765aSotto 	s2 = concat(s1, ")");
771338765aSotto 	free(s1);
781338765aSotto 	d = concat("(", s2);
791338765aSotto 	free(s2);
801338765aSotto 	return d;
811338765aSotto }
821338765aSotto 
831338765aSotto static char *
p1(size_t len)841338765aSotto p1(size_t len)
851338765aSotto {
861338765aSotto 	char *d, *s1, *s2, *s3;
871338765aSotto 	s1 = mkstr("\\(", 60);
881338765aSotto 	s2 = mkstr("(.*)", len);
891338765aSotto 	s3 = concat(s1, s2);
901338765aSotto 	free(s2);
911338765aSotto 	free(s1);
921338765aSotto 	s1 = concat(s3, ")");
931338765aSotto 	free(s3);
941338765aSotto 	d = concat("(", s1);
951338765aSotto 	free(s1);
961338765aSotto 	return d;
971338765aSotto }
981338765aSotto 
991338765aSotto static char *
ps(const char * m,const char * s,size_t len)1001338765aSotto ps(const char *m, const char *s, size_t len)
1011338765aSotto {
1021338765aSotto 	char *d, *s1, *s2, *s3;
1031338765aSotto 	s1 = mkstr(m, len);
1041338765aSotto 	s2 = mkstr(s, len);
1051338765aSotto 	s3 = concat(s1, s2);
1061338765aSotto 	free(s2);
1071338765aSotto 	free(s1);
1081338765aSotto 	d = concat("(.?)", s3);
1091338765aSotto 	free(s3);
1101338765aSotto 	return d;
1111338765aSotto }
1121338765aSotto 
1131338765aSotto static char *
p2(size_t len)1141338765aSotto p2(size_t len)
1151338765aSotto {
1161338765aSotto 	return ps("((.*){0,255}", ")", len);
1171338765aSotto }
1181338765aSotto 
1191338765aSotto static char *
p3(size_t len)1201338765aSotto p3(size_t len)
1211338765aSotto {
1221338765aSotto 	return ps("(.\\{0,}", ")", len);
1231338765aSotto }
1241338765aSotto 
1251338765aSotto static char *
p4(size_t len)1261338765aSotto p4(size_t len)
1271338765aSotto {
1281338765aSotto 	return ps("((.*){1,255}", ")", len);
1291338765aSotto }
1301338765aSotto 
1311338765aSotto static char *
p5(size_t len)1321338765aSotto p5(size_t len)
1331338765aSotto {
1341338765aSotto 	return ps("(", "){1,100}", len);
1351338765aSotto }
1361338765aSotto 
1371338765aSotto static char *
p6(size_t len)1381338765aSotto p6(size_t len)
1391338765aSotto {
1401338765aSotto 	char *d, *s1, *s2;
1411338765aSotto 	s1 = mkstr("(?:(.*)|", len);
1421338765aSotto 	s2 = concat(s1, "(.*)");
1431338765aSotto 	free(s1);
1441338765aSotto 	s1 = mkstr(")", len);
1451338765aSotto 	d = concat(s2, s1);
1461338765aSotto 	free(s1);
1471338765aSotto 	free(s2);
1481338765aSotto 	return d;
1491338765aSotto }
1501338765aSotto 
1511338765aSotto static char *(*patterns[])(size_t) = {
1521338765aSotto 	p0,
1531338765aSotto 	p1,
1541338765aSotto 	p2,
1551338765aSotto 	p3,
1561338765aSotto 	p4,
1571338765aSotto 	p5,
1581338765aSotto 	p6,
1591338765aSotto };
1601338765aSotto 
161*5b46ea68Santon int
main(void)162*5b46ea68Santon main(void)
1631338765aSotto {
1641338765aSotto 	regex_t re;
165f70ad59dSotto 	int e, ret = 0;
1661338765aSotto 	size_t i;
1671338765aSotto 
1681338765aSotto 	for (i = 0; i < sizeof(patterns) / sizeof(patterns[0]); i++) {
1691338765aSotto 		char *d = (*patterns[i])(9999);
1701338765aSotto 		e = regcomp(&re, d, i == 6 ? REG_BASIC : REG_EXTENDED);
1711338765aSotto 		free(d);
1721338765aSotto 		if (e) {
173f70ad59dSotto 			if (e != REG_ESPACE) {
1741338765aSotto 				printf("regcomp returned %d for pattern %zu", e, i);
175f70ad59dSotto 				ret = 1;
176f70ad59dSotto 			}
1771338765aSotto 			continue;
1781338765aSotto 		}
1791338765aSotto 		(void)regexec(&re, "aaaaaaaa", 0, NULL, 0);
1801338765aSotto 		regfree(&re);
1811338765aSotto 	}
182f70ad59dSotto 	return ret;
1831338765aSotto }
184