From 89332e037aeaf1223de4c24805719f733e4c0977 Mon Sep 17 00:00:00 2001 From: Constantin Konstantinidis Date: Sat, 23 Jul 2022 09:11:32 +0200 Subject: [PATCH] encoding/xml: error when more than one colon in qualified names Add test. Fixes #20396 Change-Id: I89e9013eb338f831e1908e390b284794df78fb6b Reviewed-on: https://go-review.googlesource.com/c/go/+/103875 Auto-Submit: Ian Lance Taylor TryBot-Result: Gopher Robot Run-TryBot: Ian Lance Taylor Reviewed-by: Ian Lance Taylor Reviewed-by: Michael Knyszek --- src/encoding/xml/xml.go | 2 +- src/encoding/xml/xml_test.go | 36 ++++++++++++++++++++++++++++++++++-- 2 files changed, 35 insertions(+), 3 deletions(-) diff --git a/src/encoding/xml/xml.go b/src/encoding/xml/xml.go index 2e32324415..9df556a136 100644 --- a/src/encoding/xml/xml.go +++ b/src/encoding/xml/xml.go @@ -1167,7 +1167,7 @@ func (d *Decoder) nsname() (name Name, ok bool) { return } if strings.Count(s, ":") > 1 { - name.Local = s + return name, false } else if space, local, ok := strings.Cut(s, ":"); !ok || space == "" || local == "" { name.Local = s } else { diff --git a/src/encoding/xml/xml_test.go b/src/encoding/xml/xml_test.go index 26c4a8a74b..df25812521 100644 --- a/src/encoding/xml/xml_test.go +++ b/src/encoding/xml/xml_test.go @@ -1088,6 +1088,40 @@ func TestIssue12417(t *testing.T) { } } +func TestIssue20396(t *testing.T) { + + var attrError = UnmarshalError("XML syntax error on line 1: expected attribute name in element") + + testCases := []struct { + s string + wantErr error + }{ + {``, // Issue 20396 + UnmarshalError("XML syntax error on line 1: expected element name after <")}, + {``, attrError}, + {``, attrError}, + {``, nil}, + {`1`, + UnmarshalError("XML syntax error on line 1: expected element name after <")}, + {`1`, attrError}, + {`1`, attrError}, + {`1`, nil}, + } + + var dest string + for _, tc := range testCases { + if got, want := Unmarshal([]byte(tc.s), &dest), tc.wantErr; got != want { + if got == nil { + t.Errorf("%s: Unexpected success, want %v", tc.s, want) + } else if want == nil { + t.Errorf("%s: Unexpected error, got %v", tc.s, got) + } else if got.Error() != want.Error() { + t.Errorf("%s: got %v, want %v", tc.s, got, want) + } + } + } +} + func TestIssue20685(t *testing.T) { testCases := []struct { s string @@ -1257,9 +1291,7 @@ func testRoundTrip(t *testing.T, input string) { func TestRoundTrip(t *testing.T) { tests := map[string]string{ - "leading colon": `<::Test ::foo="bar"><:::Hello>`, "trailing colon": ``, - "double colon": ``, "comments in directives": `--x --> > --x ]>`, } for name, input := range tests { -- 2.50.0