Procházet zdrojové kódy

#135 fix double negative

Tao Wen před 8 roky
rodič
revize
c966eaa031
2 změnil soubory, kde provedl 29 přidání a 0 odebrání
  1. 16 0
      feature_iter_float.go
  2. 13 0
      jsoniter_invalid_test.go

+ 16 - 0
feature_iter_float.go

@@ -159,6 +159,14 @@ func (iter *Iterator) readFloat32SlowPath() (ret float32) {
 	if iter.Error != nil && iter.Error != io.EOF {
 		return
 	}
+	if len(str) == 0 {
+		iter.ReportError("readFloat32SlowPath", "empty number")
+		return
+	}
+	if str[0] == '-' {
+		iter.ReportError("readFloat32SlowPath", "-- is not valid")
+		return
+	}
 	val, err := strconv.ParseFloat(str, 32)
 	if err != nil {
 		iter.Error = err
@@ -233,6 +241,14 @@ func (iter *Iterator) readFloat64SlowPath() (ret float64) {
 	if iter.Error != nil && iter.Error != io.EOF {
 		return
 	}
+	if len(str) == 0 {
+		iter.ReportError("readFloat64SlowPath", "empty number")
+		return
+	}
+	if str[0] == '-' {
+		iter.ReportError("readFloat64SlowPath", "-- is not valid")
+		return
+	}
 	val, err := strconv.ParseFloat(str, 64)
 	if err != nil {
 		iter.Error = err

+ 13 - 0
jsoniter_invalid_test.go

@@ -3,6 +3,7 @@ package jsoniter
 import (
 	"github.com/stretchr/testify/require"
 	"testing"
+	"encoding/json"
 )
 
 func Test_missing_object_end(t *testing.T) {
@@ -65,3 +66,15 @@ func Test_invalid_array_input(t *testing.T) {
 	obj := [0]string{}
 	should.NotNil(Unmarshal(input, &obj))
 }
+
+func Test_double_negative(t *testing.T) {
+	should := require.New(t)
+	var v interface{}
+	should.NotNil(json.Unmarshal([]byte(`--2`), &v))
+	var vFloat64 float64
+	should.NotNil(UnmarshalFromString(`--2`, &vFloat64))
+	var vFloat32 float32
+	should.NotNil(UnmarshalFromString(`--2`, &vFloat32))
+	var vInt int
+	should.NotNil(UnmarshalFromString(`--2`, &vInt))
+}