From: Alan T. DeKok Date: Tue, 29 Nov 2011 11:15:52 +0000 (+0100) Subject: Clean up regex code X-Git-Tag: release_3_0_0_beta0~458 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=5f18e49ac019190104f110999aa789dec9e6c5d5;p=thirdparty%2Ffreeradius-server.git Clean up regex code Convert it to use pairmake_xlat, which is better. Better ifdef's for regex support. boot-time check for invalid regular expressions --- diff --git a/src/lib/valuepair.c b/src/lib/valuepair.c index a2f1f0095c1..82c87e47b31 100644 --- a/src/lib/valuepair.c +++ b/src/lib/valuepair.c @@ -32,9 +32,11 @@ RCSID("$Id$") #endif #ifdef HAVE_PCREPOSIX_H +#define WITH_REGEX # include #else #ifdef HAVE_REGEX_H +#define WITH_REGEX # include #endif #endif @@ -508,50 +510,6 @@ void pairmove(VALUE_PAIR **to, VALUE_PAIR **from) continue; break; -/* really HAVE_REGEX_H */ -#if 0 - /* - * Attr-Name =~ "s/find/replace/" - * - * Very bad code. Barely working, - * if at all. - */ - - case T_OP_REG_EQ: - if (found && - (i->vp_strvalue[0] == 's')) { - regex_t reg; - regmatch_t match[1]; - - char *str; - char *p, *q; - - p = i->vp_strvalue + 1; - q = strchr(p + 1, *p); - if (!q || (q[strlen(q) - 1] != *p)) { - tailfrom = i; - continue; - } - str = strdup(i->vp_strvalue + 2); - q = strchr(str, *p); - *(q++) = '\0'; - q[strlen(q) - 1] = '\0'; - - regcomp(®, str, 0); - if (regexec(®, found->vp_strvalue, - 1, match, 0) == 0) { - fprintf(stderr, "\"%s\" will have %d to %d replaced with %s\n", - found->vp_strvalue, match[0].rm_so, - match[0].rm_eo, q); - - } - regfree(®); - free(str); - } - tailfrom = i; /* don't copy it over */ - continue; - break; -#endif case T_OP_EQ: /* = */ /* * FIXME: Tunnel attributes with @@ -1571,7 +1529,7 @@ VALUE_PAIR *pairmake(const char *attribute, const char *value, int operator) char *tc, *ts; signed char tag; int found_tag; - char buffer[64]; + char buffer[256]; const char *attrname = attribute; /* @@ -1679,6 +1637,11 @@ VALUE_PAIR *pairmake(const char *attribute, const char *value, int operator) */ case T_OP_REG_EQ: /* =~ */ case T_OP_REG_NE: /* !~ */ +#ifndef WITH_REGEX + fr_strerror_printf("Regular expressions are not supported"); + return NULL; + +#else if (!value) { fr_strerror_printf("No regular expression found in %s", vp->name); @@ -1687,7 +1650,22 @@ VALUE_PAIR *pairmake(const char *attribute, const char *value, int operator) } pairbasicfree(vp); + + if (1) { + int compare; + regex_t reg; + + compare = regcomp(®, value, REG_EXTENDED); + if (compare != 0) { + regerror(compare, ®, buffer, sizeof(buffer)); + fr_strerror_printf("Illegal regular expression in attribute: %s: %s", + attribute, buffer); + return NULL; + } + } + return pairmake_xlat(attribute, value, operator); +#endif } /* @@ -2054,7 +2032,7 @@ int paircmp(VALUE_PAIR *one, VALUE_PAIR *two) */ case T_OP_REG_EQ: case T_OP_REG_NE: -#ifndef HAVE_REGEX_H +#ifndef WITH_REGEX return -1; #else {