]> git.ipfire.org Git - thirdparty/openldap.git/commitdiff
ITS#8204 Remove bias towards the first record in RFC2782 shuffle implementation.
authorSergio Gelato <Sergio.Gelato@astro.su.se>
Sun, 6 Dec 2015 11:57:46 +0000 (12:57 +0100)
committerQuanah Gibson-Mount <quanah@openldap.org>
Mon, 22 Jun 2020 17:27:30 +0000 (17:27 +0000)
Prior to this change, given two records of weight 1 the algorithm would
return them in the order (0,1) with 100% probability instead of the
desired 50%. This was due to an off-by-one error in the range test.

srv_rand() returns a float in the range [0.0, 1.0[, so r is an integer in the
range [0, total[. The correct probability for record 0 to be chosen is
a[0].weight/total, not (a[0].weight+1)/total.

libraries/libldap/dnssrv.c

index 10f179dc2e23e5af14732512e2579f98ef29f9b4..1503c068b6b88d03303568ab718ec8fee9ba803b 100644 (file)
@@ -234,7 +234,7 @@ static void srv_shuffle(srv_record *a, int n) {
                r = srv_rand() * total;
                for (j=0; j<p; j++) {
                        r -= a[j].weight;
-                       if (r <= 0) {
+                       if (r < 0) {
                                if (j) {
                                        srv_record t = a[0];
                                        a[0] = a[j];