]> Cypherpunks repositories - gostls13.git/commitdiff
encoding/xml: check type when unmarshaling innerxml field
authorQuentin Smith <quentin@golang.org>
Tue, 8 Nov 2016 21:47:04 +0000 (16:47 -0500)
committerQuentin Smith <quentin@golang.org>
Wed, 9 Nov 2016 20:10:58 +0000 (20:10 +0000)
We only support unmarshaling into a string or a []byte, but we
previously would try (and panic while) setting a slice of a different
type. The docs say ",innerxml" is ignored if the type is not string or
[]byte, so do that for other slices as well.

Fixes #15600.

Change-Id: Ia64815945a14c3d04a0a45ccf413e38b58a69416
Reviewed-on: https://go-review.googlesource.com/32919
Run-TryBot: Quentin Smith <quentin@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Russ Cox <rsc@golang.org>
src/encoding/xml/read.go
src/encoding/xml/read_test.go

index ba623665608a29f49a3bef176b029a93719f07dd..ed4470f3eba54a27def4b62a8e0c6ca96286adc5 100644 (file)
@@ -582,7 +582,9 @@ Loop:
        case reflect.String:
                t.SetString(string(saveXMLData))
        case reflect.Slice:
-               t.Set(reflect.ValueOf(saveXMLData))
+               if t.Type().Elem().Kind() == reflect.Uint8 {
+                       t.Set(reflect.ValueOf(saveXMLData))
+               }
        }
 
        return nil
index 7a98092803add05e7d5b1aa50a7dba0891050757..b53d72c7160bc3cc14282728f8fc8d5bc26c8d2b 100644 (file)
@@ -733,3 +733,22 @@ func TestMalformedComment(t *testing.T) {
                }
        }
 }
+
+type IXField struct {
+       Five        int      `xml:"five"`
+       NotInnerXML []string `xml:",innerxml"`
+}
+
+// Issue 15600. ",innerxml" on a field that can't hold it.
+func TestInvalidInnerXMLType(t *testing.T) {
+       v := new(IXField)
+       if err := Unmarshal([]byte(`<tag><five>5</five><innertag/></tag>`), v); err != nil {
+               t.Errorf("Unmarshal failed: got %v", err)
+       }
+       if v.Five != 5 {
+               t.Errorf("Five = %v, want 5", v.Five)
+       }
+       if v.NotInnerXML != nil {
+               t.Errorf("NotInnerXML = %v, want nil", v.NotInnerXML)
+       }
+}