Ver código fonte

fix #206, do not allow nil pointer as unmarshal input

Tao Wen 8 anos atrás
pai
commit
e0df39fda2
2 arquivos alterados com 15 adições e 0 exclusões
  1. 4 0
      feature_reflect.go
  2. 11 0
      jsoniter_invalid_test.go

+ 4 - 0
feature_reflect.go

@@ -233,6 +233,10 @@ func (iter *Iterator) ReadVal(obj interface{}) {
 		return
 	}
 	e := (*emptyInterface)(unsafe.Pointer(&obj))
+	if e.word == nil {
+		iter.ReportError("ReadVal", "can not read into nil pointer")
+		return
+	}
 	decoder.Decode(e.word, iter)
 }
 

+ 11 - 0
jsoniter_invalid_test.go

@@ -136,3 +136,14 @@ func Test_valid(t *testing.T) {
 	should.True(Valid([]byte(`{}`)))
 	should.False(Valid([]byte(`{`)))
 }
+
+func Test_nil_pointer(t *testing.T) {
+	should := require.New(t)
+	data := []byte(`{"A":0}`)
+	type T struct {
+		X int
+	}
+	var obj *T
+	err := Unmarshal(data, obj)
+	should.NotNil(err)
+}