Commit 9d412a66 authored by Geoff Simmons's avatar Geoff Simmons

Error handling in set.string(), .backend() and .sub()

Ensure that the retrieved object was saved via set.add().
parent a5bb7641
Pipeline #373 skipped
......@@ -76,6 +76,8 @@ struct task_set_match {
size_t nmatches;
};
static const char null[] = "NULL";
static inline int
decimal_digits(int n)
{
......@@ -510,8 +512,10 @@ vmod_set_sub(VRT_CTX, struct vmod_re2_set *set, VCL_STRING text,
if (idx < 0)
return NULL;
if (!vbit_test(set->added[RE_ADDED], idx)) {
VERR(ctx, "%s.sub(%s, %s, %lld): Pattern %lld was not added",
set->vcl_name, text, rewrite, n, idx);
if (selects == NULL)
selects = null;
VERR(ctx, "%s.sub(%s, %s, %lld, %s): Pattern %d was not saved",
set->vcl_name, text, rewrite, n, selects, idx + 1);
return NULL;
}
return vmod_regex_sub(ctx, set->regex[idx], text, rewrite, fallback);
......@@ -533,6 +537,13 @@ vmod_set_string(VRT_CTX, struct vmod_re2_set *set, VCL_INT n, VCL_ENUM selects)
idx = get_match_idx(ctx, set, n, selects, "string");
if (idx < 0)
return NULL;
if (!vbit_test(set->added[STR_ADDED], idx)) {
if (selects == NULL)
selects = null;
VERR(ctx, "%s.string(%lld, %s): String %lld was not added",
set->vcl_name, n, selects, idx + 1);
return NULL;
}
return set->string[idx];
}
......@@ -552,5 +563,12 @@ vmod_set_backend(VRT_CTX, struct vmod_re2_set *set, VCL_INT n, VCL_ENUM selects)
idx = get_match_idx(ctx, set, n, selects, "backend");
if (idx < 0)
return NULL;
if (!vbit_test(set->added[BE_ADDED], idx)) {
if (selects == NULL)
selects = null;
VERR(ctx, "%s.backend(%lld, %s): Backend %lld was not added",
set->vcl_name, n, selects, idx + 1);
return NULL;
}
return set->backend[idx];
}
......@@ -211,3 +211,68 @@ client c1 {
} -run
logexpect l1 -wait
varnish v1 -vcl {
import ${vmod_re2};
backend be { .host = "${bad_ip}"; }
sub vcl_init {
new s = re2.set();
s.add("foo");
s.add("bar", "baz");
s.compile();
new b = re2.set();
b.add("foo", backend=be);
b.add("bar");
b.compile();
}
sub vcl_recv {
return(synth(200));
}
sub vcl_synth {
set resp.http.s-str-1 = s.string(1);
set resp.http.s-str-2 = s.string(2);
set resp.http.s-match = s.match("foobar");
set resp.http.s-first = s.string(select=FIRST);
set resp.http.s-last = s.string(select=LAST);
set resp.http.b-backend-1 = b.backend(1);
set resp.http.b-backend-2 = b.backend(2);
set resp.http.b-match = b.match("foobar");
set resp.http.b-first = b.backend(select=FIRST);
set resp.http.b-last = b.backend(select=LAST);
}
}
logexpect l1 -v v1 -d 0 -g vxid -q "VCL_Error" {
expect 0 * Begin req
expect * = VCL_Error "^vmod re2 error: s.string.1, NULL.: String 1 was not added$"
expect * = VCL_Error "^vmod re2 error: s.string.0, FIRST.: String 1 was not added$"
expect * = VCL_Error "^vmod re2 error: b.backend.2, NULL.: Backend 2 was not added$"
expect * = VCL_Error "^vmod re2 error: b.backend.0, LAST.: Backend 2 was not added$"
expect * = End
} -start
client c1 {
txreq
rxresp
expect resp.status == 200
expect resp.http.s-str-1 == ""
expect resp.http.s-str-2 == "baz"
expect resp.http.s-match == "true"
expect resp.http.s-first == resp.http.s-str-1
expect resp.http.s-last == resp.http.s-str-2
expect resp.http.b-backend-1 == "be"
expect resp.http.b-backend-2 == ""
expect resp.http.b-match == "true"
expect resp.http.b-first == resp.http.b-backend-1
expect resp.http.b-last == resp.http.b-backend-2
} -run
logexpect l1 -wait
......@@ -12,6 +12,11 @@ varnish v1 -vcl {
s.add("\w+", save=true);
s.add("b", save=true);
s.compile();
new n = re2.set();
n.add("b");
n.add("b+", save=true);
n.compile();
}
sub vcl_recv {
......@@ -19,19 +24,26 @@ varnish v1 -vcl {
}
sub vcl_synth {
set resp.http.sub-1
set resp.http.s-sub-1
= s.sub("the quick brown fox jumps over the lazy dogs.",
"\2\1ay", n=1);
set resp.http.sub-2 = s.sub("abcd.efghi@google.com",
"\0-NOSPAM", n=2);
set resp.http.sub-3 = s.sub("ababababab", "bb", n=3);
set resp.http.match
set resp.http.s-sub-2 = s.sub("abcd.efghi@google.com",
"\0-NOSPAM", n=2);
set resp.http.s-sub-3 = s.sub("ababababab", "bb", n=3);
set resp.http.s-match
= s.match("the quick brown fox jumps over the lazy dogs.");
set resp.http.n = s.nmatches();
set resp.http.pangram
set resp.http.s-n = s.nmatches();
set resp.http.s-pangram
= s.sub("the quick brown fox jumps over the lazy dogs.",
"\2\1ay", select=FIRST);
set resp.http.ab = s.sub("ababababab", "bb", select=LAST);
set resp.http.s-ab = s.sub("ababababab", "bb", select=LAST);
set resp.http.n-sub-1 = n.sub("bbbbbb", "bb", n=1);
set resp.http.n-sub-2 = n.sub("bbbbbb", "bb", n=2);
set resp.http.n-match = n.match("bbbbbb");
set resp.http.n-n = n.nmatches();
set resp.http.n-first = n.sub("bbbbbb", "bb", select=FIRST);
set resp.http.n-last = n.sub("bbbbbb", "bb", select=LAST);
}
} -start
......@@ -39,11 +51,26 @@ client c1 {
txreq
rxresp
expect resp.status == 200
expect resp.http.sub-1 == "ethay quick brown fox jumps over the lazy dogs."
expect resp.http.sub-2 == "abcd-NOSPAM.efghi@google.com"
expect resp.http.sub-3 == "abbabababab"
expect resp.http.match == "true"
expect resp.http.n == "3"
expect resp.http.pangram == resp.http.sub-1
expect resp.http.ab == resp.http.sub-3
expect resp.http.s-sub-1 == "ethay quick brown fox jumps over the lazy dogs."
expect resp.http.s-sub-2 == "abcd-NOSPAM.efghi@google.com"
expect resp.http.s-sub-3 == "abbabababab"
expect resp.http.s-match == "true"
expect resp.http.s-n == "3"
expect resp.http.s-pangram == resp.http.s-sub-1
expect resp.http.s-ab == resp.http.s-sub-3
expect resp.http.n-sub-1 == ""
expect resp.http.n-sub-2 == "bb"
expect resp.http.n-match == "true"
expect resp.http.n-n == "2"
expect resp.http.n-first == resp.http.n-sub-1
expect resp.http.n-last == resp.http.n-sub-2
} -run
logexpect l1 -v v1 -d 1 -g vxid -q "VCL_Error" {
expect 0 * Begin req
expect * = VCL_Error "^vmod re2 error: n.sub.bbbbbb, bb, 1, NULL.: Pattern 1 was not saved$"
expect * = VCL_Error "^vmod re2 error: n.sub.bbbbbb, bb, 0, FIRST.: Pattern 1 was not saved$"
expect * = End
} -run
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment