Commit 10c36fbc authored by Russ Cox's avatar Russ Cox

encoding/xml: fix panic in Marshal

Fixes #6341.

R=golang-dev, bradfitz
CC=golang-dev
https://golang.org/cl/13512048
parent 1b651556
...@@ -655,7 +655,10 @@ func (p *printer) marshalSimple(typ reflect.Type, val reflect.Value) (string, [] ...@@ -655,7 +655,10 @@ func (p *printer) marshalSimple(typ reflect.Type, val reflect.Value) (string, []
case reflect.Bool: case reflect.Bool:
return strconv.FormatBool(val.Bool()), nil, nil return strconv.FormatBool(val.Bool()), nil, nil
case reflect.Array: case reflect.Array:
// will be [...]byte if typ.Elem().Kind() != reflect.Uint8 {
break
}
// [...]byte
var bytes []byte var bytes []byte
if val.CanAddr() { if val.CanAddr() {
bytes = val.Slice(0, val.Len()).Bytes() bytes = val.Slice(0, val.Len()).Bytes()
...@@ -665,7 +668,10 @@ func (p *printer) marshalSimple(typ reflect.Type, val reflect.Value) (string, [] ...@@ -665,7 +668,10 @@ func (p *printer) marshalSimple(typ reflect.Type, val reflect.Value) (string, []
} }
return "", bytes, nil return "", bytes, nil
case reflect.Slice: case reflect.Slice:
// will be []byte if typ.Elem().Kind() != reflect.Uint8 {
break
}
// []byte
return "", val.Bytes(), nil return "", val.Bytes(), nil
} }
return "", nil, &UnsupportedTypeError{typ} return "", nil, &UnsupportedTypeError{typ}
......
...@@ -904,6 +904,10 @@ type AttrParent struct { ...@@ -904,6 +904,10 @@ type AttrParent struct {
X string `xml:"X>Y,attr"` X string `xml:"X>Y,attr"`
} }
type BadAttr struct {
Name []string `xml:"name,attr"`
}
var marshalErrorTests = []struct { var marshalErrorTests = []struct {
Value interface{} Value interface{}
Err string Err string
...@@ -936,6 +940,10 @@ var marshalErrorTests = []struct { ...@@ -936,6 +940,10 @@ var marshalErrorTests = []struct {
Value: &AttrParent{}, Value: &AttrParent{},
Err: `xml: X>Y chain not valid with attr flag`, Err: `xml: X>Y chain not valid with attr flag`,
}, },
{
Value: BadAttr{[]string{"X", "Y"}},
Err: `xml: unsupported type: []string`,
},
} }
var marshalIndentTests = []struct { var marshalIndentTests = []struct {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment