Skip to content

Commit 4d77d6b

Browse files
committed
encoding/xml: allow ]]> in attribute values
This is permitted by the XML specification. Fixes: #68387
1 parent 239666c commit 4d77d6b

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

src/encoding/xml/xml.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -1004,8 +1004,9 @@ Input:
10041004
}
10051005

10061006
// <![CDATA[ section ends with ]]>.
1007-
// It is an error for ]]> to appear in ordinary text.
1008-
if b0 == ']' && b1 == ']' && b == '>' {
1007+
// It is an error for ]]> to appear in ordinary text,
1008+
// but it is allowed in quoted strings.
1009+
if quote < 0 && b0 == ']' && b1 == ']' && b == '>' {
10091010
if cdata {
10101011
trunc = 2
10111012
break Input

src/encoding/xml/xml_test.go

+24
Original file line numberDiff line numberDiff line change
@@ -626,6 +626,30 @@ type item struct {
626626
FieldA string
627627
}
628628

629+
func TestIssue68387(t *testing.T) {
630+
data := `<item b=']]>'/>`
631+
dec := NewDecoder(strings.NewReader(data))
632+
var tok1, tok2, tok3 Token
633+
var err error
634+
if tok1, err = dec.RawToken(); err != nil {
635+
t.Fatalf("RawToken() failed: %#v", err)
636+
}
637+
if tok2, err = dec.RawToken(); err != nil {
638+
t.Fatalf("RawToken() failed: %#v", err)
639+
}
640+
if tok3, err = dec.RawToken(); err != io.EOF || tok3 != nil {
641+
t.Fatalf("Missed EOF")
642+
}
643+
s := StartElement{Name{"", "item"}, []Attr{Attr{Name{"","b"}, "]]>"}}}
644+
if !reflect.DeepEqual(tok1.(StartElement), s) {
645+
t.Error("Wrong start element")
646+
}
647+
e := EndElement{Name{"","item"}}
648+
if tok2.(EndElement) != e {
649+
t.Error("Wrong end element")
650+
}
651+
}
652+
629653
func TestIssue569(t *testing.T) {
630654
data := `<item><FieldA>abcd</FieldA></item>`
631655
var i item

0 commit comments

Comments
 (0)