GH-50964: [C++] fix out-of-bounds read in DecimalRescale for out-of-range scale - #50965
Open
Arawoof06 wants to merge 1 commit into
Open
GH-50964: [C++] fix out-of-bounds read in DecimalRescale for out-of-range scale#50965Arawoof06 wants to merge 1 commit into
Arawoof06 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.
Rationale for this change
DecimalRescaleindexes the scale-multiplier table throughGetScaleMultiplier(abs_delta_scale), but that table only holdskMaxScale + 1entries and is guarded by aDCHECKthat disappears underNDEBUG.Decimal::FromStringclamps a negative parsed scale but leaves a positive one unbounded, so a literal like1E-100parses to precision 1 and scale 100. In the CSV decimal converter that passes the precision check and then callsRescale(100, 0), readingkDecimal128PowersOfTen[100]well past the end of the 39-entry table. The same path is reachable from the publicDecimal32/64/128/256::FromStringandRescaleAPIs. UBSan flags it asindex 100 out of bounds for type 'const BasicDecimal128[39]'.What changes are included in this PR?
Reject a scale delta whose magnitude exceeds
kMaxScaleinDecimalRescalebefore the lookup, returningkRescaleDataLoss. A change that large can never be represented without overflow or truncation anyway. The check lives in the one templated function so it covers all four decimal widths, and testing the delta beforestd::absalso keepsstd::abs(INT32_MIN)out of reach.Are these changes tested?
Yes. The typed
Rescaletest now asserts a delta pastkMaxScalefails in both directions (a delta of exactlykMaxScaleis still exercised by the existing loops). A newDecimal128case parses1E-100, confirms scale 100, and checks the followingRescalereturns Invalid instead of reading past the table. Built with-fsanitize=bounds, the prior code aborts atGetScaleMultiplier; with the fix it returns cleanly.Are there any user-facing changes?
A decimal string whose scale exceeds the type maximum now returns an Invalid status from
Rescalerather than reading out of bounds. Valid inputs are unchanged.This PR contains a "Critical Fix". An out-of-bounds read of a static table, reachable from untrusted decimal strings (for example CSV values), that the
DCHECKbound does not catch in release builds.