Commit 89d7a2fb authored by Ian Lance Taylor's avatar Ian Lance Taylor Committed by Brad Fitzpatrick

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: 's avatarBrad Fitzpatrick <bradfitz@golang.org>
parent 617fc0ff
......@@ -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)
}
}
......@@ -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
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment