[master] 520cd8af2 vre: Replace VRE_exec() with a simpler VRE_match()
Dridi Boukelmoune
dridi.boukelmoune at gmail.com
Mon Jul 5 15:49:05 UTC 2021
commit 520cd8af2a33a9e7cab2722e535fe1d1aa522191
Author: Dridi Boukelmoune <dridi.boukelmoune at gmail.com>
Date: Fri Jun 18 17:33:17 2021 +0200
vre: Replace VRE_exec() with a simpler VRE_match()
With the introduction of VRE_sub() many VRE_exec() use cases went away:
- VRE_NOTEMPTY flag
- start offset
- ovector for capture groups
The subject string length is now optional and zero can be passed to signal
that the subject string is null-terminated.
There are no options for VRE_match() but an option argument is kept in
case we start exposing some in the future.
diff --git a/bin/varnishd/cache/cache_vrt_re.c b/bin/varnishd/cache/cache_vrt_re.c
index 886c35a8b..bf58e1f71 100644
--- a/bin/varnishd/cache/cache_vrt_re.c
+++ b/bin/varnishd/cache/cache_vrt_re.c
@@ -70,7 +70,7 @@ VRT_re_match(VRT_CTX, const char *s, VCL_REGEX re)
if (s == NULL)
s = "";
AN(re);
- i = VRE_exec(re, s, strlen(s), 0, 0, NULL, 0, &cache_param->vre_limits);
+ i = VRE_match(re, s, 0, 0, &cache_param->vre_limits);
if (i >= 0)
return (1);
if (i < VRE_ERROR_NOMATCH )
diff --git a/bin/varnishtest/vtc_haproxy.c b/bin/varnishtest/vtc_haproxy.c
index 89be37b43..f54b9c04b 100644
--- a/bin/varnishtest/vtc_haproxy.c
+++ b/bin/varnishtest/vtc_haproxy.c
@@ -323,7 +323,7 @@ cmd_haproxy_cli_expect(CMD_ARGS)
vtc_fatal(hc->vl, "CLI regexp error: '%s' (@%d) (%s)",
error, erroroffset, spec);
- i = VRE_exec(vre, hc->rxbuf, strlen(hc->rxbuf), 0, 0, NULL, 0, 0);
+ i = VRE_match(vre, hc->rxbuf, 0, 0, NULL);
VRE_free(&vre);
diff --git a/bin/varnishtest/vtc_logexp.c b/bin/varnishtest/vtc_logexp.c
index ba7818555..152144021 100644
--- a/bin/varnishtest/vtc_logexp.c
+++ b/bin/varnishtest/vtc_logexp.c
@@ -394,8 +394,7 @@ logexp_match(const struct logexp *le, struct logexp_test *test,
if (test->vre &&
test->tag >= 0 &&
test->tag == tag &&
- VRE_ERROR_NOMATCH == VRE_exec(test->vre, data,
- len, 0, 0, NULL, 0, NULL))
+ VRE_ERROR_NOMATCH == VRE_match(test->vre, data, len, 0, NULL))
ok = 0;
alt = (test->skip_max == LE_ALT);
diff --git a/bin/varnishtest/vtc_misc.c b/bin/varnishtest/vtc_misc.c
index 6c3288d97..a2b2038c7 100644
--- a/bin/varnishtest/vtc_misc.c
+++ b/bin/varnishtest/vtc_misc.c
@@ -193,10 +193,8 @@ cmd_shell_engine(struct vtclog *vl, int ok, const char *cmd,
else
vtc_log(vl, 4, "shell_expect found");
} else if (vre != NULL) {
- if (VRE_exec(vre, VSB_data(vsb), VSB_len(vsb), 0, 0,
- NULL, 0, NULL) < 1)
- vtc_fatal(vl,
- "shell_match failed: (\"%s\")", re);
+ if (VRE_match(vre, VSB_data(vsb), VSB_len(vsb), 0, NULL) < 1)
+ vtc_fatal(vl, "shell_match failed: (\"%s\")", re);
else
vtc_log(vl, 4, "shell_match succeeded");
VRE_free(&vre);
diff --git a/bin/varnishtest/vtc_subr.c b/bin/varnishtest/vtc_subr.c
index 3eb60c100..7d5607cd0 100644
--- a/bin/varnishtest/vtc_subr.c
+++ b/bin/varnishtest/vtc_subr.c
@@ -103,7 +103,7 @@ vtc_expect(struct vtclog *vl,
if (vre == NULL)
vtc_fatal(vl, "REGEXP error: %s (@%d) (%s)",
error, erroroffset, rhs);
- i = VRE_exec(vre, lhs, strlen(lhs), 0, 0, NULL, 0, 0);
+ i = VRE_match(vre, lhs, 0, 0, NULL);
retval = (i >= 0 && *cmp == '~') || (i < 0 && *cmp == '!');
VRE_free(&vre);
} else if (!strcmp(cmp, "==")) {
diff --git a/bin/varnishtest/vtc_syslog.c b/bin/varnishtest/vtc_syslog.c
index 403a394ab..221a5b6f5 100644
--- a/bin/varnishtest/vtc_syslog.c
+++ b/bin/varnishtest/vtc_syslog.c
@@ -411,7 +411,7 @@ cmd_syslog_expect(CMD_ARGS)
vtc_fatal(s->vl, "REGEXP error: '%s' (@%d) (%s)",
error, erroroffset, spec);
- i = VRE_exec(vre, s->rxbuf, strlen(s->rxbuf), 0, 0, NULL, 0, 0);
+ i = VRE_match(vre, s->rxbuf, 0, 0, NULL);
VRE_free(&vre);
diff --git a/bin/varnishtest/vtc_varnish.c b/bin/varnishtest/vtc_varnish.c
index 3612aaa9c..97a876876 100644
--- a/bin/varnishtest/vtc_varnish.c
+++ b/bin/varnishtest/vtc_varnish.c
@@ -755,7 +755,7 @@ varnish_cli(struct varnish *v, const char *cli, unsigned exp, const char *re)
if (exp != 0 && exp != (unsigned)u)
vtc_fatal(v->vl, "FAIL CLI response %u expected %u", u, exp);
if (vre != NULL) {
- err = VRE_exec(vre, resp, strlen(resp), 0, 0, NULL, 0, NULL);
+ err = VRE_match(vre, resp, 0, 0, NULL);
if (err < 1)
vtc_fatal(v->vl, "Expect failed (%d)", err);
VRE_free(&vre);
diff --git a/include/vre.h b/include/vre.h
index 27158663a..488791daf 100644
--- a/include/vre.h
+++ b/include/vre.h
@@ -53,12 +53,10 @@ typedef struct vre vre_t;
/* And those to PCRE options */
extern const unsigned VRE_has_jit;
extern const unsigned VRE_CASELESS;
-extern const unsigned VRE_NOTEMPTY;
vre_t *VRE_compile(const char *, unsigned, const char **, int *);
-int VRE_exec(const vre_t *code, const char *subject, int length,
- int startoffset, int options, int *ovector, int ovecsize,
- const volatile struct vre_limits *lim);
+int VRE_match(const vre_t *code, const char *subject, size_t length,
+ int options, const volatile struct vre_limits *lim);
int VRE_sub(const vre_t *code, const char *subject, const char *replacement,
struct vsb *vsb, const volatile struct vre_limits *lim, int all);
void VRE_free(vre_t **);
diff --git a/lib/libvarnish/vre.c b/lib/libvarnish/vre.c
index e7a15946d..5dae04f97 100644
--- a/lib/libvarnish/vre.c
+++ b/lib/libvarnish/vre.c
@@ -69,15 +69,14 @@ struct vre {
* here.
*/
const unsigned VRE_CASELESS = PCRE_CASELESS;
-const unsigned VRE_NOTEMPTY = PCRE_NOTEMPTY;
/*
* Even though we only have one for each case so far, keep track of masks
- * to differentiate between compile and exec options and enfore the hard
+ * to differentiate between compile and match options and enfore the hard
* VRE linkage.
*/
#define VRE_MASK_COMPILE PCRE_CASELESS
-#define VRE_MASK_EXEC PCRE_NOTEMPTY
+#define VRE_MASK_MATCH 0
vre_t *
VRE_compile(const char *pattern, unsigned options,
@@ -115,18 +114,14 @@ VRE_compile(const char *pattern, unsigned options,
return (v);
}
-int
-VRE_exec(const vre_t *code, const char *subject, int length,
+static int
+vre_exec(const vre_t *code, const char *subject, int length,
int startoffset, int options, int *ovector, int ovecsize,
const volatile struct vre_limits *lim)
{
- CHECK_OBJ_NOTNULL(code, VRE_MAGIC);
- int ov[30];
- if (ovector == NULL) {
- ovector = ov;
- ovecsize = sizeof(ov)/sizeof(ov[0]);
- }
+ CHECK_OBJ_NOTNULL(code, VRE_MAGIC);
+ AN(ovector);
if (lim != NULL) {
/* XXX: not reentrant */
@@ -139,11 +134,27 @@ VRE_exec(const vre_t *code, const char *subject, int length,
code->re_extra->flags &= ~PCRE_EXTRA_MATCH_LIMIT_RECURSION;
}
- AZ(options & (~VRE_MASK_EXEC));
return (pcre_exec(code->re, code->re_extra, subject, length,
startoffset, options, ovector, ovecsize));
}
+int
+VRE_match(const vre_t *code, const char *subject, size_t length,
+ int options, const volatile struct vre_limits *lim)
+{
+ int ovector[30];
+
+ CHECK_OBJ_NOTNULL(code, VRE_MAGIC);
+ AN(subject);
+ AZ(options & (~VRE_MASK_MATCH));
+
+ if (length == 0)
+ length = strlen(subject);
+
+ return (vre_exec(code, subject, length, 0, options,
+ ovector, 30, lim));
+}
+
int
VRE_sub(const vre_t *code, const char *subject, const char *replacement,
struct vsb *vsb, const volatile struct vre_limits *lim, int all)
@@ -152,7 +163,6 @@ VRE_sub(const vre_t *code, const char *subject, const char *replacement,
int i, l;
const char *s;
unsigned x;
- int options = 0;
int offset = 0;
size_t len;
@@ -163,7 +173,7 @@ VRE_sub(const vre_t *code, const char *subject, const char *replacement,
memset(ovector, 0, sizeof(ovector));
len = strlen(subject);
- i = VRE_exec(code, subject, len, 0, options, ovector, 30, lim);
+ i = vre_exec(code, subject, len, 0, 0, ovector, 30, lim);
if (i <= VRE_ERROR_NOMATCH)
return (i);
@@ -189,9 +199,8 @@ VRE_sub(const vre_t *code, const char *subject, const char *replacement,
if (!all)
break;
memset(ovector, 0, sizeof(ovector));
- options |= VRE_NOTEMPTY;
- i = VRE_exec(code, subject, len, offset, options, ovector, 30,
- lim);
+ i = vre_exec(code, subject, len, offset, PCRE_NOTEMPTY,
+ ovector, 30, lim);
if (i < VRE_ERROR_NOMATCH )
return (i);
} while (i != VRE_ERROR_NOMATCH);
diff --git a/lib/libvarnishapi/vsl.c b/lib/libvarnishapi/vsl.c
index 681c69985..7fadba364 100644
--- a/lib/libvarnishapi/vsl.c
+++ b/lib/libvarnishapi/vsl.c
@@ -173,7 +173,7 @@ vsl_match_IX(struct VSL_data *vsl, const vslf_list *list,
CHECK_OBJ_NOTNULL(vslf, VSLF_MAGIC);
if (vslf->tags != NULL && !vbit_test(vslf->tags, tag))
continue;
- if (VRE_exec(vslf->vre, cdata, len, 0, 0, NULL, 0, NULL) >= 0)
+ if (VRE_match(vslf->vre, cdata, len, 0, NULL) >= 0)
return (1);
}
return (0);
diff --git a/lib/libvarnishapi/vsl_query.c b/lib/libvarnishapi/vsl_query.c
index 9a8fa8283..adcb77957 100644
--- a/lib/libvarnishapi/vsl_query.c
+++ b/lib/libvarnishapi/vsl_query.c
@@ -264,13 +264,13 @@ vslq_test_rec(const struct vex *vex, const struct VSLC_ptr *rec)
return (0);
case '~': /* ~ */
assert(rhs->type == VEX_REGEX && rhs->val_regex != NULL);
- i = VRE_exec(rhs->val_regex, b, e - b, 0, 0, NULL, 0, NULL);
+ i = VRE_match(rhs->val_regex, b, e - b, 0, NULL);
if (i != VRE_ERROR_NOMATCH)
return (1);
return (0);
case T_NOMATCH: /* !~ */
assert(rhs->type == VEX_REGEX && rhs->val_regex != NULL);
- i = VRE_exec(rhs->val_regex, b, e - b, 0, 0, NULL, 0, NULL);
+ i = VRE_match(rhs->val_regex, b, e - b, 0, NULL);
if (i == VRE_ERROR_NOMATCH)
return (1);
return (0);
More information about the varnish-commit
mailing list