From 08867d7401180b6cde631bbc33627d2e3bbeb6c8 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Fri, 26 Jun 2026 02:30:59 +0000 Subject: [PATCH] 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. --- ext/json/ext/parser/parser.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/ext/json/ext/parser/parser.c b/ext/json/ext/parser/parser.c index 4d9fa25b..a6154c83 100644 --- a/ext/json/ext/parser/parser.c +++ b/ext/json/ext/parser/parser.c @@ -1133,6 +1133,13 @@ static inline VALUE json_decode_float(JSON_ParserConfig *config, uint64_t mantis } if (RB_UNLIKELY(mantissa_digits > 18 || mantissa_digits + exponent < -307)) { + // If the value is so small that it definitely underflows to 0.0, return early + // to avoid triggering a "Float out of range" warning from rb_cstr_to_dbl. + // When mantissa_digits + exponent < -324, value < 10^(-324) < DBL_TRUE_MIN/2, + // so it rounds to 0 in IEEE 754 round-to-nearest. + if (RB_UNLIKELY(mantissa_digits + exponent < -324)) { + return rb_float_new(negative ? -0.0 : 0.0); + } return json_decode_large_float(start, end - start); }