Skip to content

Commit 10c36fb

Browse files
committed
encoding/xml: fix panic in Marshal
Fixes #6341. R=golang-dev, bradfitz CC=golang-dev https://golang.org/cl/13512048
1 parent 1b65155 commit 10c36fb

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

src/pkg/encoding/xml/marshal.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -655,7 +655,10 @@ func (p *printer) marshalSimple(typ reflect.Type, val reflect.Value) (string, []
655655
case reflect.Bool:
656656
return strconv.FormatBool(val.Bool()), nil, nil
657657
case reflect.Array:
658-
// will be [...]byte
658+
if typ.Elem().Kind() != reflect.Uint8 {
659+
break
660+
}
661+
// [...]byte
659662
var bytes []byte
660663
if val.CanAddr() {
661664
bytes = val.Slice(0, val.Len()).Bytes()
@@ -665,7 +668,10 @@ func (p *printer) marshalSimple(typ reflect.Type, val reflect.Value) (string, []
665668
}
666669
return "", bytes, nil
667670
case reflect.Slice:
668-
// will be []byte
671+
if typ.Elem().Kind() != reflect.Uint8 {
672+
break
673+
}
674+
// []byte
669675
return "", val.Bytes(), nil
670676
}
671677
return "", nil, &UnsupportedTypeError{typ}

src/pkg/encoding/xml/marshal_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -904,6 +904,10 @@ type AttrParent struct {
904904
X string `xml:"X>Y,attr"`
905905
}
906906

907+
type BadAttr struct {
908+
Name []string `xml:"name,attr"`
909+
}
910+
907911
var marshalErrorTests = []struct {
908912
Value interface{}
909913
Err string
@@ -936,6 +940,10 @@ var marshalErrorTests = []struct {
936940
Value: &AttrParent{},
937941
Err: `xml: X>Y chain not valid with attr flag`,
938942
},
943+
{
944+
Value: BadAttr{[]string{"X", "Y"}},
945+
Err: `xml: unsupported type: []string`,
946+
},
939947
}
940948

941949
var marshalIndentTests = []struct {

0 commit comments

Comments
 (0)