aboutsummaryrefslogtreecommitdiff
path: root/src/encoding
diff options
context:
space:
mode:
authorDidier Spezia <didier.06@gmail.com>2015-07-15 20:09:24 +0000
committerRuss Cox <rsc@golang.org>2015-07-23 14:16:01 +0000
commitca1d6c4b449caaada97a8024430ec572724b3ae9 (patch)
tree8af9510f499f822a2b9a42181707ff2c52cd99bb /src/encoding
parent74ec5bf2d88364a9f29ab0468a3285e82abe5eb9 (diff)
downloadgo-ca1d6c4b449caaada97a8024430ec572724b3ae9.tar.xz
encoding/xml: EncodeToken silently eats tokens with invalid type
EncodeToken takes a Token (i.e. an interface{}) as a parameter, and expects a value of type StartElement, EndElement, CharData, Comment, ProcInst, or Directive. If a pointer is passed instead, or any type which does not match this list, the token is silently ignored. Added a default case in the type switch to issue a proper error when the type is invalid. The behavior could be later improved by allowing pointers to token to be accepted as well, but not for go1.5. Fixes #11719 Change-Id: Ifd13c1563450b474acf66d57669fdccba76c1949 Reviewed-on: https://go-review.googlesource.com/12252 Reviewed-by: Andrew Gerrand <adg@golang.org> Reviewed-by: Russ Cox <rsc@golang.org>
Diffstat (limited to 'src/encoding')
-rw-r--r--src/encoding/xml/marshal.go4
-rw-r--r--src/encoding/xml/marshal_test.go31
2 files changed, 35 insertions, 0 deletions
diff --git a/src/encoding/xml/marshal.go b/src/encoding/xml/marshal.go
index 5a49cc3528..3c3b6aca58 100644
--- a/src/encoding/xml/marshal.go
+++ b/src/encoding/xml/marshal.go
@@ -199,6 +199,7 @@ var (
// elements (including the StartElement itself) will use the declared
// prefix when encoding names with matching namespace URIs.
func (enc *Encoder) EncodeToken(t Token) error {
+
p := &enc.p
switch t := t.(type) {
case StartElement:
@@ -245,6 +246,9 @@ func (enc *Encoder) EncodeToken(t Token) error {
p.WriteString("<!")
p.Write(t)
p.WriteString(">")
+ default:
+ return fmt.Errorf("xml: EncodeToken of invalid token type")
+
}
return p.cachedWriteError()
}
diff --git a/src/encoding/xml/marshal_test.go b/src/encoding/xml/marshal_test.go
index 78fe841d76..5dc78e748b 100644
--- a/src/encoding/xml/marshal_test.go
+++ b/src/encoding/xml/marshal_test.go
@@ -1906,3 +1906,34 @@ func TestIsValidDirective(t *testing.T) {
}
}
}
+
+// Issue 11719. EncodeToken used to silently eat tokens with an invalid type.
+func TestSimpleUseOfEncodeToken(t *testing.T) {
+ var buf bytes.Buffer
+ enc := NewEncoder(&buf)
+ if err := enc.EncodeToken(&StartElement{Name: Name{"", "object1"}}); err == nil {
+ t.Errorf("enc.EncodeToken: pointer type should be rejected")
+ }
+ if err := enc.EncodeToken(&EndElement{Name: Name{"", "object1"}}); err == nil {
+ t.Errorf("enc.EncodeToken: pointer type should be rejected")
+ }
+ if err := enc.EncodeToken(StartElement{Name: Name{"", "object2"}}); err != nil {
+ t.Errorf("enc.EncodeToken: StartElement %s", err)
+ }
+ if err := enc.EncodeToken(EndElement{Name: Name{"", "object2"}}); err != nil {
+ t.Errorf("enc.EncodeToken: EndElement %s", err)
+ }
+ if err := enc.EncodeToken(Universe{}); err == nil {
+ t.Errorf("enc.EncodeToken: invalid type not caught")
+ }
+ if err := enc.Flush(); err != nil {
+ t.Errorf("enc.Flush: %s", err)
+ }
+ if buf.Len() == 0 {
+ t.Errorf("enc.EncodeToken: empty buffer")
+ }
+ want := "<object2></object2>"
+ if buf.String() != want {
+ t.Errorf("enc.EncodeToken: expected %q; got %q", want, buf.String())
+ }
+}