From f13849a7af4c058caa6af14f7d3f9aa81982c124 Mon Sep 17 00:00:00 2001 From: Constantin Konstantinidis Date: Sun, 15 Apr 2018 11:26:32 +0200 Subject: [PATCH] encoding/xml: error when closing tag does not match opening tag Comparing opening and closing tag is done using the prefix when available. Documentation states that Token returns URI in the Space part of the Name. Translation has been moved for the End tag before the namespace is removed from the stack. After closing a tag using a namespace, the valid namespace must be taken from the opening tag. Tests added. Fixes #20685 Change-Id: I4d90b19f7e21a76663f0ea1c1db6c6bf9fd2a389 Reviewed-on: https://go-review.googlesource.com/c/go/+/107255 Run-TryBot: Ian Lance Taylor Reviewed-by: Ian Lance Taylor TryBot-Result: Gopher Robot Auto-Submit: Ian Lance Taylor Reviewed-by: Michael Knyszek Run-TryBot: Ian Lance Taylor --- src/encoding/xml/xml.go | 5 +++-- src/encoding/xml/xml_test.go | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/src/encoding/xml/xml.go b/src/encoding/xml/xml.go index 50a91a897fb195..d4509dfc851f55 100644 --- a/src/encoding/xml/xml.go +++ b/src/encoding/xml/xml.go @@ -314,15 +314,14 @@ func (d *Decoder) Token() (Token, error) { } } + d.pushElement(t1.Name) d.translate(&t1.Name, true) for i := range t1.Attr { d.translate(&t1.Attr[i].Name, false) } - d.pushElement(t1.Name) t = t1 case EndElement: - d.translate(&t1.Name, true) if !d.popElement(&t1) { return nil, d.err } @@ -495,6 +494,8 @@ func (d *Decoder) popElement(t *EndElement) bool { return false } + d.translate(&t.Name, true) + // Pop stack until a Start or EOF is on the top, undoing the // translations that were associated with the element we just closed. for d.stk != nil && d.stk.kind != stkStart && d.stk.kind != stkEOF { diff --git a/src/encoding/xml/xml_test.go b/src/encoding/xml/xml_test.go index 15c5a7492fa084..e20dc781a1273b 100644 --- a/src/encoding/xml/xml_test.go +++ b/src/encoding/xml/xml_test.go @@ -1059,6 +1059,41 @@ func TestIssue12417(t *testing.T) { } } +func TestIssue20685(t *testing.T) { + testCases := []struct { + s string + ok bool + }{ + {`one`, false}, + {`one`, true}, + {`one`, false}, + {`one`, false}, + {`one`, false}, + {`one`, false}, + {`one`, false}, + } + for _, tc := range testCases { + d := NewDecoder(strings.NewReader(tc.s)) + var err error + for { + _, err = d.Token() + if err != nil { + if err == io.EOF { + err = nil + } + break + } + } + if err != nil && tc.ok { + t.Errorf("%q: Closing tag with namespace : expected no error, got %s", tc.s, err) + continue + } + if err == nil && !tc.ok { + t.Errorf("%q: Closing tag with namespace : expected error, got nil", tc.s) + } + } +} + func tokenMap(mapping func(t Token) Token) func(TokenReader) TokenReader { return func(src TokenReader) TokenReader { return mapper{