]> Cypherpunks repositories - gostls13.git/commitdiff
big: prevent errors in Exp in the face of aliasing
authorAdam Langley <agl@golang.org>
Mon, 24 May 2010 18:32:55 +0000 (14:32 -0400)
committerAdam Langley <agl@golang.org>
Mon, 24 May 2010 18:32:55 +0000 (14:32 -0400)
R=gri
CC=golang-dev, golang-dev
https://golang.org/cl/1244044

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

index dd91796603cab8dfadf215e89df797f4692b8f76..a74028fd745b58fb599dadec3edbab556e1ff7b4 100755 (executable)
@@ -434,8 +434,9 @@ func (z *Int) BitLen() int {
 // See Knuth, volume 2, section 4.6.3.
 func (z *Int) Exp(x, y, m *Int) *Int {
        if y.neg || len(y.abs) == 0 {
+               neg := x.neg
                z.SetInt64(1)
-               z.neg = x.neg
+               z.neg = neg
                return z
        }
 
index 064f467311f41017384132c5ff083f35ade09648..e92ebe508a1002f654a5daba047031e87daf3e93 100755 (executable)
@@ -602,7 +602,7 @@ func TestExp(t *testing.T) {
                        continue
                }
 
-               z := new(Int).Exp(x, y, m)
+               z := y.Exp(x, y, m)
                if !isNormalized(z) {
                        t.Errorf("#%d: %v is not normalized", i, *z)
                }
index dc066580a191ffa458b2736eda24f71cfeed25ab..dc2e6be288b3c52963345ad23e352f97e5af5be5 100755 (executable)
@@ -920,6 +920,11 @@ func (z nat) random(rand *rand.Rand, limit nat, n int) nat {
 // If m != nil, expNN calculates x**y mod m. Otherwise it calculates x**y. It
 // reuses the storage of z if possible.
 func (z nat) expNN(x, y, m nat) nat {
+       if alias(z, x) || alias(z, y) {
+               // We cannot allow in place modification of x or y.
+               z = nil
+       }
+
        if len(y) == 0 {
                z = z.make(1)
                z[0] = 1