From 14bc4f5e5fd5a113c97bdf1455b4f7d36a81f54b Mon Sep 17 00:00:00 2001 From: Leigh McCulloch Date: Fri, 27 Oct 2017 07:04:57 +0000 Subject: [PATCH] encoding/xml: ignore whitespace in values and attrs Whitespace is ignored in bool values and attrs. It is convenient and relatively safe since whitespace around a bool value is often unimportant. The same logic can be applied to numeric values of types int, uint, and float. Fixes #22146 Change-Id: Ie0462def90304af144b8e2e72d85b644857c27cc Reviewed-on: https://go-review.googlesource.com/73891 Reviewed-by: Sam Whited Reviewed-by: Ian Lance Taylor Run-TryBot: Sam Whited TryBot-Result: Gobot Gobot --- src/encoding/xml/read.go | 8 +-- src/encoding/xml/read_test.go | 118 +++++++++++++++++++++++++++++++++- 2 files changed, 120 insertions(+), 6 deletions(-) diff --git a/src/encoding/xml/read.go b/src/encoding/xml/read.go index 6709d5aeba..36c7ba6311 100644 --- a/src/encoding/xml/read.go +++ b/src/encoding/xml/read.go @@ -113,7 +113,7 @@ import ( // Unmarshal maps an XML element or attribute value to an integer or // floating-point field by setting the field to the result of // interpreting the string value in decimal. There is no check for -// overflow. +// overflow. Whitespace is trimmed and ignored. // // Unmarshal maps an XML element to a Name by recording the element // name. @@ -615,7 +615,7 @@ func copyValue(dst reflect.Value, src []byte) (err error) { dst.SetInt(0) return nil } - itmp, err := strconv.ParseInt(string(src), 10, dst.Type().Bits()) + itmp, err := strconv.ParseInt(strings.TrimSpace(string(src)), 10, dst.Type().Bits()) if err != nil { return err } @@ -625,7 +625,7 @@ func copyValue(dst reflect.Value, src []byte) (err error) { dst.SetUint(0) return nil } - utmp, err := strconv.ParseUint(string(src), 10, dst.Type().Bits()) + utmp, err := strconv.ParseUint(strings.TrimSpace(string(src)), 10, dst.Type().Bits()) if err != nil { return err } @@ -635,7 +635,7 @@ func copyValue(dst reflect.Value, src []byte) (err error) { dst.SetFloat(0) return nil } - ftmp, err := strconv.ParseFloat(string(src), dst.Type().Bits()) + ftmp, err := strconv.ParseFloat(strings.TrimSpace(string(src)), dst.Type().Bits()) if err != nil { return err } diff --git a/src/encoding/xml/read_test.go b/src/encoding/xml/read_test.go index 08c3e3b4fe..ba203cd98b 100644 --- a/src/encoding/xml/read_test.go +++ b/src/encoding/xml/read_test.go @@ -912,12 +912,50 @@ func TestUnmarshalEmptyValues(t *testing.T) { type WhitespaceValuesParent struct { BFalse bool BTrue bool + I int + INeg int + I8 int8 + I8Neg int8 + I16 int16 + I16Neg int16 + I32 int32 + I32Neg int32 + I64 int64 + I64Neg int64 + UI uint + UI8 uint8 + UI16 uint16 + UI32 uint32 + UI64 uint64 + F32 float32 + F32Neg float32 + F64 float64 + F64Neg float64 } const whitespaceValuesXML = ` false true + 266703 + -266703 + 112 + -112 + 6703 + -6703 + 266703 + -266703 + 266703 + -266703 + 266703 + 112 + 6703 + 266703 + 266703 + 266.703 + -266.703 + 266.703 + -266.703 ` @@ -931,6 +969,25 @@ func TestUnmarshalWhitespaceValues(t *testing.T) { want := WhitespaceValuesParent{ BFalse: false, BTrue: true, + I: 266703, + INeg: -266703, + I8: 112, + I8Neg: -112, + I16: 6703, + I16Neg: -6703, + I32: 266703, + I32Neg: -266703, + I64: 266703, + I64Neg: -266703, + UI: 266703, + UI8: 112, + UI16: 6703, + UI32: 266703, + UI64: 266703, + F32: 266.703, + F32Neg: -266.703, + F64: 266.703, + F64Neg: -266.703, } if v != want { t.Fatalf("whitespace values: Unmarshal:\nhave: %#+v\nwant: %#+v", v, want) @@ -938,14 +995,52 @@ func TestUnmarshalWhitespaceValues(t *testing.T) { } type WhitespaceAttrsParent struct { - BFalse bool `xml:",attr"` - BTrue bool `xml:",attr"` + BFalse bool `xml:",attr"` + BTrue bool `xml:",attr"` + I int `xml:",attr"` + INeg int `xml:",attr"` + I8 int8 `xml:",attr"` + I8Neg int8 `xml:",attr"` + I16 int16 `xml:",attr"` + I16Neg int16 `xml:",attr"` + I32 int32 `xml:",attr"` + I32Neg int32 `xml:",attr"` + I64 int64 `xml:",attr"` + I64Neg int64 `xml:",attr"` + UI uint `xml:",attr"` + UI8 uint8 `xml:",attr"` + UI16 uint16 `xml:",attr"` + UI32 uint32 `xml:",attr"` + UI64 uint64 `xml:",attr"` + F32 float32 `xml:",attr"` + F32Neg float32 `xml:",attr"` + F64 float64 `xml:",attr"` + F64Neg float64 `xml:",attr"` } const whitespaceAttrsXML = ` ` @@ -960,6 +1055,25 @@ func TestUnmarshalWhitespaceAttrs(t *testing.T) { want := WhitespaceAttrsParent{ BFalse: false, BTrue: true, + I: 266703, + INeg: -266703, + I8: 112, + I8Neg: -112, + I16: 6703, + I16Neg: -6703, + I32: 266703, + I32Neg: -266703, + I64: 266703, + I64Neg: -266703, + UI: 266703, + UI8: 112, + UI16: 6703, + UI32: 266703, + UI64: 266703, + F32: 266.703, + F32Neg: -266.703, + F64: 266.703, + F64Neg: -266.703, } if v != want { t.Fatalf("whitespace attrs: Unmarshal:\nhave: %#+v\nwant: %#+v", v, want) -- 2.48.1