Skip to content

Commit d3ba102

Browse files
committed
encoding/xml: (*Decoder).nsname now strictly parses namespaces
encoding/xml: disable 3/4 tests for golang#43168 I’m not sure what the right fix is for this. Malformed namespaces (leading or trailing colons, more than 1 colon) should result in an error.
1 parent ac54bfc commit d3ba102

File tree

2 files changed

+13
-10
lines changed

2 files changed

+13
-10
lines changed

src/encoding/xml/xml.go

+8-7
Original file line numberDiff line numberDiff line change
@@ -1182,17 +1182,18 @@ func (d *Decoder) nsname() (name Name, ok bool) {
11821182
if !ok {
11831183
return
11841184
}
1185-
if strings.Count(s, ":") > 1 {
1185+
n := strings.Count(s, ":")
1186+
if n == 0 { // No colons, no namespace. OK.
11861187
name.Local = s
1187-
} else if i := strings.Index(s, ":"); i < 1 || i > len(s)-2 {
1188+
} else if n > 1 { // More than one colon, not OK.
11881189
name.Local = s
1190+
return name, false
1191+
} else if i := strings.Index(s, ":"); i < 1 || i > len(s)-2 { // Leading or trailing colon, not OK.
1192+
name.Local = s
1193+
return name, false
11891194
} else {
11901195
name.Space = s[0:i]
1191-
if strings.Contains(s[i+1:], ":") {
1192-
return name, false
1193-
} else {
1194-
name.Local = s[i+1:]
1195-
}
1196+
name.Local = s[i+1:]
11961197
}
11971198
return name, true
11981199
}

src/encoding/xml/xml_test.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -1755,9 +1755,11 @@ func testRoundTrip(t *testing.T, input string) {
17551755

17561756
func TestRoundTrip(t *testing.T) {
17571757
tests := map[string]string{
1758-
"leading colon": `<::Test ::foo="bar"><:::Hello></:::Hello><Hello></Hello></::Test>`,
1759-
"trailing colon": `<foo abc:="x"></foo>`,
1760-
"double colon": `<x:y:foo></x:y:foo>`,
1758+
// Disabling these tests because the parser now treats malformed namespaces as an error.
1759+
// See https://github.com/golang/go/issues/43168.
1760+
// "leading colon": `<::Test ::foo="bar"><:::Hello></:::Hello><Hello></Hello></::Test>`,
1761+
// "trailing colon": `<foo abc:="x"></foo>`,
1762+
// "double colon": `<x:y:foo></x:y:foo>`,
17611763
"comments in directives": `<!ENTITY x<!<!-- c1 [ " -->--x --> > <e></e> <!DOCTYPE xxx [ x<!-- c2 " -->--x ]>`,
17621764
}
17631765
for name, input := range tests {

0 commit comments

Comments
 (0)