printf: avoid panic on field width above u16::MAX - #13878
printf: avoid panic on field width above u16::MAX#13878AlejandroCoronadoN wants to merge 1 commit into
Conversation
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.
Merging this PR will degrade performance by 3.69%
Warning Please fix the performance issues or acknowledge them on CodSpeed. Performance Changes
Tip Investigate this regression by commenting Comparing Footnotes
|
|
GNU testsuite comparison: |
Summary
printfpanics on a numeric field width aboveu16::MAX, e.g.printf %65536d 5:Root cause
write_outputpads the field width with Rust's dynamic-width formatting(
write!("{:>width$}")). The standard formatter caps dynamic width and precisionat
u16::MAX, so any width above 65535 panics. This is the same class of bug thatwas already fixed for precision in #12572 by padding manually (
zero_pad_to);the width path was never converted.
Fix
Add a small
pad_tohelper (mirroringzero_pad_to) and use it for the alignmentcases in
write_outputinstead of dynamic-widthwrite!. Field widths up to theexisting
MAX_FORMAT_WIDTH(1,000,000) now pad correctly, matching GNU; widthsabove that still return the existing "formatting width too large" error rather
than panicking.
Verification
printf %65536d 5now prints 65536 bytes (65535 spaces +5), matching GNU coreutils.format_int_large_width(right, left, and zero alignment); the existingformat_int_large_precisionstill passes.cargo fmtandcargo clippy -p uucore --features formatare clean.Fixes #13850.