aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdam Langley <agl@golang.org>2010-05-24 14:32:55 -0400
committerAdam Langley <agl@golang.org>2010-05-24 14:32:55 -0400
commiteadebba36fcfb57a4da9469b61b786c30d2e2ee0 (patch)
tree56341bb7177546301ab946a4f6aeaf8dcbb8e9d2
parent977475fd491680157eda8f33dfd11261ec85fabd (diff)
downloadgo-eadebba36fcfb57a4da9469b61b786c30d2e2ee0.tar.gz
go-eadebba36fcfb57a4da9469b61b786c30d2e2ee0.zip
big: prevent errors in Exp in the face of aliasing
R=gri CC=golang-dev, golang-dev https://golang.org/cl/1244044
-rwxr-xr-xsrc/pkg/big/int.go3
-rwxr-xr-xsrc/pkg/big/int_test.go2
-rwxr-xr-xsrc/pkg/big/nat.go5
3 files changed, 8 insertions, 2 deletions
diff --git a/src/pkg/big/int.go b/src/pkg/big/int.go
index dd91796603..a74028fd74 100755
--- a/src/pkg/big/int.go
+++ b/src/pkg/big/int.go
@@ -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
}
diff --git a/src/pkg/big/int_test.go b/src/pkg/big/int_test.go
index 064f467311..e92ebe508a 100755
--- a/src/pkg/big/int_test.go
+++ b/src/pkg/big/int_test.go
@@ -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)
}
diff --git a/src/pkg/big/nat.go b/src/pkg/big/nat.go
index dc066580a1..dc2e6be288 100755
--- a/src/pkg/big/nat.go
+++ b/src/pkg/big/nat.go
@@ -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