From 8006ee58b89e141ab27f5aadea0340df17548a68 Mon Sep 17 00:00:00 2001 From: Sergio Gelato Date: Sun, 6 Dec 2015 13:33:17 +0100 Subject: [PATCH] ITS#8204 Improved RFC2782 shuffle when several, but not all, records have weight 0. The fallback to a straight Fisher-Yates shuffle needs to occur whenever the sum of the *remaining* weights is zero, or else the remaining records will not be reordered. Testing only once at the beginning covers the case when all weights are zero, and obviously no shuffling is needed when only one weight is zero; but other weight combinations are possible, such as (1, 0, 0). --- libraries/libldap/dnssrv.c | 43 +++++++++++++++----------------------- 1 file changed, 17 insertions(+), 26 deletions(-) diff --git a/libraries/libldap/dnssrv.c b/libraries/libldap/dnssrv.c index 1503c068b6..69c9bb5ff0 100644 --- a/libraries/libldap/dnssrv.c +++ b/libraries/libldap/dnssrv.c @@ -216,36 +216,27 @@ static void srv_shuffle(srv_record *a, int n) { for (i=0; i1; a++, p--) { + if (!total) { + /* all remaining weights are zero, + do a straight Fisher-Yates shuffle */ + j = srv_rand() * p; + } else { + r = srv_rand() * total; + for (j=0; j