From 05cef96d9e63a00b319f88343cf9869c8e612843 Mon Sep 17 00:00:00 2001 From: Rich Felker Date: Mon, 22 Sep 2014 17:55:08 -0400 Subject: fix incorrect sequence generation in *rand48 prng functions patch by Jens Gustedt. this fixes a bug reported by Nadav Har'El. the underlying issue was that a left-shift by 16 bits after promotion of unsigned short to int caused integer overflow. while some compilers define this overflow case as "shifting into the sign bit", doing so doesn't help; the sign bit then gets extended through the upper bits in subsequent arithmetic as unsigned long long. this patch imposes a promotion to unsigned prior to the shift, so that the result is well-defined and matches the specified behavior. --- src/prng/__rand48_step.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/prng') diff --git a/src/prng/__rand48_step.c b/src/prng/__rand48_step.c index ccaffc37..961d30fc 100644 --- a/src/prng/__rand48_step.c +++ b/src/prng/__rand48_step.c @@ -3,8 +3,8 @@ uint64_t __rand48_step(unsigned short *xi, unsigned short *lc) { uint64_t a, x; - x = xi[0] | xi[1]<<16 | xi[2]+0ULL<<32; - a = lc[0] | lc[1]<<16 | lc[2]+0ULL<<32; + x = xi[0] | xi[1]+0U<<16 | xi[2]+0ULL<<32; + a = lc[0] | lc[1]+0U<<16 | lc[2]+0ULL<<32; x = a*x + lc[3]; xi[0] = x; xi[1] = x>>16; -- cgit v1.2.1