]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: regex: Copy the original regex expression into string.
authorThierry FOURNIER <tfournier@exceliance.fr>
Fri, 6 Dec 2013 19:38:46 +0000 (20:38 +0100)
committerWilly Tarreau <w@1wt.eu>
Thu, 12 Dec 2013 14:43:34 +0000 (15:43 +0100)
This is useful for the debug or for search regex in maps.

include/common/regex.h
src/regex.c

index 9789ec3c9bea2eaa70c88b67e333f06eaf0a1bc1..a3d1b5f400513d71d5fd9b5a988fbbf77f0f0f49 100644 (file)
@@ -47,6 +47,7 @@ struct my_regex {
 #else /* no PCRE */
        regex_t regex;
 #endif
+       char *regstr; /* this contain the original string */
 };
 
 /* what to do when a header matches a regex */
@@ -108,6 +109,8 @@ static inline void regex_free(struct my_regex *preg) {
 #else
        regfree(&preg->regex);
 #endif
+       free(preg->regstr);
+       preg->regstr = NULL;
 }
 
 #endif /* _COMMON_REGEX_H */
index 7a7694050b41cd666f06c76bad1f3b89dc589ee9..64f93c30ecdf7b11382dc92d9462e94507383da2 100644 (file)
@@ -124,6 +124,13 @@ const char *chain_regex(struct hdr_exp **head, const regex_t *preg,
 
 int regex_comp(const char *str, struct my_regex *regex, int cs, int cap, char **err)
 {
+       /* copy the original regex format */
+       regex->regstr = strdup(str);
+       if (!regex->regstr) {
+               memprintf(err, "out of memory");
+               return 0;
+       }
+
 #ifdef USE_PCRE_JIT
        int flags = 0;
        const char *error;
@@ -136,12 +143,14 @@ int regex_comp(const char *str, struct my_regex *regex, int cs, int cap, char **
 
        regex->reg = pcre_compile(str, flags, &error, &erroffset, NULL);
        if (!regex->reg) {
+               free(regex->regstr);
                memprintf(err, "regex '%s' is invalid (error=%s, erroffset=%d)", str, error, erroffset);
                return 0;
        }
 
        regex->extra = pcre_study(regex->reg, PCRE_STUDY_JIT_COMPILE, &error);
        if (!regex->extra) {
+               free(regex->regstr);
                pcre_free(regex->reg);
                memprintf(err, "failed to compile regex '%s' (error=%s)", str, error);
                return 0;
@@ -155,6 +164,7 @@ int regex_comp(const char *str, struct my_regex *regex, int cs, int cap, char **
                flags |= REG_NOSUB;
 
        if (regcomp(&regex->regex, str, flags) != 0) {
+               free(regex->regstr);
                memprintf(err, "regex '%s' is invalid", str);
                return 0;
        }