mirror of
https://github.com/golang/go
synced 2024-11-22 05:04:40 -07:00
math/big: Rand shouldn't hang if argument is also receiver.
Fixes #2607. R=rsc CC=golang-dev https://golang.org/cl/5489109
This commit is contained in:
parent
b6122b0a64
commit
fc78c5aa00
@ -9,6 +9,7 @@ import (
|
|||||||
"encoding/gob"
|
"encoding/gob"
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"math/rand"
|
||||||
"testing"
|
"testing"
|
||||||
"testing/quick"
|
"testing/quick"
|
||||||
)
|
)
|
||||||
@ -1405,3 +1406,9 @@ func TestIntGobEncoding(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestIssue2607(t *testing.T) {
|
||||||
|
// This code sequence used to hang.
|
||||||
|
n := NewInt(10)
|
||||||
|
n.Rand(rand.New(rand.NewSource(9)), n)
|
||||||
|
}
|
||||||
|
@ -1196,12 +1196,16 @@ func (x nat) powersOfTwoDecompose() (q nat, k int) {
|
|||||||
// random creates a random integer in [0..limit), using the space in z if
|
// random creates a random integer in [0..limit), using the space in z if
|
||||||
// possible. n is the bit length of limit.
|
// possible. n is the bit length of limit.
|
||||||
func (z nat) random(rand *rand.Rand, limit nat, n int) nat {
|
func (z nat) random(rand *rand.Rand, limit nat, n int) nat {
|
||||||
|
if alias(z, limit) {
|
||||||
|
z = nil // z is an alias for limit - cannot reuse
|
||||||
|
}
|
||||||
|
z = z.make(len(limit))
|
||||||
|
|
||||||
bitLengthOfMSW := uint(n % _W)
|
bitLengthOfMSW := uint(n % _W)
|
||||||
if bitLengthOfMSW == 0 {
|
if bitLengthOfMSW == 0 {
|
||||||
bitLengthOfMSW = _W
|
bitLengthOfMSW = _W
|
||||||
}
|
}
|
||||||
mask := Word((1 << bitLengthOfMSW) - 1)
|
mask := Word((1 << bitLengthOfMSW) - 1)
|
||||||
z = z.make(len(limit))
|
|
||||||
|
|
||||||
for {
|
for {
|
||||||
for i := range z {
|
for i := range z {
|
||||||
|
Loading…
Reference in New Issue
Block a user