File tree Expand file tree Collapse file tree 3 files changed +36
-1
lines changed Expand file tree Collapse file tree 3 files changed +36
-1
lines changed Original file line number Diff line number Diff line change @@ -77,7 +77,13 @@ class RubyObjectEncoder {
77
77
78
78
void encode_float (VALUE v) {
79
79
double f = rb_float_value (v);
80
- writer.Double (f);
80
+ if (isinf (f)) {
81
+ rb_raise (rb_eEncodeError, " Float::INFINITY is not allowed in JSON" );
82
+ } else if (isnan (f)) {
83
+ rb_raise (rb_eEncodeError, " Float::NAN is not allowed in JSON" );
84
+ } else {
85
+ writer.Double (f);
86
+ }
81
87
}
82
88
83
89
void encode_string (VALUE v) {
Original file line number Diff line number Diff line change @@ -119,4 +119,23 @@ def test_encode_false
119
119
def test_encode_nil
120
120
assert_equal "null" , encode ( nil )
121
121
end
122
+
123
+ def test_encode_NaN
124
+ error = assert_raises RapidJSON ::EncodeError do
125
+ encode ( Float ::NAN )
126
+ end
127
+ assert_match "Float::NAN is not allowed in JSON" , error . message
128
+ end
129
+
130
+ def test_encode_Infinity
131
+ error = assert_raises RapidJSON ::EncodeError do
132
+ encode ( Float ::INFINITY )
133
+ end
134
+ assert_match "Float::INFINITY is not allowed in JSON" , error . message
135
+
136
+ error = assert_raises RapidJSON ::EncodeError do
137
+ encode ( -Float ::INFINITY )
138
+ end
139
+ assert_match "Float::INFINITY is not allowed in JSON" , error . message
140
+ end
122
141
end
Original file line number Diff line number Diff line change @@ -78,4 +78,14 @@ def test_parse_too_deep
78
78
assert parse ( "[" *( max +1 ) + "]" *( max +1 ) )
79
79
end
80
80
end
81
+
82
+ def test_parse_NaN_and_Infinity
83
+ assert_raises RapidJSON ::ParseError do
84
+ parse ( "NaN" )
85
+ end
86
+
87
+ assert_raises RapidJSON ::ParseError do
88
+ parse ( "Infinity" )
89
+ end
90
+ end
81
91
end
You can’t perform that action at this time.
0 commit comments