1
0
mirror of https://github.com/golang/go synced 2024-11-18 03:24:42 -07:00

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 <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
This commit is contained in:
Ian Lance Taylor 2017-12-06 18:02:02 -08:00 committed by Brad Fitzpatrick
parent 617fc0ffd3
commit 89d7a2fbda
2 changed files with 20 additions and 1 deletions

View File

@ -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)
}
}

View File

@ -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