Skip to content

printf: avoid panic on field width above u16::MAX - #13878

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

printf: avoid panic on field width above u16::MAX#13878
AlejandroCoronadoN wants to merge 1 commit into
uutils:mainfrom
AlejandroCoronadoN:fix-printf-large-width

Conversation

@AlejandroCoronadoN

Copy link
Copy Markdown

Summary

printf panics on a numeric field width above u16::MAX, e.g. printf %65536d 5:

thread 'main' panicked at .../num_format.rs: Formatting argument out of range

Root cause

write_output pads the field width with Rust's dynamic-width formatting
(write!("{:>width$}")). The standard formatter caps dynamic width and precision
at u16::MAX, so any width above 65535 panics. This is the same class of bug that
was already fixed for precision in #12572 by padding manually (zero_pad_to);
the width path was never converted.

Fix

Add a small pad_to helper (mirroring zero_pad_to) and use it for the alignment
cases in write_output instead of dynamic-width write!. Field widths up to the
existing MAX_FORMAT_WIDTH (1,000,000) now pad correctly, matching GNU; widths
above that still return the existing "formatting width too large" error rather
than panicking.

Verification

  • printf %65536d 5 now prints 65536 bytes (65535 spaces + 5), matching GNU coreutils.
  • New regression test format_int_large_width (right, left, and zero alignment); the existing format_int_large_precision still passes.
  • cargo fmt and cargo clippy -p uucore --features format are clean.

Fixes #13850.

Rust dynamic-width formatting (write!("{:>width$}")) panics with
"Formatting argument out of range" once the width exceeds u16::MAX, so a
valid field width such as %65536d aborted printf instead of padding the
output. Pad manually via a small helper, mirroring the existing zero_pad_to
that fixed the same problem for precision. Adds a regression test.

Fixes uutils#13850.
@codspeed-hq

codspeed-hq Bot commented Aug 11, 2026

Copy link
Copy Markdown

Merging this PR will degrade performance by 3.69%

❌ 1 regressed benchmark
✅ 346 untouched benchmarks
⏩ 46 skipped benchmarks1

Warning

Please fix the performance issues or acknowledge them on CodSpeed.

Performance Changes

Mode Benchmark BASE HEAD Efficiency
Simulation seq_formatted 160.1 ms 166.2 ms -3.69%

Tip

Investigate this regression by commenting @codspeedbot fix this regression on this PR, or directly use the CodSpeed MCP with your agent.


Comparing AlejandroCoronadoN:fix-printf-large-width (f235b19) with main (88f1944)2

Open in CodSpeed

Footnotes

  1. 46 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

  2. No successful run was found on main (822aa83) during the generation of this report, so 88f1944 was used instead as the comparison base. There might be some changes unrelated to this pull request in this report.

@github-actions

Copy link
Copy Markdown

GNU testsuite comparison:

Skip an intermittent issue tests/misc/io-errors (fails in this run but passes in the 'main' branch)
Skip an intermittent issue tests/tail/inotify-dir-recreate (fails in this run but passes in the 'main' branch)
Skipping an intermittent issue tests/cut/bounded-memory (passes in this run but fails in the 'main' branch)
Skipping an intermittent issue tests/pr/bounded-memory (passes in this run but fails in the 'main' branch)
Skipping an intermittent issue tests/tail/follow-name (passes in this run but fails in the 'main' branch)
Note: The gnu test tests/tail/tail-n0f 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: panics on a numeric field width above 65535 (%65536d)

1 participant