printf: avoid panic on i64::MIN dynamic field width - #13879
Open
AlejandroCoronadoN wants to merge 1 commit into
Open
printf: avoid panic on i64::MIN dynamic field width#13879AlejandroCoronadoN wants to merge 1 commit into
AlejandroCoronadoN wants to merge 1 commit into
Conversation
A negative %*d width resolved its magnitude with -(nb as isize), which overflows for i64::MIN since it has no positive counterpart. Use unsigned_abs so the value is well defined; the magnitude exceeds the maximum width, so printf now fails cleanly instead of aborting. Adds a regression test. Fixes uutils#13766.
|
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.
Summary
printf's%*ddynamic field width panics when the width argument isi64::MIN:(under
-C overflow-checks).Root cause
For a negative width,
resolve_asterisk_widthcomputes the magnitude as-(nb as isize). Whennbisi64::MINthere is no positive counterpart as ani64/isize, so the negation overflows.Fix
Use
nb.unsigned_abs()to obtain the magnitude, which is defined fori64::MIN.The resulting width exceeds
MAX_FORMAT_WIDTH, soprintfnow fails cleanly with"formatting width too large" (exit 1) instead of aborting. Normal
%*dwidths,including negative (left-aligned) ones, are unchanged. Adds a regression test.
Fixes #13766.