Skip to content

Commit 48c6048

Browse files
committed
encoding/xml: check type when unmarshaling innerxml field
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 <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Russ Cox <[email protected]>
1 parent 9c2037f commit 48c6048

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

src/encoding/xml/read.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -582,7 +582,9 @@ Loop:
582582
case reflect.String:
583583
t.SetString(string(saveXMLData))
584584
case reflect.Slice:
585-
t.Set(reflect.ValueOf(saveXMLData))
585+
if t.Type().Elem().Kind() == reflect.Uint8 {
586+
t.Set(reflect.ValueOf(saveXMLData))
587+
}
586588
}
587589

588590
return nil

src/encoding/xml/read_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -733,3 +733,22 @@ func TestMalformedComment(t *testing.T) {
733733
}
734734
}
735735
}
736+
737+
type IXField struct {
738+
Five int `xml:"five"`
739+
NotInnerXML []string `xml:",innerxml"`
740+
}
741+
742+
// Issue 15600. ",innerxml" on a field that can't hold it.
743+
func TestInvalidInnerXMLType(t *testing.T) {
744+
v := new(IXField)
745+
if err := Unmarshal([]byte(`<tag><five>5</five><innertag/></tag>`), v); err != nil {
746+
t.Errorf("Unmarshal failed: got %v", err)
747+
}
748+
if v.Five != 5 {
749+
t.Errorf("Five = %v, want 5", v.Five)
750+
}
751+
if v.NotInnerXML != nil {
752+
t.Errorf("NotInnerXML = %v, want nil", v.NotInnerXML)
753+
}
754+
}

0 commit comments

Comments
 (0)