Skip to content

Commit 6ea6201

Browse files
committed
Fix parsing of number with out of range negative exponents
1 parent 1b67eb3 commit 6ea6201

3 files changed

Lines changed: 16 additions & 6 deletions

File tree

‎ext/json/ext/parser/parser.c‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1169,11 +1169,11 @@ static inline VALUE json_decode_float(JSON_ParserConfig *config, uint64_t mantis
11691169
return rb_funcallv(config->decimal_class, config->decimal_method_id, 1, &text);
11701170
}
11711171

1172-
if (RB_UNLIKELY(exponent > INT32_MAX)) {
1172+
if (RB_UNLIKELY(exponent >= (int64_t)INT32_MAX)) {
11731173
return negative ? CMinusInfinity : CInfinity;
11741174
}
11751175

1176-
if (RB_UNLIKELY(exponent < INT32_MIN)) {
1176+
if (RB_UNLIKELY(exponent <= INT32_MIN)) {
11771177
return rb_float_new(negative ? -0.0 : 0.0);
11781178
}
11791179

@@ -1514,7 +1514,7 @@ static inline VALUE json_parse_number(JSON_ParserState *state, JSON_ParserConfig
15141514
}
15151515

15161516
if (RB_UNLIKELY(exponent_digits >= 20 || abs_exponent > (uint64_t)INT64_MAX)) {
1517-
exponent = negative_exponent ? INT64_MIN : INT64_MAX;
1517+
exponent = negative_exponent ? INT32_MIN : INT32_MAX;
15181518
} else {
15191519
exponent = negative_exponent ? -(int64_t)abs_exponent : (int64_t)abs_exponent;
15201520
}

‎java/src/json/ext/Parser.java‎

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎test/json/json_parser_test.rb‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,12 @@ def test_parse_numbers
147147
assert_raise(ParserError) { parse('-Infinity') }
148148
assert_equal(-1.0/0, parse('-Infinity', allow_nan: true))
149149
capture_output { assert_equal(Float::INFINITY, parse("23456789012E666")) }
150+
151+
assert_parse_float "1.5e-99999999999999999999"
152+
assert_parse_float "1.5e-9223372036854775808"
153+
assert_parse_float "0.0e-99999999999999999999"
154+
assert_parse_float "-1.5e-99999999999999999999"
155+
assert_parse_float "123.456e-18446744073709551615"
150156
end
151157

152158
INTEGER_FAST_PATH_BOUNDARIES = [
@@ -1084,6 +1090,10 @@ def encode(*)
10841090

10851091
private
10861092

1093+
def assert_parse_float(float_str)
1094+
assert_equal Float(float_str), JSON.parse(float_str)
1095+
end
1096+
10871097
def assert_equal_float(expected, actual, delta = 1e-2)
10881098
Array === expected and expected = expected.first
10891099
Array === actual and actual = actual.first

0 commit comments

Comments
 (0)