From: Shulhan Date: Tue, 16 Nov 2021 18:36:15 +0000 (+0700) Subject: math/big: call norm when returning success from Rat SetString X-Git-Tag: go1.19beta1~856 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=a80070e0cf0b65d619b6669a789d27746a5b2126;p=gostls13.git math/big: call norm when returning success from Rat SetString After CL 24430, reflect.DeepEqual no longer returns true when comparing a *Rat built with (*Rat).SetString("0") with one built with (*Rat).SetInt64(0). These should be equivalent, but because (*Rat).SetString does not call norm() when returning the zero value, the result of reflect.DeepEqual will be false. One could suggest that developers should use (*Rat).Cmp instead of relying on reflect.DeepEqual, but if a (*Rat) is part of a larger struct that is being compared, this can be cumbersome. This is fixed by calling z.norm() when returning zero in SetString. Fixes #50944 Change-Id: Ib84ae975bf82fe02d1203aa9668a01960c0fd59d Reviewed-on: https://go-review.googlesource.com/c/go/+/364434 Reviewed-by: Katie Hockman Trust: Katie Hockman Trust: Ian Lance Taylor --- diff --git a/src/math/big/ratconv.go b/src/math/big/ratconv.go index 90053a9c81..dadd4d7b8e 100644 --- a/src/math/big/ratconv.go +++ b/src/math/big/ratconv.go @@ -113,7 +113,7 @@ func (z *Rat) SetString(s string) (*Rat, bool) { // special-case 0 (see also issue #16176) if len(z.a.abs) == 0 { - return z, true + return z.norm(), true } // len(z.a.abs) > 0 diff --git a/src/math/big/ratconv_test.go b/src/math/big/ratconv_test.go index e55e655718..45a35608f4 100644 --- a/src/math/big/ratconv_test.go +++ b/src/math/big/ratconv_test.go @@ -9,6 +9,7 @@ import ( "fmt" "io" "math" + "reflect" "strconv" "strings" "testing" @@ -205,6 +206,14 @@ func TestRatSetString(t *testing.T) { } } +func TestRatSetStringZero(t *testing.T) { + got, _ := new(Rat).SetString("0") + want := new(Rat).SetInt64(0) + if !reflect.DeepEqual(got, want) { + t.Errorf("got %#+v, want %#+v", got, want) + } +} + func TestRatScan(t *testing.T) { var buf bytes.Buffer for i, test := range setStringTests {