]> err.no Git - varnish/commitdiff
Fix an off-by one in the random director
authorphk <phk@d4fa192b-c00b-0410-8231-f00ffab90ce4>
Thu, 10 Jul 2008 18:09:42 +0000 (18:09 +0000)
committerphk <phk@d4fa192b-c00b-0410-8231-f00ffab90ce4>
Thu, 10 Jul 2008 18:09:42 +0000 (18:09 +0000)
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

index c6b80b6e8d0c42a5f2f9e84225df87cbbe877455..162a46a072b7868137ab39d1abd8d818e5bc879c 100644 (file)
@@ -35,6 +35,7 @@
 #include <sys/types.h>
 #include <sys/socket.h>
 
+#include <stdio.h>
 #include <errno.h>
 #include <stdlib.h>
 #include <string.h>
@@ -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;
 }