• 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
util_test.go 3.08 KB