Skip to content

Commit 724edda

Browse files
Copilotbyroot
authored andcommitted
Avoid Float out of range warning for clearly underflowing numbers
When parsing JSON floats with extremely negative exponents (like 123.456e-789 or 123e-10000000), the parser would fall back to rb_cstr_to_dbl which internally calls strtod. When strtod returns ERANGE due to underflow to 0.0, Ruby emits a "Float out of range" warning, causing noise in the test output. Fix: when mantissa_digits + exponent < -324, the effective value is less than 10^(-324) < DBL_TRUE_MIN/2, so it must round to 0.0 in IEEE 754 round-to-nearest. Return 0.0 directly without going through rb_cstr_to_dbl, avoiding the spurious warning. This fixes warnings introduced by JSONMinefieldParserTest tests (test_i_number_double_huge_neg_exp and test_i_number_real_underflow) added in commit 6507a836c5.
1 parent 168095c commit 724edda

1 file changed

Lines changed: 7 additions & 0 deletions

File tree

ext/json/ext/parser/parser.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1133,6 +1133,13 @@ static inline VALUE json_decode_float(JSON_ParserConfig *config, uint64_t mantis
11331133
}
11341134

11351135
if (RB_UNLIKELY(mantissa_digits > 18 || mantissa_digits + exponent < -307)) {
1136+
// If the value is so small that it definitely underflows to 0.0, return early
1137+
// to avoid triggering a "Float out of range" warning from rb_cstr_to_dbl.
1138+
// When mantissa_digits + exponent < -324, value < 10^(-324) < DBL_TRUE_MIN/2,
1139+
// so it rounds to 0 in IEEE 754 round-to-nearest.
1140+
if (RB_UNLIKELY(mantissa_digits + exponent < -324)) {
1141+
return rb_float_new(negative ? -0.0 : 0.0);
1142+
}
11361143
return json_decode_large_float(start, end - start);
11371144
}
11381145

0 commit comments

Comments
 (0)