From a44b23ba63f2eef53add0e4531a9775f22da9c58 Mon Sep 17 00:00:00 2001 From: Stefan Berger Date: Sun, 4 Apr 2010 10:34:52 -0400 Subject: [PATCH] Get rid of the regular expressions when evaluating variable names and values. Rather use the strspn() function. Along with this cleanup the initialization function for the code that used the regular expression can also be removed. --- src/conf/nwfilter_conf.c | 5 +--- src/conf/nwfilter_conf.h | 3 --- src/conf/nwfilter_params.c | 50 +++++++++++++------------------------- src/conf/nwfilter_params.h | 6 +++++ 4 files changed, 24 insertions(+), 40 deletions(-) diff --git a/src/conf/nwfilter_conf.c b/src/conf/nwfilter_conf.c index 7c71ece18b..c8dda044e5 100644 --- a/src/conf/nwfilter_conf.c +++ b/src/conf/nwfilter_conf.c @@ -2635,16 +2635,13 @@ int virNWFilterConfLayerInit(virHashIterator domUpdateCB) if (virMutexInit(&updateMutex)) return 1; - if (virNWFilterParamConfLayerInit()) - return 1; - return 0; } void virNWFilterConfLayerShutdown(void) { - virNWFilterParamConfLayerShutdown(); + virMutexDestroy(&updateMutex); } diff --git a/src/conf/nwfilter_conf.h b/src/conf/nwfilter_conf.h index bac67efc45..8ab0b89664 100644 --- a/src/conf/nwfilter_conf.h +++ b/src/conf/nwfilter_conf.h @@ -568,9 +568,6 @@ void virNWFilterPoolObjUnlock(virNWFilterPoolObjPtr obj); int virNWFilterConfLayerInit(virHashIterator domUpdateCB); void virNWFilterConfLayerShutdown(void); -int virNWFilterParamConfLayerInit(void); -void virNWFilterParamConfLayerShutdown(void); - # define virNWFilterReportError(conn, code, fmt...) \ (void)conn; \ virReportErrorHelper(NULL, VIR_FROM_NWFILTER, code, __FILE__, \ diff --git a/src/conf/nwfilter_params.c b/src/conf/nwfilter_params.c index 286c5ef1bf..a1c30bb751 100644 --- a/src/conf/nwfilter_params.c +++ b/src/conf/nwfilter_params.c @@ -22,8 +22,6 @@ #include -#include - #include "internal.h" #include "memory.h" @@ -35,13 +33,6 @@ #define VIR_FROM_THIS VIR_FROM_NWFILTER -/* - * regular expressions for parameter names and values - */ -static regex_t regex_nam; -static regex_t regex_val; - - static void hashDealloc(void *payload, const char *name ATTRIBUTE_UNUSED) { @@ -215,6 +206,21 @@ err_exit: #ifndef PROXY + +static bool +isValidVarName(const char *var) +{ + return var[strspn(var, VALID_VARNAME)] == 0; +} + + +static bool +isValidVarValue(const char *value) +{ + return value[strspn(value, VALID_VARVALUE)] == 0; +} + + virNWFilterHashTablePtr virNWFilterParseParamAttributes(xmlNodePtr cur) { @@ -234,9 +240,9 @@ virNWFilterParseParamAttributes(xmlNodePtr cur) nam = virXMLPropString(cur, "name"); val = virXMLPropString(cur, "value"); if (nam != NULL && val != NULL) { - if (regexec(®ex_nam, nam, 0, NULL, 0) != 0) + if (!isValidVarName(nam)) goto skip_entry; - if (regexec(®ex_val, val, 0, NULL, 0) != 0) + if (!isValidVarValue(nam)) goto skip_entry; if (virNWFilterHashTablePut(table, nam, val, 1)) { VIR_FREE(nam); @@ -296,25 +302,3 @@ virNWFilterFormatParamAttributes(virNWFilterHashTablePtr table, return virBufferContentAndReset(&buf); } - - -int virNWFilterParamConfLayerInit(void) { - - if (regcomp(®ex_nam, "^[a-zA-Z0-9_]+$" , - REG_NOSUB|REG_EXTENDED) != 0) - return 1; - - if (regcomp(®ex_val, "^[a-zA-Z0-9_.:]+$", - REG_NOSUB|REG_EXTENDED) != 0) { - regfree(®ex_nam); - return 1; - } - - return 0; -} - - -void virNWFilterParamConfLayerShutdown(void) { - regfree(®ex_nam); - regfree(®ex_val); -} diff --git a/src/conf/nwfilter_params.h b/src/conf/nwfilter_params.h index 5b4afba609..dd9750e676 100644 --- a/src/conf/nwfilter_params.h +++ b/src/conf/nwfilter_params.h @@ -50,4 +50,10 @@ int virNWFilterHashTablePutAll(virConnectPtr conn, virNWFilterHashTablePtr src, virNWFilterHashTablePtr dest); +# define VALID_VARNAME \ + "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_" + +# define VALID_VARVALUE \ + "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_.:" + #endif /* NWFILTER_PARAMS_H */