From 607bd735b4aaf21b385ec74656d7536cc2c55ef6 Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Fri, 1 Nov 2019 14:04:23 +1100 Subject: [PATCH] Have IntRange methods return distinct *big.Int's --- lib/interval/interval.go | 15 ++++++++++----- lib/interval/interval_test.go | 10 ++++++++++ 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/lib/interval/interval.go b/lib/interval/interval.go index b848a4c2..8390ee90 100644 --- a/lib/interval/interval.go +++ b/lib/interval/interval.go @@ -138,6 +138,11 @@ func (x *biggerIntPair) fromIntRange(y IntRange) { // The zero value (zero in the Go sense, not in the integer sense) is a valid, // infinitely sized interval, unbounded at both ends. // +// IntRange's operator-like methods (Add, Mul, etc) return an IntRange whose +// *big.Int pointer values (if non-nil) are always distinct from its inputs' +// *big.Int pointer values. Specifically, after "z = x.Add(y)", mutating +// "*z[0]" will not affect "*x[0]", "*x[1]", "*y[0]" or "*y[1]". +// // A subtle point is that an interval's minimum or maximum can be infinite, but // if an integer value i is known to be within such an interval, i's possible // values are arbitrarily large but not infinite. Specifically, 0*i is @@ -638,11 +643,11 @@ func (x IntRange) And(y IntRange) (z IntRange, ok bool) { if y[1] != nil { zMax = x.andMax(y) } else { - return IntRange{big.NewInt(0), x[1]}, true + return IntRange{big.NewInt(0), big.NewInt(0).Set(x[1])}, true } } else { if y[1] != nil { - return IntRange{big.NewInt(0), y[1]}, true + return IntRange{big.NewInt(0), big.NewInt(0).Set(y[1])}, true } else { return IntRange{big.NewInt(0), nil}, true } @@ -693,10 +698,10 @@ func (x IntRange) Or(y IntRange) (z IntRange, ok bool) { // value is contained in the other interval. For example, if both xx // and yy can be x[0], then (x[0] | x[0]) is simply x[0]. if x.ContainsInt(y[0]) { - return IntRange{y[0], nil}, true + return IntRange{big.NewInt(0).Set(y[0]), nil}, true } if y.ContainsInt(x[0]) { - return IntRange{x[0], nil}, true + return IntRange{big.NewInt(0).Set(x[0]), nil}, true } if x[1] == nil && y[1] == nil { panic("unreachable") @@ -804,7 +809,7 @@ func (x IntRange) andMax(y IntRange) *big.Int { if x[1].Cmp(y[1]) > 0 { min = y[1] } - return min + return big.NewInt(0).Set(min) } // Otherwise, x and y don't overlap. Four examples: diff --git a/lib/interval/interval_test.go b/lib/interval/interval_test.go index 4e924ba9..874a5a5e 100644 --- a/lib/interval/interval_test.go +++ b/lib/interval/interval_test.go @@ -291,9 +291,19 @@ func TestContainsEtc(tt *testing.T) { } } +func shareBigIntPointers(x IntRange, y IntRange) bool { + return ((x[0] == y[0]) && (x[0] != nil)) || + ((x[0] == y[1]) && (x[0] != nil)) || + ((x[1] == y[0]) && (x[1] != nil)) || + ((x[1] == y[1]) && (x[1] != nil)) +} + func testBruteForceAgrees(x IntRange, y IntRange, opKey string) error { brute, bruteOK := bruteForce(x, y, opKey) got, gotOK := intOperators[opKey](x, y) + if shareBigIntPointers(got, x) || shareBigIntPointers(got, y) { + return fmt.Errorf("*big.Int pointers (pointing to mutable state) were re-used") + } if !got.Eq(brute) || gotOK != bruteOK { return fmt.Errorf("got %v, %t, brute force gave %v, %t", got, gotOK, brute, bruteOK) }