• Wade Simmons's avatar
    crypto/rand: only read necessary bytes for Int · 8a2553e3
    Wade Simmons authored
    We only need to read the number of bytes required to store the value
    "max - 1" to generate a random number in the range [0, max).
    
    Before, there was an off-by-one error where an extra byte was read from
    the io.Reader for inputs like "256" (right at the boundary for a byte).
    There was a similar off-by-one error in the logic for clearing bits and
    thus for any input that was a power of 2, there was a 50% chance the
    read would continue to be retried as the mask failed to remove a bit.
    
    Fixes #18165.
    
    Change-Id: I548c1368990e23e365591e77980e9086fafb6518
    Reviewed-on: https://go-review.googlesource.com/43891Reviewed-by: 's avatarBrad Fitzpatrick <bradfitz@golang.org>
    Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
    TryBot-Result: Gobot Gobot <gobot@golang.org>
    8a2553e3
Name
Last commit
Last update
..
aes Loading commit data...
cipher Loading commit data...
des Loading commit data...
dsa Loading commit data...
ecdsa Loading commit data...
elliptic Loading commit data...
hmac Loading commit data...
internal/cipherhw Loading commit data...
md5 Loading commit data...
rand Loading commit data...
rc4 Loading commit data...
rsa Loading commit data...
sha1 Loading commit data...
sha256 Loading commit data...
sha512 Loading commit data...
subtle Loading commit data...
tls Loading commit data...
x509 Loading commit data...
crypto.go Loading commit data...