From 8ab00b8fbc01315e36f6210e0ce1c3d5a356931f Mon Sep 17 00:00:00 2001 From: Ed Schouten Date: Tue, 6 Dec 2016 19:08:29 +0000 Subject: [PATCH] Properly sign extend the result of jrand48() and mrand48(). These functions are supposed to return a value between [_2^31, 2^31). This doesn't seem to work on 64-bit systems, where we return a value between [0, 3^32). Patch up the function to use proper casts to int32_t. While there, fix some other style bugs. MFC after: 2 weeks --- lib/libc/gen/jrand48.c | 5 ++++- lib/libc/gen/mrand48.c | 6 +++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/lib/libc/gen/jrand48.c b/lib/libc/gen/jrand48.c index 17076205559a..5aab1e1bec9e 100644 --- a/lib/libc/gen/jrand48.c +++ b/lib/libc/gen/jrand48.c @@ -14,11 +14,14 @@ #include __FBSDID("$FreeBSD$"); +#include + #include "rand48.h" long jrand48(unsigned short xseed[3]) { + _dorand48(xseed); - return ((long) xseed[2] << 16) + (long) xseed[1]; + return ((int32_t)(((uint32_t)xseed[2] << 16) | (uint32_t)xseed[1])); } diff --git a/lib/libc/gen/mrand48.c b/lib/libc/gen/mrand48.c index ef20fb87bf50..795a8585802d 100644 --- a/lib/libc/gen/mrand48.c +++ b/lib/libc/gen/mrand48.c @@ -14,6 +14,8 @@ #include __FBSDID("$FreeBSD$"); +#include + #include "rand48.h" extern unsigned short _rand48_seed[3]; @@ -21,6 +23,8 @@ extern unsigned short _rand48_seed[3]; long mrand48(void) { + _dorand48(_rand48_seed); - return ((long) _rand48_seed[2] << 16) + (long) _rand48_seed[1]; + return ((int32_t)(((uint32_t)_rand48_seed[2] << 16) | + (uint32_t)_rand48_seed[1])); }