fix(printf): compute asterisk width magnitude in unsigned arithmetic - #13800
Open
bolverk wants to merge 1 commit into
Open
fix(printf): compute asterisk width magnitude in unsigned arithmetic#13800bolverk wants to merge 1 commit into
bolverk wants to merge 1 commit into
Conversation
|
GNU testsuite comparison: |
bolverk
force-pushed
the
fix/printf-asterisk-width-i64-min-overflow
branch
from
August 8, 2026 07:14
8728f09 to
2d76eba
Compare
Author
|
Updated the integration test to be portable: on 32-bit targets ( |
A negative '*' field width argument of i64::MIN used to panic with attempt to negate with overflow because |i64::MIN| = 2^63 is not representable in i64/isize. Use nb.unsigned_abs() to compute the magnitude in unsigned arithmetic, which can represent 2^63. Add a unit test for the width resolution and an end-to-end regression test verifying printf fails gracefully with a write error instead of panicking. Fixes uutils#13766
bolverk
force-pushed
the
fix/printf-asterisk-width-i64-min-overflow
branch
from
August 8, 2026 08:27
2d76eba to
2864ff4
Compare
sylvestre
reviewed
Aug 8, 2026
|
|
||
| #[test] | ||
| fn test_asterisk_width_i64_min_no_panic() { | ||
| // Regression test for https://github.com/uutils/coreutils/issues/13766 |
Contributor
There was a problem hiding this comment.
Please make the comment shorter
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 #13766
Summary
A negative
*field-width argument ofi64::MIN(printf '%*d' -9223372036854775808 1) maderesolve_asterisk_widthpanic withattempt to negate with overflow: the old code computed the magnitude as-(nb as isize), and|i64::MIN| = 2^63is not representable ini64/isize.The fix computes the magnitude with
nb.unsigned_abs(), which is designed for exactly this case and returns2^63in unsigned (u64) arithmetic.Behavior
panic/ exit 134 (attempt to negate with overflow)2^63exceedsMAX_FORMAT_WIDTH, soprintffails gracefully withprintf: write error: formatting width too large(exit 1), matching the handling of other oversized widths.Verification
Formal verification of the width-resolution arithmetic was done in Dafny (4.11): it specifies the intended behavior (negative width ⇒ left-align with magnitude), proves
|i64::MIN|overflowsi64but fitsu64/usize, and proves theunsigned_abs()-based implementation is overflow-free and matches the spec. Proof:15 verified, 0 errors.Tests
resolve_asterisk_width::asterisk_i64_min_width(bothSignedIntandUnparsedargument forms)test_printf::test_asterisk_width_i64_min_no_panicprintfsuite passes: 131 passed, 0 failed, 1 ignored;cargo fmtandclippyclean.