Skip to content

Commit 08047c1

Browse files
author
Tao Wen
committed
fix #365, return error for +inf -inf and NaN
1 parent 68347ec commit 08047c1

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

misc_tests/jsoniter_float_test.go

+21
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package misc_tests
22

33
import (
44
"encoding/json"
5+
"math"
56
"testing"
67

78
"github.com/json-iterator/go"
@@ -77,6 +78,26 @@ func Test_read_number(t *testing.T) {
7778
should.Equal(`92233720368547758079223372036854775807`, string(val))
7879
}
7980

81+
func Test_encode_inf(t *testing.T) {
82+
should := require.New(t)
83+
_, err := json.Marshal(math.Inf(1))
84+
should.Error(err)
85+
_, err = jsoniter.Marshal(float32(math.Inf(1)))
86+
should.Error(err)
87+
_, err = jsoniter.Marshal(math.Inf(-1))
88+
should.Error(err)
89+
}
90+
91+
func Test_encode_nan(t *testing.T) {
92+
should := require.New(t)
93+
_, err := json.Marshal(math.NaN())
94+
should.Error(err)
95+
_, err = jsoniter.Marshal(float32(math.NaN()))
96+
should.Error(err)
97+
_, err = jsoniter.Marshal(math.NaN())
98+
should.Error(err)
99+
}
100+
80101
func Benchmark_jsoniter_float(b *testing.B) {
81102
b.ReportAllocs()
82103
input := []byte(`1.1123,`)

stream_float.go

+17
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package jsoniter
22

33
import (
4+
"fmt"
45
"math"
56
"strconv"
67
)
@@ -13,6 +14,10 @@ func init() {
1314

1415
// WriteFloat32 write float32 to stream
1516
func (stream *Stream) WriteFloat32(val float32) {
17+
if math.IsInf(float64(val), 0) || math.IsNaN(float64(val)) {
18+
stream.Error = fmt.Errorf("unsupported value: %f", val)
19+
return
20+
}
1621
abs := math.Abs(float64(val))
1722
fmt := byte('f')
1823
// Note: Must use float32 comparisons for underlying float32 value to get precise cutoffs right.
@@ -26,6 +31,10 @@ func (stream *Stream) WriteFloat32(val float32) {
2631

2732
// WriteFloat32Lossy write float32 to stream with ONLY 6 digits precision although much much faster
2833
func (stream *Stream) WriteFloat32Lossy(val float32) {
34+
if math.IsInf(float64(val), 0) || math.IsNaN(float64(val)) {
35+
stream.Error = fmt.Errorf("unsupported value: %f", val)
36+
return
37+
}
2938
if val < 0 {
3039
stream.writeByte('-')
3140
val = -val
@@ -54,6 +63,10 @@ func (stream *Stream) WriteFloat32Lossy(val float32) {
5463

5564
// WriteFloat64 write float64 to stream
5665
func (stream *Stream) WriteFloat64(val float64) {
66+
if math.IsInf(val, 0) || math.IsNaN(val) {
67+
stream.Error = fmt.Errorf("unsupported value: %f", val)
68+
return
69+
}
5770
abs := math.Abs(val)
5871
fmt := byte('f')
5972
// Note: Must use float32 comparisons for underlying float32 value to get precise cutoffs right.
@@ -67,6 +80,10 @@ func (stream *Stream) WriteFloat64(val float64) {
6780

6881
// WriteFloat64Lossy write float64 to stream with ONLY 6 digits precision although much much faster
6982
func (stream *Stream) WriteFloat64Lossy(val float64) {
83+
if math.IsInf(val, 0) || math.IsNaN(val) {
84+
stream.Error = fmt.Errorf("unsupported value: %f", val)
85+
return
86+
}
7087
if val < 0 {
7188
stream.writeByte('-')
7289
val = -val

0 commit comments

Comments
 (0)