[SLOF] [PATCH 2/7] libc: Fix the rand() function to return non-zero values

Thomas Huth thuth at redhat.com
Fri Dec 18 07:18:51 AEDT 2015


The rand() function in SLOF's libc has a bug which caused the
function to always return zero: The _rand value was shifted left
by 16, and then ANDed with 0x7fff. Obviously, the shift operation
should be ">>" instead of "<<".
And while we're at it, also increase the constant for the
multiplaction in there so that more upper bits are affected.

Signed-off-by: Thomas Huth <thuth at redhat.com>
---
 lib/libc/stdlib/rand.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/lib/libc/stdlib/rand.c b/lib/libc/stdlib/rand.c
index 87e3efd..98c7e6d 100644
--- a/lib/libc/stdlib/rand.c
+++ b/lib/libc/stdlib/rand.c
@@ -18,7 +18,7 @@ static unsigned long _rand = 1;
 int
 rand(void)
 {
-	_rand = _rand * 25364735 + 34563;
+	_rand = _rand * 1237732973 + 34563;
 
-	return ((unsigned int) (_rand << 16) & RAND_MAX);
+	return ((unsigned int) (_rand >> 16) & RAND_MAX);
 }
-- 
1.8.3.1



More information about the SLOF mailing list