mirror of
https://github.com/golang/go
synced 2024-11-16 19:54:55 -07:00
encoding/xml: check nil pointer in DecodeElement
Fixes #53350
Change-Id: Id5e1f4016db5f1d4349ee1a76a9dfe3aeae83cee
GitHub-Last-Rev: 45add12161
GitHub-Pull-Request: golang/go#53407
Reviewed-on: https://go-review.googlesource.com/c/go/+/412634
Auto-Submit: Ian Lance Taylor <iant@google.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
Run-TryBot: Ian Lance Taylor <iant@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Alex Rakoczy <alex@golang.org>
This commit is contained in:
parent
f571518139
commit
606c6c371a
@ -148,6 +148,10 @@ func (d *Decoder) DecodeElement(v any, start *StartElement) error {
|
|||||||
if val.Kind() != reflect.Pointer {
|
if val.Kind() != reflect.Pointer {
|
||||||
return errors.New("non-pointer passed to Unmarshal")
|
return errors.New("non-pointer passed to Unmarshal")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if val.IsNil() {
|
||||||
|
return errors.New("nil pointer passed to Unmarshal")
|
||||||
|
}
|
||||||
return d.unmarshal(val.Elem(), start)
|
return d.unmarshal(val.Elem(), start)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1079,3 +1079,18 @@ func TestUnmarshalWhitespaceAttrs(t *testing.T) {
|
|||||||
t.Fatalf("whitespace attrs: Unmarshal:\nhave: %#+v\nwant: %#+v", v, want)
|
t.Fatalf("whitespace attrs: Unmarshal:\nhave: %#+v\nwant: %#+v", v, want)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// golang.org/issues/53350
|
||||||
|
func TestUnmarshalIntoNil(t *testing.T) {
|
||||||
|
type T struct {
|
||||||
|
A int `xml:"A"`
|
||||||
|
}
|
||||||
|
|
||||||
|
var nilPointer *T
|
||||||
|
err := Unmarshal([]byte("<T><A>1</A></T>"), nilPointer)
|
||||||
|
|
||||||
|
if err == nil {
|
||||||
|
t.Fatalf("no error in unmarshalling")
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user