From 89d7a2fbda06976858cd00451d6eee81fffd9aea Mon Sep 17 00:00:00 2001 From: Ian Lance Taylor Date: Wed, 6 Dec 2017 18:02:02 -0800 Subject: [PATCH] encoding/xml: don't crash on invalid XMLName tag Fixes #20953 Change-Id: Ia30a6e0e335c1f738e1359500e09057b5981f1c7 Reviewed-on: https://go-review.googlesource.com/82397 Run-TryBot: Ian Lance Taylor TryBot-Result: Gobot Gobot Reviewed-by: Brad Fitzpatrick --- src/encoding/xml/marshal_test.go | 19 +++++++++++++++++++ src/encoding/xml/typeinfo.go | 2 +- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/encoding/xml/marshal_test.go b/src/encoding/xml/marshal_test.go index 11f451270a..5c79a48e7a 100644 --- a/src/encoding/xml/marshal_test.go +++ b/src/encoding/xml/marshal_test.go @@ -2441,3 +2441,22 @@ func TestIssue16158(t *testing.T) { t.Errorf("Unmarshal: expected error, got nil") } } + +// Issue 20953. Crash on invalid XMLName attribute. + +type InvalidXMLName struct { + XMLName Name `xml:"error"` + Type struct { + XMLName Name `xml:"type,attr"` + } +} + +func TestInvalidXMLName(t *testing.T) { + var buf bytes.Buffer + enc := NewEncoder(&buf) + if err := enc.Encode(InvalidXMLName{}); err == nil { + t.Error("unexpected success") + } else if want := "invalid tag"; !strings.Contains(err.Error(), want) { + t.Errorf("error %q does not contain %q", err, want) + } +} diff --git a/src/encoding/xml/typeinfo.go b/src/encoding/xml/typeinfo.go index 2e7ae935a8..48de3d7e9e 100644 --- a/src/encoding/xml/typeinfo.go +++ b/src/encoding/xml/typeinfo.go @@ -241,7 +241,7 @@ func lookupXMLName(typ reflect.Type) (xmlname *fieldInfo) { continue } finfo, err := structFieldInfo(typ, &f) - if finfo.name != "" && err == nil { + if err == nil && finfo.name != "" { return finfo } // Also consider errors as a non-existent field tag -- 2.51.0