Avoid Float out of range warning for clearly underflowing numbers#1044
Open
znz wants to merge 1 commit into
Open
Avoid Float out of range warning for clearly underflowing numbers#1044znz wants to merge 1 commit into
znz wants to merge 1 commit into
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Two new minefield parser tests (
test_i_number_double_huge_neg_exp,test_i_number_real_underflow) introduced in 6507a836c5 cause spurious CI warnings because numbers like123.456e-789and123e-10000000fall through tojson_decode_large_float→rb_cstr_to_dbl→strtod, which setserrno=ERANGEon underflow and triggers Ruby'srb_warning("Float %s out of range", ...).Fix
Add an early-return guard in
json_decode_float()before therb_cstr_to_dblfallback:When
mantissa_digits + exponent < -324, the value is bounded above by10^(-324) = 1e-324 < DBL_TRUE_MIN/2 ≈ 2.47e-324, so IEEE 754 round-to-nearest guarantees a result of0.0— no need to callrb_cstr_to_dbl. The subnormal range (-324 ≤ mantissa_digits + exponent < -307) still goes throughrb_cstr_to_dblfor accurate results.Same approach as 5b4d95b8d0 which added the
exponent < INT32_MIN → 0.0fast-path with the same motivation.