]> Cypherpunks repositories - gostls13.git/commitdiff
encoding/xml: check nil pointer in DecodeElement
authorshaoliming <shaojacobi@gmail.com>
Fri, 17 Jun 2022 05:01:26 +0000 (05:01 +0000)
committerGopher Robot <gobot@golang.org>
Wed, 22 Jun 2022 16:23:59 +0000 (16:23 +0000)
Fixes #53350

Change-Id: Id5e1f4016db5f1d4349ee1a76a9dfe3aeae83cee
GitHub-Last-Rev: 45add121612a8144c2525828bd7386c4adb05174
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>
src/encoding/xml/read.go
src/encoding/xml/read_test.go

index 565d9a8bea68a574e5528cc3a0c4c254bcae0085..257591262fc182db45edf044f9eaea73a41d6648 100644 (file)
@@ -148,6 +148,10 @@ func (d *Decoder) DecodeElement(v any, start *StartElement) error {
        if val.Kind() != reflect.Pointer {
                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)
 }
 
index 391fe731a800f5af9d95c030c4d3095231d2eb5e..6ef55de77be12335a8870f21985d49b99466ebc9 100644 (file)
@@ -1079,3 +1079,18 @@ func TestUnmarshalWhitespaceAttrs(t *testing.T) {
                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")
+       }
+
+}