]> Cypherpunks repositories - gostls13.git/commitdiff
math/big: avoid MinExp exponent wrap-around in 'x' Text format
authorRobert Griesemer <gri@golang.org>
Tue, 17 Sep 2019 22:45:40 +0000 (15:45 -0700)
committerRobert Griesemer <gri@golang.org>
Wed, 18 Sep 2019 04:48:34 +0000 (04:48 +0000)
Fixes #34343.

Change-Id: I74240c8f431f6596338633a86a7a5ee1fce70a65
Reviewed-on: https://go-review.googlesource.com/c/go/+/196057
Run-TryBot: Robert Griesemer <gri@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
src/math/big/floatconv_test.go
src/math/big/ftoa.go

index c6c6ba63e58cdf4446ddd9c3125561fd04032328..3aa68341436fef55daa368b0a4405d9751b9d999 100644 (file)
@@ -536,6 +536,10 @@ func TestFloatText(t *testing.T) {
                {"-8191.53125", ToNegativeInf, 53, 'x', 4, "-0x1.fff9p+12"},
                {"8191.53125", ToPositiveInf, 53, 'x', 4, "0x1.fff9p+12"},
                {"-8191.53125", ToPositiveInf, 53, 'x', 4, "-0x1.fff8p+12"},
+
+               // issue 34343
+               {"0x.8p-2147483648", ToNearestEven, 4, 'p', -1, "0x.8p-2147483648"},
+               {"0x.8p-2147483648", ToNearestEven, 4, 'x', -1, "0x1p-2147483649"},
        } {
                f, _, err := ParseFloat(test.x, 0, test.prec, ToNearestEven)
                if err != nil {
index 6cae63ed0978eb63aa69b0fbf38c67757de22f7d..5506e6e4257c50a1f93f3e774398c5ae36b08bd0 100644 (file)
@@ -384,7 +384,7 @@ func (x *Float) fmtX(buf []byte, prec int) []byte {
        case w > n:
                m = nat(nil).shr(m, w-n)
        }
-       exp := x.exp - 1
+       exp64 := int64(x.exp) - 1 // avoid wrap-around
 
        hm := m.utoa(16)
        if debugFloat && hm[0] != '1' {
@@ -397,7 +397,6 @@ func (x *Float) fmtX(buf []byte, prec int) []byte {
        }
 
        buf = append(buf, 'p')
-       exp64 := int64(exp)
        if exp64 >= 0 {
                buf = append(buf, '+')
        } else {