Skip to content

Commit 898f9db

Browse files
committed
math/big: make Rat accessors safe for concurrent use
Do not modify the underlying Rat denominator when calling one of the accessors Float32, Float64; verify that we don't modify the Rat denominator when calling Inv, Sign, IsInt, Num. Fixes #34919. Reopens #33792. Change-Id: Ife6d1252373f493a597398ee51e7b5695b708df5 Reviewed-on: https://go-review.googlesource.com/c/go/+/201205 Reviewed-by: Ian Lance Taylor <[email protected]>
1 parent 52e5987 commit 898f9db

File tree

2 files changed

+30
-4
lines changed

2 files changed

+30
-4
lines changed

src/math/big/rat.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ func quotToFloat64(a, b nat) (f float64, exact bool) {
271271
func (x *Rat) Float32() (f float32, exact bool) {
272272
b := x.b.abs
273273
if len(b) == 0 {
274-
b = b.set(natOne) // materialize denominator
274+
b = natOne
275275
}
276276
f, exact = quotToFloat32(x.a.abs, b)
277277
if x.a.neg {
@@ -287,7 +287,7 @@ func (x *Rat) Float32() (f float32, exact bool) {
287287
func (x *Rat) Float64() (f float64, exact bool) {
288288
b := x.b.abs
289289
if len(b) == 0 {
290-
b = b.set(natOne) // materialize denominator
290+
b = natOne
291291
}
292292
f, exact = quotToFloat64(x.a.abs, b)
293293
if x.a.neg {
@@ -377,7 +377,7 @@ func (z *Rat) Inv(x *Rat) *Rat {
377377
z.Set(x)
378378
a := z.b.abs
379379
if len(a) == 0 {
380-
a = a.set(natOne) // materialize numerator
380+
a = a.set(natOne) // materialize numerator (a is part of z!)
381381
}
382382
b := z.a.abs
383383
if b.cmp(natOne) == 0 {
@@ -418,7 +418,7 @@ func (x *Rat) Num() *Int {
418418
func (x *Rat) Denom() *Int {
419419
x.b.neg = false // the result is always >= 0
420420
if len(x.b.abs) == 0 {
421-
x.b.abs = x.b.abs.set(natOne) // materialize denominator
421+
x.b.abs = x.b.abs.set(natOne) // materialize denominator (see issue #33792)
422422
}
423423
return &x.b
424424
}

src/math/big/rat_test.go

+26
Original file line numberDiff line numberDiff line change
@@ -678,3 +678,29 @@ func BenchmarkRatCmp(b *testing.B) {
678678
x.Cmp(y)
679679
}
680680
}
681+
682+
// TestIssue34919 verifies that a Rat's denominator is not modified
683+
// when simply accessing the Rat value.
684+
func TestIssue34919(t *testing.T) {
685+
for _, acc := range []struct {
686+
name string
687+
f func(*Rat)
688+
}{
689+
{"Float32", func(x *Rat) { x.Float32() }},
690+
{"Float64", func(x *Rat) { x.Float64() }},
691+
{"Inv", func(x *Rat) { new(Rat).Inv(x) }},
692+
{"Sign", func(x *Rat) { x.Sign() }},
693+
{"IsInt", func(x *Rat) { x.IsInt() }},
694+
{"Num", func(x *Rat) { x.Num() }},
695+
// {"Denom", func(x *Rat) { x.Denom() }}, TODO(gri) should we change the API? See issue #33792.
696+
} {
697+
// A denominator of length 0 is interpreted as 1. Make sure that
698+
// "materialization" of the denominator doesn't lead to setting
699+
// the underlying array element 0 to 1.
700+
r := &Rat{Int{abs: nat{991}}, Int{abs: make(nat, 0, 1)}}
701+
acc.f(r)
702+
if d := r.b.abs[:1][0]; d != 0 {
703+
t.Errorf("%s modified denominator: got %d, want 0", acc.name, d)
704+
}
705+
}
706+
}

0 commit comments

Comments
 (0)