Skip to content

Commit

Permalink
encoding/xml: Return SyntaxError for unmatched root start elements.
Browse files Browse the repository at this point in the history
Currently, the xml.Decoder's Token routine returns successfully for
XML input that does not properly close root start elements (and any
unclosed descendants). For example, all the following inputs

    <root>
    <root><foo>
    <root><foo></foo>

cause Token to return with nil and io.EOF, indicating a successful
parse.

This change fixes that. It leaves the semantics of RawToken intact.

Fixes #11405

Change-Id: I6f1328c410cf41e17de0a93cf357a69f12c2a9f7
Reviewed-on: https://go-review.googlesource.com/14315
Reviewed-by: Nigel Tao <nigeltao@golang.org>
  • Loading branch information
rsto authored and nigeltao committed Sep 10, 2015
1 parent da7e9e4 commit bf21643
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/encoding/xml/xml.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,9 @@ func (d *Decoder) Token() (t Token, err error) {
t = d.nextToken
d.nextToken = nil
} else if t, err = d.rawToken(); err != nil {
if err == io.EOF && d.stk != nil && d.stk.kind != stkEOF {
err = d.syntaxError("unexpected EOF")
}
return
}

Expand Down
21 changes: 21 additions & 0 deletions src/encoding/xml/xml_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -750,3 +750,24 @@ func TestIssue5880(t *testing.T) {
t.Errorf("Marshal generated invalid UTF-8: %x", data)
}
}

func TestIssue11405(t *testing.T) {
testCases := []string{
"<root>",
"<root><foo>",
"<root><foo></foo>",
}
for _, tc := range testCases {
d := NewDecoder(strings.NewReader(tc))
var err error
for {
_, err = d.Token()
if err != nil {
break
}
}
if _, ok := err.(*SyntaxError); !ok {
t.Errorf("%s: Token: Got error %v, want SyntaxError", tc, err)
}
}
}

0 comments on commit bf21643

Please sign in to comment.