]> Cypherpunks repositories - gostls13.git/commitdiff
math/big: avoid panic in float.Text with negative prec
authorAlberto Donizetti <alb.donizetti@gmail.com>
Wed, 1 Jun 2016 17:16:56 +0000 (19:16 +0200)
committerRobert Griesemer <gri@golang.org>
Wed, 1 Jun 2016 19:20:52 +0000 (19:20 +0000)
Fixes #15918

Change-Id: I4b434aed262960a2e6c659d4c2296fbf662c3a52
Reviewed-on: https://go-review.googlesource.com/23633
Run-TryBot: Robert Griesemer <gri@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>

src/math/big/floatconv_test.go
src/math/big/ftoa.go

index b6f999360825b8332f88cf90dba0cc6b01522b4b..b2a1ab05fcc69d9fadcae425d1d11251c85b3888 100644 (file)
@@ -290,6 +290,11 @@ func TestFloat64Text(t *testing.T) {
                // Issue 2625.
                {383260575764816448, 'f', 0, "383260575764816448"},
                {383260575764816448, 'g', -1, "3.8326057576481645e+17"},
+
+               // Issue 15918.
+               {1, 'f', -10, "1"},
+               {1, 'f', -11, "1"},
+               {1, 'f', -12, "1"},
        } {
                // The test cases are from the strconv package which tests float64 values.
                // When formatting values with prec = -1 (shortest representation),
index 624ea5e0731926c67b8cbd5cc02a25f9f57fa9bb..57b16e1ad1ff7ffa337eabbe961458464d8c9ac1 100644 (file)
@@ -41,8 +41,11 @@ import (
 // x.Prec() mantissa bits.
 // The prec value is ignored for the 'b' or 'p' format.
 func (x *Float) Text(format byte, prec int) string {
-       const extra = 10 // TODO(gri) determine a good/better value here
-       return string(x.Append(make([]byte, 0, prec+extra), format, prec))
+       cap := 10 // TODO(gri) determine a good/better value here
+       if prec > 0 {
+               cap += prec
+       }
+       return string(x.Append(make([]byte, 0, cap), format, prec))
 }
 
 // String formats x like x.Text('g', 10).