Commit ca6de008 authored by Roger Peppe's avatar Roger Peppe Committed by Robert Griesemer

math/big: fix destination leak into result value

This code would panic:
z := big.NewInt(1)
z.SetBit(big.NewInt(0), 2, 1)
if z.Cmp(big.NewInt(1<<2)) != 0 {
        panic("fail")
}

R=rsc, gri
CC=golang-dev
https://golang.org/cl/5437081
parent 8dce57e1
...@@ -1242,10 +1242,14 @@ func TestBitSet(t *testing.T) { ...@@ -1242,10 +1242,14 @@ func TestBitSet(t *testing.T) {
x.SetString(test.x, 0) x.SetString(test.x, 0)
b := x.Bit(test.i) b := x.Bit(test.i)
if b != test.b { if b != test.b {
t.Errorf("#%d got %v want %v", i, b, test.b)
t.Errorf("#%d want %v got %v", i, test.b, b)
} }
} }
z := NewInt(1)
z.SetBit(NewInt(0), 2, 1)
if z.Cmp(NewInt(4)) != 0 {
t.Errorf("destination leaked into result; got %s want 4", z)
}
} }
func BenchmarkBitset(b *testing.B) { func BenchmarkBitset(b *testing.B) {
......
...@@ -1065,9 +1065,11 @@ func (z nat) setBit(x nat, i uint, b uint) nat { ...@@ -1065,9 +1065,11 @@ func (z nat) setBit(x nat, i uint, b uint) nat {
return z.norm() return z.norm()
case 1: case 1:
if j >= n { if j >= n {
n = j + 1 z = z.make(j + 1)
z[n:].clear()
} else {
z = z.make(n)
} }
z = z.make(n)
copy(z, x) copy(z, x)
z[j] |= m z[j] |= m
// no need to normalize // no need to normalize
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment