]> Cypherpunks repositories - gostls13.git/commitdiff
encoding/xml: fix panic in Marshal
authorRuss Cox <rsc@golang.org>
Mon, 9 Sep 2013 20:42:07 +0000 (16:42 -0400)
committerRuss Cox <rsc@golang.org>
Mon, 9 Sep 2013 20:42:07 +0000 (16:42 -0400)
Fixes #6341.

R=golang-dev, bradfitz
CC=golang-dev
https://golang.org/cl/13512048

src/pkg/encoding/xml/marshal.go
src/pkg/encoding/xml/marshal_test.go

index a6ee5d51285b729ec307f8fc7e927e5f666b3175..ac6c6296c08471a7a7c0eaa1a81b02577b663906 100644 (file)
@@ -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()
@@ -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}
index 8d9239eb4adcf92e9c45c611346f90f02c983788..6cd894e0c641a3882ad24fc7dae9e45652e4661f 100644 (file)
@@ -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
@@ -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 {