Reviewers:
golang-dev_googlegroups.com,
Message:
Hello
golan...@googlegroups.com,
I'd like you to review this change to
https://code.google.com/p/go/
Description:
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.
Please review this at
https://codereview.appspot.com/7393075/
Affected files:
M src/pkg/strconv/atof_test.go
M src/pkg/strconv/atoi.go
M src/pkg/strconv/atoi_test.go
Index: src/pkg/strconv/atof_test.go
===================================================================
--- a/src/pkg/strconv/atof_test.go
+++ b/src/pkg/strconv/atof_test.go
@@ -110,6 +110,7 @@
{"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},
Index: src/pkg/strconv/atoi.go
===================================================================
--- a/src/pkg/strconv/atoi.go
+++ b/src/pkg/strconv/atoi.go
@@ -20,7 +20,7 @@
}
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 {
Index: src/pkg/strconv/atoi_test.go
===================================================================
--- 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"
@@ -187,6 +188,22 @@
}
}
+func TestNumError(t *testing.T) {
+ for _, c := range []struct{ Num, Want string }{
+ {Num: "0", Want: `strconv.ParseFloat: parsing "0": failed`},
+ {Num: "1\x00.2", Want: `strconv.ParseFloat: parsing "1\x00.2": failed`},
+ } {
+ err := &NumError{
+ Func: "ParseFloat",
+ Num: c.Num,
+ Err: errors.New("failed"),
+ }
+ if got := err.Error(); got != c.Want {
+ t.Errorf(`&NumError{"ParseFloat", %q, "failed"} = %v, want %v`, c.Num,
got, c.Want)
+ }
+ }
+}
+
func TestParseUint64(t *testing.T) {
for i := range atoui64tests {
test := &atoui64tests[i]