]> CyberLeo.Net >> Repos - FreeBSD/stable/9.git/commit
1) POSIX requires rand(3) return values to be in the [0, RAND_MAX]
authorache <ache@ccf9f872-aa2e-dd11-9fc8-001c23d0bc1f>
Wed, 24 Jul 2013 10:12:50 +0000 (10:12 +0000)
committerache <ache@ccf9f872-aa2e-dd11-9fc8-001c23d0bc1f>
Wed, 24 Jul 2013 10:12:50 +0000 (10:12 +0000)
commitfb4d662178bdc77b00f001d9c20a4a82e96b7fd4
tree6b6f6350b6b72327265f569cf330f4f0dfa93af7
parent49942f11556e355c222c0b6b14e78d02d5bb21df
1) POSIX requires rand(3) return values to be in the [0, RAND_MAX]
range, but ACM formula we use have internal state (and return value) in
the [1, 0x7ffffffe] range, so our RAND_MAX (0x7fffffff) is never reached
because it is off by one, zero is not reached too.

Correct both RAND_MAX and rand(3) return value, shifting last one
to the 0 by 1 subtracted, resulting POSIXed [0, 0x7ffffffd(=new
RAND_MAX)] range.

2) Add a checks for not overflowing on too big seeds. It may happens on
the machines, where sizeof(unsigned int) > 32 bits.

This change is binary compatible because range is reduced, not expanded,
so no bump is needed.

Reviewed by:    bde
Approved by:    re (glebius)

git-svn-id: svn://svn.freebsd.org/base/stable/9@253607 ccf9f872-aa2e-dd11-9fc8-001c23d0bc1f
include/stdlib.h
lib/libc/stdlib/rand.c