]> Cypherpunks repositories - gostls13.git/commitdiff
encoding/xml: do not ignore error return from copyValue
authorEric Lagergren <ericscottlagergren@gmail.com>
Sat, 20 Aug 2016 22:46:06 +0000 (15:46 -0700)
committerBrad Fitzpatrick <bradfitz@golang.org>
Mon, 22 Aug 2016 18:01:47 +0000 (18:01 +0000)
The error return from copyValue was ignored causing some XML attribute
parsing to swallow an error.

Additionally, type MyMarshalerAttrTest had no UnmarshalXMLAttr method
causing marshalTests not to be symmetrical and the test suite to fail
for test case 101.

Fixes #16158

Change-Id: Icebc505295a2c656ca4b42ba37bb0957dd7260c6
Reviewed-on: https://go-review.googlesource.com/27455
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
src/encoding/xml/marshal_test.go
src/encoding/xml/read.go

index fe8b16fe43145b63bfba444c554c66028df96381..c0c6a0cd9fb6c7cdb36019fd87770eabbbb951aa 100644 (file)
@@ -317,6 +317,10 @@ func (m *MyMarshalerAttrTest) MarshalXMLAttr(name Name) (Attr, error) {
        return Attr{name, "hello world"}, nil
 }
 
+func (m *MyMarshalerAttrTest) UnmarshalXMLAttr(attr Attr) error {
+       return nil
+}
+
 type MarshalerStruct struct {
        Foo MyMarshalerAttrTest `xml:",attr"`
 }
@@ -1732,7 +1736,7 @@ func TestDecodeEncode(t *testing.T) {
        in.WriteString(`<?xml version="1.0" encoding="UTF-8"?>
 <?Target Instruction?>
 <root>
-</root>        
+</root>
 `)
        dec := NewDecoder(&in)
        enc := NewEncoder(&out)
@@ -1823,3 +1827,14 @@ func TestSimpleUseOfEncodeToken(t *testing.T) {
                t.Errorf("enc.EncodeToken: expected %q; got %q", want, buf.String())
        }
 }
+
+// Issue 16158. Decoder.unmarshalAttr ignores the return value of copyValue.
+func TestIssue16158(t *testing.T) {
+       const data = `<foo b="HELLOWORLD"></foo>`
+       err := Unmarshal([]byte(data), &struct {
+               B byte `xml:"b,attr,omitempty"`
+       }{})
+       if err == nil {
+               t.Errorf("Unmarshal: expected error, got nil")
+       }
+}
index 937432e3a7ec75ecde0bcfd4bd5afe3ddfaa4081..53c15a2840e6ad554d9cab36fa8d5c6c7f17e6d8 100644 (file)
@@ -232,7 +232,6 @@ func (p *Decoder) unmarshalAttr(val reflect.Value, attr Attr) error {
                }
                val = val.Elem()
        }
-
        if val.CanInterface() && val.Type().Implements(unmarshalerAttrType) {
                // This is an unmarshaler with a non-pointer receiver,
                // so it's likely to be incorrect, but we do what we're told.
@@ -257,9 +256,7 @@ func (p *Decoder) unmarshalAttr(val reflect.Value, attr Attr) error {
                        return pv.Interface().(encoding.TextUnmarshaler).UnmarshalText([]byte(attr.Value))
                }
        }
-
-       copyValue(val, []byte(attr.Value))
-       return nil
+       return copyValue(val, []byte(attr.Value))
 }
 
 var (