]> Cypherpunks repositories - gostls13.git/commitdiff
math/big: fix destination leak into result value
authorRoger Peppe <rogpeppe@gmail.com>
Wed, 30 Nov 2011 17:29:58 +0000 (09:29 -0800)
committerRobert Griesemer <gri@golang.org>
Wed, 30 Nov 2011 17:29:58 +0000 (09:29 -0800)
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

src/pkg/math/big/int_test.go
src/pkg/math/big/nat.go

index 163c662b0bbdf81bdab8b0d2b2906beffce61614..aa7c19495494fff3778d2d5d326eb789ba8a9371 100644 (file)
@@ -1242,10 +1242,14 @@ func TestBitSet(t *testing.T) {
                x.SetString(test.x, 0)
                b := x.Bit(test.i)
                if b != test.b {
-
-                       t.Errorf("#%d want %v got %v", i, test.b, b)
+                       t.Errorf("#%d got %v want %v", i, b, test.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) {
index 9fba2d2a062d4529873f938f6eab6bb24f95d82e..680445dc9a73a30d24f4020b8fcd57a99b877d3d 100644 (file)
@@ -1065,9 +1065,11 @@ func (z nat) setBit(x nat, i uint, b uint) nat {
                return z.norm()
        case 1:
                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)
                z[j] |= m
                // no need to normalize