clamp StringToIeee exponent accumulation against signed overflow - #310
Open
Ramya-9353 wants to merge 1 commit into
Open
clamp StringToIeee exponent accumulation against signed overflow#310Ramya-9353 wants to merge 1 commit into
Ramya-9353 wants to merge 1 commit into
Conversation
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.
Repro: two valid inputs, both under INT_MAX bytes, overflow the exponent.
"0."then 10737448230s then"1e-1073741823"underflows at string-to-double.cc:754 (UBSan: signed integer overflow-1073744824 + -1073741823); 10737438231s then"e1073741823"overflows at :768 (1073741823 + 1073743051).Cause: StringToIeee accumulates the decimal exponent from digit runs that are bounded only by the input length. Leading fractional zeros decrement it with no floor, and the number of integer digits dropped past the significand limit is added to it, so a long enough integer or fractional part pushes it past int range. The parsed exponent field is capped at INT_MAX/2 and strtod clamps its own exponent (#308), but neither of these two accumulations was bounded.
Fix: floor the leading-zero and fractional decrements at -INT_MAX/4 (the range the assert just below them already documents) and combine the dropped-digit count in int64, clamped to INT_MAX/2. An exponent that large is far outside the double range, so valid results are unchanged; the two cases above now read as 0 and inf, and the ctest suite stays green.