Skip to content

printf: avoid panic on i64::MIN dynamic field width - #13879

Open
AlejandroCoronadoN wants to merge 1 commit into
uutils:mainfrom
AlejandroCoronadoN:fix-printf-asterisk-width-overflow
Open

printf: avoid panic on i64::MIN dynamic field width#13879
AlejandroCoronadoN wants to merge 1 commit into
uutils:mainfrom
AlejandroCoronadoN:fix-printf-asterisk-width-overflow

Conversation

@AlejandroCoronadoN

Copy link
Copy Markdown

Summary

printf's %*d dynamic field width panics when the width argument is i64::MIN:

$ printf '%*d' -9223372036854775808 1
thread 'main' panicked at .../spec.rs: attempt to negate with overflow
$ echo $?
134

(under -C overflow-checks).

Root cause

For a negative width, resolve_asterisk_width computes the magnitude as
-(nb as isize). When nb is i64::MIN there is no positive counterpart as an
i64/isize, so the negation overflows.

Fix

Use nb.unsigned_abs() to obtain the magnitude, which is defined for i64::MIN.
The resulting width exceeds MAX_FORMAT_WIDTH, so printf now fails cleanly with
"formatting width too large" (exit 1) instead of aborting. Normal %*d widths,
including negative (left-aligned) ones, are unchanged. Adds a regression test.

Fixes #13766.

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.
@github-actions

Copy link
Copy Markdown

GNU testsuite comparison:

Skip an intermittent issue tests/date/date-locale-hour (fails in this run but passes in the 'main' branch)
Skip an intermittent issue tests/misc/io-errors (fails in this run but passes in the 'main' branch)
Skipping an intermittent issue tests/tail/symlink (passes in this run but fails in the 'main' branch)
Congrats! The gnu test tests/tail/pipe-f2 is no longer failing!
Note: The gnu test tests/cut/cut-huge-range is now being skipped but was previously passing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

printf: arithmetic overflow (overflow-checks) on a %*d dynamic field width of i64::MIN

1 participant