From: Matt Brown Date: Thu, 28 Feb 2013 18:08:05 +0000 (-0800) Subject: strconv: use Quote to escape the input string for failed conversion errors X-Git-Tag: go1.1rc2~779 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=1300fb54491496ac19b33c5ef3b4e92fbe89b4e4;p=gostls13.git strconv: use Quote to escape the input string for failed conversion errors This reveals the presence of control and non-printable characters in the errors returned by the Parse functions. Also add unit tests for NumError. R=golang-dev, r, rsc CC=golang-dev https://golang.org/cl/7393075 --- diff --git a/src/pkg/strconv/atof_test.go b/src/pkg/strconv/atof_test.go index b4f3a6f08f..ba4933218b 100644 --- a/src/pkg/strconv/atof_test.go +++ b/src/pkg/strconv/atof_test.go @@ -110,6 +110,7 @@ var atoftests = []atofTest{ {"1e", "0", ErrSyntax}, {"1e-", "0", ErrSyntax}, {".e-1", "0", ErrSyntax}, + {"1\x00.2", "0", ErrSyntax}, // http://www.exploringbinary.com/java-hangs-when-converting-2-2250738585072012e-308/ {"2.2250738585072012e-308", "2.2250738585072014e-308", nil}, diff --git a/src/pkg/strconv/atoi.go b/src/pkg/strconv/atoi.go index bdd5d71f87..21c6900965 100644 --- a/src/pkg/strconv/atoi.go +++ b/src/pkg/strconv/atoi.go @@ -20,7 +20,7 @@ type NumError struct { } func (e *NumError) Error() string { - return "strconv." + e.Func + ": " + `parsing "` + e.Num + `": ` + e.Err.Error() + return "strconv." + e.Func + ": " + "parsing " + Quote(e.Num) + ": " + e.Err.Error() } func syntaxError(fn, str string) *NumError { diff --git a/src/pkg/strconv/atoi_test.go b/src/pkg/strconv/atoi_test.go index d0e7b61dba..9407573078 100644 --- a/src/pkg/strconv/atoi_test.go +++ b/src/pkg/strconv/atoi_test.go @@ -5,6 +5,7 @@ package strconv_test import ( + "errors" "reflect" . "strconv" "testing" @@ -146,6 +147,16 @@ var atoi32tests = []atoi32Test{ {"-2147483649", -1 << 31, ErrRange}, } +type numErrorTest struct { + num, want string +} + +var numErrorTests = []numErrorTest{ + {"0", `strconv.ParseFloat: parsing "0": failed`}, + {"`", "strconv.ParseFloat: parsing \"`\": failed"}, + {"1\x00.2", `strconv.ParseFloat: parsing "1\x00.2": failed`}, +} + func init() { // The atoi routines return NumErrors wrapping // the error and the string. Convert the tables above. @@ -277,6 +288,19 @@ func TestParseInt(t *testing.T) { } } +func TestNumError(t *testing.T) { + for _, test := range numErrorTests { + err := &NumError{ + Func: "ParseFloat", + Num: test.num, + Err: errors.New("failed"), + } + if got := err.Error(); got != test.want { + t.Errorf(`(&NumError{"ParseFloat", %q, "failed"}).Error() = %v, want %v`, test.num, got, test.want) + } + } +} + func BenchmarkAtoi(b *testing.B) { for i := 0; i < b.N; i++ { ParseInt("12345678", 10, 0)