From ff6730ab7a0d17dd4ae29fc8671545a357b73b6d Mon Sep 17 00:00:00 2001 From: phk Date: Thu, 10 Jul 2008 18:09:42 +0000 Subject: [PATCH] Fix an off-by one in the random director git-svn-id: svn+ssh://projects.linpro.no/svn/varnish/trunk@2919 d4fa192b-c00b-0410-8231-f00ffab90ce4 --- varnish-cache/bin/varnishd/cache_dir_random.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/varnish-cache/bin/varnishd/cache_dir_random.c b/varnish-cache/bin/varnishd/cache_dir_random.c index c6b80b6e..162a46a0 100644 --- a/varnish-cache/bin/varnishd/cache_dir_random.c +++ b/varnish-cache/bin/varnishd/cache_dir_random.c @@ -35,6 +35,7 @@ #include #include +#include #include #include #include @@ -74,7 +75,7 @@ vdi_random_choose(struct sess *sp) r = random(); r &= 0x7fffffff; - for (vh = vs->hosts; i < vs->nhosts; vh++) + for (i = 0, vh = vs->hosts; i < vs->nhosts; vh++) if (r < vh->weight) return (vh->backend); assert(0 == __LINE__); @@ -136,7 +137,7 @@ VRT_init_dir_random(struct cli *cli, struct director **bp, const struct vrt_dir_ i = 0; a = 0.0; assert(s > 0.0); - for (te = t->members; te->host != NULL; te++, i++) { + for (te = t->members; i < t->nmember; te++, i++) { /* First normalize the specified weight in FP */ b = te->weight / s; /*lint !e795 not zero division */ /* Then accumulate to eliminate rounding errors */ @@ -145,5 +146,6 @@ VRT_init_dir_random(struct cli *cli, struct director **bp, const struct vrt_dir_ v = (unsigned)((1U<<31) * a); vs->hosts[i].weight = v; } + assert(vs->hosts[t->nmember - 1].weight > 0x7fffffff); *bp = &vs->dir; } -- 2.39.5