numfmt: report oversized --padding instead of aborting#12561
Open
leeewee wants to merge 1 commit into
Open
Conversation
|
GNU testsuite comparison: |
70a8b32 to
e1f7963
Compare
sylvestre
reviewed
Jun 2, 2026
sylvestre
reviewed
Jun 2, 2026
sylvestre
reviewed
Jun 2, 2026
A large `--padding` made `pad_string` call `String::with_capacity(width)` with an unbounded, user-controlled width, aborting the process (allocation failure, or a `capacity overflow` panic for `--padding=-9223372036854775808`, whose magnitude exceeds `isize::MAX`). Validate the padding up front, before reading any input, and fail with a clean `memory exhausted` error (exit 1) — matching GNU. Doing it up front also matches GNU's behavior of not blocking on stdin and not silently succeeding on empty input. Fixes uutils#12560.
e1f7963 to
ae33b10
Compare
Contributor
Author
|
All done — addressed all three comments. |
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 #12560.
Problem
A large
--paddingmakespad_string(added to handle widths beyondformat!'s u16 cap) callString::with_capacity(width)with an unbounded,user-controlled width, crashing the process:
(
-9223372036854775808isisize::MIN; its magnitude isisize::MAX + 1,which overflows
with_capacity's capacity check deterministically.)GNU reports this cleanly and exits 1:
Fix
Validate the padding up front in
uumain— before reading any input — with afallible
try_reserve_exact, and fail withmemory exhausted(exit 1) onoverflow/allocation failure.
Doing the check up front (rather than lazily inside
pad_string) also matchesGNU's observable behavior:
(
numfmt --padding=-9223372036854775808no longer waits for input);memory exhaustedis surfaced asNumfmtError::IoError, which exits 1 —consistent with how uutils numfmt already matches GNU's other exit codes
(option errors → 1, input-conversion errors → 2).
Tests
Added
test_padding_too_large_fails_cleanly. Full numfmt suite passes(152 integration + unit tests);
cargo fmt/clippyclean.