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