Skip to content

Commit

Permalink
encoding/xml: fix panic in Marshal
Browse files Browse the repository at this point in the history
Fixes #6341.

R=golang-dev, bradfitz
CC=golang-dev
https://golang.org/cl/13512048
  • Loading branch information
rsc committed Sep 9, 2013
1 parent 1b65155 commit 10c36fb
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/pkg/encoding/xml/marshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -655,7 +655,10 @@ func (p *printer) marshalSimple(typ reflect.Type, val reflect.Value) (string, []
case reflect.Bool:
return strconv.FormatBool(val.Bool()), nil, nil
case reflect.Array:
// will be [...]byte
if typ.Elem().Kind() != reflect.Uint8 {
break
}
// [...]byte
var bytes []byte
if val.CanAddr() {
bytes = val.Slice(0, val.Len()).Bytes()
Expand All @@ -665,7 +668,10 @@ func (p *printer) marshalSimple(typ reflect.Type, val reflect.Value) (string, []
}
return "", bytes, nil
case reflect.Slice:
// will be []byte
if typ.Elem().Kind() != reflect.Uint8 {
break
}
// []byte
return "", val.Bytes(), nil
}
return "", nil, &UnsupportedTypeError{typ}
Expand Down
8 changes: 8 additions & 0 deletions src/pkg/encoding/xml/marshal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -904,6 +904,10 @@ type AttrParent struct {
X string `xml:"X>Y,attr"`
}

type BadAttr struct {
Name []string `xml:"name,attr"`
}

var marshalErrorTests = []struct {
Value interface{}
Err string
Expand Down Expand Up @@ -936,6 +940,10 @@ var marshalErrorTests = []struct {
Value: &AttrParent{},
Err: `xml: X>Y chain not valid with attr flag`,
},
{
Value: BadAttr{[]string{"X", "Y"}},
Err: `xml: unsupported type: []string`,
},
}

var marshalIndentTests = []struct {
Expand Down

0 comments on commit 10c36fb

Please sign in to comment.