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

Stefan Berger stefanb at linux.vnet.ibm.com
Tue Dec 22 02:46:29 AEDT 2015


On 12/17/2015 03:18 PM, Thomas Huth wrote:
> 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);
>   }

Reviewed-by: Stefan Berger <stefanb at linux.vnet.ibm.com>



More information about the SLOF mailing list