]> Cypherpunks repositories - gostls13.git/commitdiff
encoding/xml: error when more than one colon in qualified names
authorConstantin Konstantinidis <constantinkonstantinidis@gmail.com>
Sat, 23 Jul 2022 07:11:32 +0000 (09:11 +0200)
committerGopher Robot <gobot@golang.org>
Wed, 9 Nov 2022 22:56:44 +0000 (22:56 +0000)
Add test.

Fixes #20396

Change-Id: I89e9013eb338f831e1908e390b284794df78fb6b
Reviewed-on: https://go-review.googlesource.com/c/go/+/103875
Auto-Submit: Ian Lance Taylor <iant@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Run-TryBot: Ian Lance Taylor <iant@google.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
Reviewed-by: Michael Knyszek <mknyszek@google.com>
src/encoding/xml/xml.go
src/encoding/xml/xml_test.go

index 2e3232441553c160c46ad063cffaae081d7d339a..9df556a1369800bce077b841905f7f3838a8eb60 100644 (file)
@@ -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 {
index 26c4a8a74bed15780144c6ece77a6d10c25384c0..df2581252106c4d2b915228e877b1c28d3593ccb 100644 (file)
@@ -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
+       }{
+               {`<a:te:st xmlns:a="abcd"/>`, // Issue 20396
+                       UnmarshalError("XML syntax error on line 1: expected element name after <")},
+               {`<a:te=st xmlns:a="abcd"/>`, attrError},
+               {`<a:te&st xmlns:a="abcd"/>`, attrError},
+               {`<a:test xmlns:a="abcd"/>`, nil},
+               {`<a:te:st xmlns:a="abcd">1</a:te:st>`,
+                       UnmarshalError("XML syntax error on line 1: expected element name after <")},
+               {`<a:te=st xmlns:a="abcd">1</a:te=st>`, attrError},
+               {`<a:te&st xmlns:a="abcd">1</a:te&st>`, attrError},
+               {`<a:test xmlns:a="abcd">1</a:test>`, 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></:::Hello><Hello></Hello></::Test>`,
                "trailing colon":         `<foo abc:="x"></foo>`,
-               "double colon":           `<x:y:foo></x:y:foo>`,
                "comments in directives": `<!ENTITY x<!<!-- c1 [ " -->--x --> > <e></e> <!DOCTYPE xxx [ x<!-- c2 " -->--x ]>`,
        }
        for name, input := range tests {