1
0
mirror of https://github.com/json-iterator/go.git synced 2025-04-23 11:37:32 +02:00

#135 fix double negative

This commit is contained in:
Tao Wen 2017-07-18 11:05:39 +08:00
parent f6da8e62c3
commit c966eaa031
2 changed files with 29 additions and 0 deletions

View File

@ -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

View File

@ -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))
}