numfmt: reject an oversized --from-unit/--to-unit instead of panicking#13484
Merged
Conversation
parse_unit_size computed the unit size as `n * multiplier` with an unchecked multiplication. A large numeric part with a suffix overflows usize: when the product wraps to exactly 0 (e.g. `--to-unit=18014398509481984Ki`, 2^54 * 1024 = 2^64), the zero unit later reaches `integer % to_unit` and aborts with a remainder-by-zero; a merely-huge value overflows the multiply itself under overflow-checks. Use `checked_mul` so an out-of-range product falls through to the existing "invalid unit size" error (exit 1), matching GNU numfmt, whose `unit_to_umax` rejects a value that overflows uintmax_t.
|
GNU testsuite comparison: |
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.
Fixes #13481.
parse_unit_sizecomputed the unit size asn * multiplierwith an unchecked multiplication. A large numeric part with a suffix overflowsusize, surfacing as two panics:0(e.g.--to-unit=18014398509481984Ki,2^54 * 1024 = 2^64), the zero unit later reachesinteger % to_unitand aborts (exit 134);--from-unit=18446744073709551615K) overflows the multiply itself underoverflow-checks, and wraps silently in the default release build.This uses
checked_mulso an out-of-range product falls through to the existinginvalid unit sizeerror (exit 1) — matching GNU numfmt, whoseunit_to_umaxrejects any value that overflowsuintmax_t:Extended
test_invalid_unit_sizewith both overflow inputs.