Skip to content

Commit f13849a

Browse files
iwdgogopherbot
authored andcommitted
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 <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Auto-Submit: Ian Lance Taylor <[email protected]> Reviewed-by: Michael Knyszek <[email protected]> Run-TryBot: Ian Lance Taylor <[email protected]>
1 parent b459166 commit f13849a

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

src/encoding/xml/xml.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -314,15 +314,14 @@ func (d *Decoder) Token() (Token, error) {
314314
}
315315
}
316316

317+
d.pushElement(t1.Name)
317318
d.translate(&t1.Name, true)
318319
for i := range t1.Attr {
319320
d.translate(&t1.Attr[i].Name, false)
320321
}
321-
d.pushElement(t1.Name)
322322
t = t1
323323

324324
case EndElement:
325-
d.translate(&t1.Name, true)
326325
if !d.popElement(&t1) {
327326
return nil, d.err
328327
}
@@ -495,6 +494,8 @@ func (d *Decoder) popElement(t *EndElement) bool {
495494
return false
496495
}
497496

497+
d.translate(&t.Name, true)
498+
498499
// Pop stack until a Start or EOF is on the top, undoing the
499500
// translations that were associated with the element we just closed.
500501
for d.stk != nil && d.stk.kind != stkStart && d.stk.kind != stkEOF {

src/encoding/xml/xml_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1059,6 +1059,41 @@ func TestIssue12417(t *testing.T) {
10591059
}
10601060
}
10611061

1062+
func TestIssue20685(t *testing.T) {
1063+
testCases := []struct {
1064+
s string
1065+
ok bool
1066+
}{
1067+
{`<x:book xmlns:x="abcd" xmlns:y="abcd"><unclosetag>one</x:book>`, false},
1068+
{`<x:book xmlns:x="abcd" xmlns:y="abcd">one</x:book>`, true},
1069+
{`<x:book xmlns:x="abcd" xmlns:y="abcd">one</y:book>`, false},
1070+
{`<x:book xmlns:y="abcd" xmlns:x="abcd">one</y:book>`, false},
1071+
{`<x:book xmlns:x="abcd">one</y:book>`, false},
1072+
{`<x:book>one</y:book>`, false},
1073+
{`<xbook>one</ybook>`, false},
1074+
}
1075+
for _, tc := range testCases {
1076+
d := NewDecoder(strings.NewReader(tc.s))
1077+
var err error
1078+
for {
1079+
_, err = d.Token()
1080+
if err != nil {
1081+
if err == io.EOF {
1082+
err = nil
1083+
}
1084+
break
1085+
}
1086+
}
1087+
if err != nil && tc.ok {
1088+
t.Errorf("%q: Closing tag with namespace : expected no error, got %s", tc.s, err)
1089+
continue
1090+
}
1091+
if err == nil && !tc.ok {
1092+
t.Errorf("%q: Closing tag with namespace : expected error, got nil", tc.s)
1093+
}
1094+
}
1095+
}
1096+
10621097
func tokenMap(mapping func(t Token) Token) func(TokenReader) TokenReader {
10631098
return func(src TokenReader) TokenReader {
10641099
return mapper{

0 commit comments

Comments
 (0)