From: Ondřej Kuzník Date: Fri, 16 Sep 2022 13:49:11 +0000 (+0100) Subject: ITS#8196/ITS#9714 Switch to xorshift X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=1220282dd5f941829e999d612eeb226e532b55d7;p=thirdparty%2Fopenldap.git ITS#8196/ITS#9714 Switch to xorshift --- diff --git a/libraries/libldap/dnssrv.c b/libraries/libldap/dnssrv.c index 433c37f5e0..4c78035777 100644 --- a/libraries/libldap/dnssrv.c +++ b/libraries/libldap/dnssrv.c @@ -184,22 +184,28 @@ typedef struct srv_record { char hostname[MAXHOST]; } srv_record; -/* Linear Congruential Generator - we don't need - * high quality randomness, and we don't want to +/* + * xorshift - we don't need high quality randomness, and we don't want to * interfere with anyone else's use of srand(). * - * The PRNG here cycles thru 941,955 numbers. + * The PRNG here cycles thru all nonzero 32bit numbers. */ -static float srv_seed; +static ac_uint4 srv_seed; -static void srv_srand(int seed) { - srv_seed = (float)seed / (float)RAND_MAX; +static void +srv_srand( int seed ) +{ + srv_seed = seed; } -static float srv_rand() { - float val = 9821.0 * srv_seed + .211327; - srv_seed = val - (int)val; - return srv_seed; +static ac_uint4 +srv_rand( void ) { + ac_uint4 val = srv_seed; + val ^= val << 13; + val ^= val >> 17; + val ^= val << 5; + srv_seed = val; + return val; } static int srv_cmp(const void *aa, const void *bb){ @@ -221,9 +227,11 @@ static void srv_shuffle(srv_record *a, int n) { if (!total) { /* all remaining weights are zero, do a straight Fisher-Yates shuffle */ - j = srv_rand() * p; + j = srv_rand() % p; } else { - r = srv_rand() * total; + /* Extra share for the first (possibly 0-weight entry), + * RFC2782 Errata (rejected) discusses the effects. */ + r = srv_rand() % (total+1); for (j=0; j