Skip to content

Commit

Permalink
fix #195 when decode float as int, report it clearly
Browse files Browse the repository at this point in the history
  • Loading branch information
taowen committed Nov 15, 2017
1 parent 3c0e576 commit 9f088cb
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 3 deletions.
2 changes: 1 addition & 1 deletion feature_iter.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ func (iter *Iterator) isObjectEnd() bool {
if c == '}' {
return true
}
iter.ReportError("isObjectEnd", "object ended prematurely, unexpected char " + string([]byte{c}))
iter.ReportError("isObjectEnd", "object ended prematurely, unexpected char "+string([]byte{c}))
return true
}

Expand Down
10 changes: 10 additions & 0 deletions feature_iter_int.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,11 @@ func (iter *Iterator) ReadUint32() (ret uint32) {
}

func (iter *Iterator) readUint32(c byte) (ret uint32) {
defer func() {
if iter.buf[iter.head] == '.' {
iter.ReportError("readUint32", "can not decode float as int")
}
}()
ind := intDigits[c]
if ind == 0 {
return 0 // single zero
Expand Down Expand Up @@ -224,6 +229,11 @@ func (iter *Iterator) ReadUint64() uint64 {
}

func (iter *Iterator) readUint64(c byte) (ret uint64) {
defer func() {
if iter.buf[iter.head] == '.' {
iter.ReportError("readUint64", "can not decode float as int")
}
}()
ind := intDigits[c]
if ind == 0 {
return 0 // single zero
Expand Down
6 changes: 6 additions & 0 deletions jsoniter_int_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,12 @@ func Test_null_as_number(t *testing.T) {
should.Equal("", string(v2))
}

func Test_float_as_int(t *testing.T) {
should := require.New(t)
var i int
should.NotNil(Unmarshal([]byte(`1.1`), &i))
}

func Benchmark_jsoniter_encode_int(b *testing.B) {
stream := NewStream(ConfigDefault, ioutil.Discard, 64)
for n := 0; n < b.N; n++ {
Expand Down
4 changes: 2 additions & 2 deletions jsoniter_interface_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ import (
func Test_write_empty_interface_via_placeholder(t *testing.T) {
fmt.Println(^uint(0) >> 1)
should := require.New(t)
m := map[uint32]interface{}{1:"hello"}
m := map[uint32]interface{}{1: "hello"}
inf := reflect.ValueOf(m).MapIndex(reflect.ValueOf(uint32(1))).Interface()
encoder := &placeholderEncoder{
cfg: ConfigFastest.(*frozenConfig),
cfg: ConfigFastest.(*frozenConfig),
cacheKey: reflect.TypeOf(m).Elem(),
}
stream := ConfigFastest.BorrowStream(nil)
Expand Down

0 comments on commit 9f088cb

Please sign in to comment.