Skip to content

Commit

Permalink
Fix bug in the parsing of imbalanced XML elements
Browse files Browse the repository at this point in the history
  • Loading branch information
beevik committed Jul 18, 2020
1 parent 977ca2e commit 4a2f8b9
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
8 changes: 7 additions & 1 deletion etree.go
Original file line number Diff line number Diff line change
Expand Up @@ -683,7 +683,7 @@ func (e *Element) RemoveChildAt(index int) Token {
return t
}

// ReadFrom reads XML from the reader ;ri' and stores the result as a new
// ReadFrom reads XML from the reader 'ri' and stores the result as a new
// child of this element.
func (e *Element) readFrom(ri io.Reader, settings ReadSettings) (n int64, err error) {
r := newCountReader(ri)
Expand All @@ -697,6 +697,9 @@ func (e *Element) readFrom(ri io.Reader, settings ReadSettings) (n int64, err er
t, err := dec.RawToken()
switch {
case err == io.EOF:
if len(stack.data) != 1 {
return r.bytes, ErrXML
}
return r.bytes, nil
case err != nil:
return r.bytes, err
Expand All @@ -714,6 +717,9 @@ func (e *Element) readFrom(ri io.Reader, settings ReadSettings) (n int64, err er
}
stack.push(e)
case xml.EndElement:
if top.Tag != t.Name.Local || top.Space != t.Name.Space {
return r.bytes, ErrXML
}
stack.pop()
case xml.CharData:
data := string(t)
Expand Down
23 changes: 23 additions & 0 deletions etree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,29 @@ func TestDocument(t *testing.T) {
}
}

func TestImbalancedXML(t *testing.T) {
cases := []string{
`<test>`,
`</test>`,
`<test></test2>`,
`<doc xmlns:p="xyz"><p:test></test></doc>`,
`<doc xmlns:p="xyz"><test></p:test></doc>`,
`<test>malformed`,
`malformed</test>`,
`<test><test></test>`,
`<test></test></test>`,
`<test><test></test></test2>`,
`<test><test2></test></test2>`,
}
for _, c := range cases {
doc := NewDocument()
err := doc.ReadFromString(c)
if err == nil {
t.Errorf("etree: imbalanced XML should have failed:\n%s", c)
}
}
}

func TestDocumentReadNonUTF8Encodings(t *testing.T) {
s := `<?xml version="1.0" encoding="ISO-8859-1"?>
<store>
Expand Down
2 changes: 1 addition & 1 deletion path_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ var testXML = `
<author>James Linn</author>
<author>Vaidyanathan Nagarajan</author>
<year>2003</year>
<price>49.99</p:price>
<price>49.99</price>
<editor>
</editor>
</book>
Expand Down

0 comments on commit 4a2f8b9

Please sign in to comment.