[master] 365e02a Directors, like all other code, should handle NULL BACKEND arguments sanely, in this case by failing VCL initialization.
Poul-Henning Kamp
phk at FreeBSD.org
Wed Nov 15 09:42:05 UTC 2017
commit 365e02a800e149915260778b92685b99d507961f
Author: Poul-Henning Kamp <phk at FreeBSD.org>
Date: Wed Nov 15 09:40:24 2017 +0000
Directors, like all other code, should handle NULL BACKEND arguments
sanely, in this case by failing VCL initialization.
Fixes #2036
diff --git a/bin/varnishtest/tests/r02036.vtc b/bin/varnishtest/tests/r02036.vtc
new file mode 100644
index 0000000..f6bfcfc
--- /dev/null
+++ b/bin/varnishtest/tests/r02036.vtc
@@ -0,0 +1,29 @@
+varnishtest "NULL directors in vcl_init"
+
+server s1 {
+ rxreq
+ txresp
+} -start
+
+varnish v1 -vcl+backend {} -start
+
+varnish v1 -errvcl "NULL backend cannot be added" {
+ import directors;
+
+ probe cacheprobe {
+ .initial = 0;
+ }
+
+ backend b01 {
+ .host = "127.0.0.2";
+ .probe = cacheprobe;
+ }
+
+ sub vcl_init {
+ new hash = directors.hash();
+ hash.add_backend(b01,1.0);
+
+ new rr = directors.round_robin();
+ rr.add_backend(hash.backend("text"));
+ }
+}
diff --git a/lib/libvmod_directors/fall_back.c b/lib/libvmod_directors/fall_back.c
index 544c168..1f32615 100644
--- a/lib/libvmod_directors/fall_back.c
+++ b/lib/libvmod_directors/fall_back.c
@@ -122,7 +122,7 @@ vmod_fallback_add_backend(VRT_CTX,
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
CHECK_OBJ_NOTNULL(fb, VMOD_DIRECTORS_FALLBACK_MAGIC);
- (void)vdir_add_backend(fb->vd, be, 0.0);
+ vdir_add_backend(ctx, fb->vd, be, 0.0);
}
VCL_VOID __match_proto__()
@@ -131,7 +131,7 @@ vmod_fallback_remove_backend(VRT_CTX,
{
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
CHECK_OBJ_NOTNULL(fb, VMOD_DIRECTORS_FALLBACK_MAGIC);
- vdir_remove_backend(fb->vd, be, &fb->cur);
+ vdir_remove_backend(ctx, fb->vd, be, &fb->cur);
}
VCL_BACKEND __match_proto__()
diff --git a/lib/libvmod_directors/hash.c b/lib/libvmod_directors/hash.c
index a8961df..5e5fc97 100644
--- a/lib/libvmod_directors/hash.c
+++ b/lib/libvmod_directors/hash.c
@@ -81,7 +81,7 @@ vmod_hash_add_backend(VRT_CTX,
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
CHECK_OBJ_NOTNULL(rr, VMOD_DIRECTORS_HASH_MAGIC);
- (void)vdir_add_backend(rr->vd, be, w);
+ vdir_add_backend(ctx, rr->vd, be, w);
}
VCL_VOID __match_proto__()
@@ -91,7 +91,7 @@ vmod_hash_remove_backend(VRT_CTX,
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
CHECK_OBJ_NOTNULL(rr, VMOD_DIRECTORS_HASH_MAGIC);
- vdir_remove_backend(rr->vd, be, NULL);
+ vdir_remove_backend(ctx, rr->vd, be, NULL);
}
VCL_BACKEND __match_proto__()
diff --git a/lib/libvmod_directors/random.c b/lib/libvmod_directors/random.c
index 0608021..b2924db 100644
--- a/lib/libvmod_directors/random.c
+++ b/lib/libvmod_directors/random.c
@@ -109,7 +109,7 @@ vmod_random_add_backend(VRT_CTX,
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
CHECK_OBJ_NOTNULL(rr, VMOD_DIRECTORS_RANDOM_MAGIC);
- (void)vdir_add_backend(rr->vd, be, w);
+ vdir_add_backend(ctx, rr->vd, be, w);
}
VCL_VOID vmod_random_remove_backend(VRT_CTX,
@@ -117,7 +117,7 @@ VCL_VOID vmod_random_remove_backend(VRT_CTX,
{
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
CHECK_OBJ_NOTNULL(rr, VMOD_DIRECTORS_RANDOM_MAGIC);
- vdir_remove_backend(rr->vd, be, NULL);
+ vdir_remove_backend(ctx, rr->vd, be, NULL);
}
VCL_BACKEND __match_proto__()
diff --git a/lib/libvmod_directors/round_robin.c b/lib/libvmod_directors/round_robin.c
index 64ed115..8b4a92e 100644
--- a/lib/libvmod_directors/round_robin.c
+++ b/lib/libvmod_directors/round_robin.c
@@ -118,7 +118,7 @@ vmod_round_robin_add_backend(VRT_CTX,
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
CHECK_OBJ_NOTNULL(rr, VMOD_DIRECTORS_ROUND_ROBIN_MAGIC);
- (void)vdir_add_backend(rr->vd, be, 0.0);
+ vdir_add_backend(ctx, rr->vd, be, 0.0);
}
VCL_VOID __match_proto__()
@@ -127,7 +127,7 @@ vmod_round_robin_remove_backend(VRT_CTX,
{
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
CHECK_OBJ_NOTNULL(rr, VMOD_DIRECTORS_ROUND_ROBIN_MAGIC);
- vdir_remove_backend(rr->vd, be, NULL);
+ vdir_remove_backend(ctx, rr->vd, be, NULL);
}
VCL_BACKEND __match_proto__()
diff --git a/lib/libvmod_directors/vdir.c b/lib/libvmod_directors/vdir.c
index c286497..a334635 100644
--- a/lib/libvmod_directors/vdir.c
+++ b/lib/libvmod_directors/vdir.c
@@ -113,12 +113,16 @@ vdir_unlock(struct vdir *vd)
}
-unsigned
-vdir_add_backend(struct vdir *vd, VCL_BACKEND be, double weight)
+void
+vdir_add_backend(VRT_CTX, struct vdir *vd, VCL_BACKEND be, double weight)
{
unsigned u;
CHECK_OBJ_NOTNULL(vd, VDIR_MAGIC);
+ if (be == NULL) {
+ VRT_fail(ctx, "NULL backend cannot be added");
+ return;
+ }
AN(be);
vdir_wrlock(vd);
if (vd->n_backend >= vd->l_backend)
@@ -129,17 +133,18 @@ vdir_add_backend(struct vdir *vd, VCL_BACKEND be, double weight)
vd->weight[u] = weight;
vd->total_weight += weight;
vdir_unlock(vd);
- return (u);
}
void
-vdir_remove_backend(struct vdir *vd, VCL_BACKEND be, unsigned *cur)
+vdir_remove_backend(VRT_CTX, struct vdir *vd, VCL_BACKEND be, unsigned *cur)
{
unsigned u, n;
CHECK_OBJ_NOTNULL(vd, VDIR_MAGIC);
- if (be == NULL)
+ if (be == NULL) {
+ VRT_fail(ctx, "NULL backend cannot be removed");
return;
+ }
CHECK_OBJ(be, DIRECTOR_MAGIC);
vdir_wrlock(vd);
for (u = 0; u < vd->n_backend; u++)
diff --git a/lib/libvmod_directors/vdir.h b/lib/libvmod_directors/vdir.h
index 0bdfb04..c4862e5 100644
--- a/lib/libvmod_directors/vdir.h
+++ b/lib/libvmod_directors/vdir.h
@@ -47,8 +47,8 @@ void vdir_delete(struct vdir **vdp);
void vdir_rdlock(struct vdir *vd);
void vdir_wrlock(struct vdir *vd);
void vdir_unlock(struct vdir *vd);
-unsigned vdir_add_backend(struct vdir *, VCL_BACKEND be, double weight);
-void vdir_remove_backend(struct vdir *, VCL_BACKEND be, unsigned *cur);
+void vdir_add_backend(VRT_CTX, struct vdir *, VCL_BACKEND, double weight);
+void vdir_remove_backend(VRT_CTX, struct vdir *, VCL_BACKEND, unsigned *cur);
unsigned vdir_any_healthy(struct vdir *, const struct busyobj *,
double *changed);
VCL_BACKEND vdir_pick_be(struct vdir *, double w, const struct busyobj *);
More information about the varnish-commit
mailing list