Skip to content

printf: avoid panic on %s/%c field width above u16::MAX - #13883

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

printf: avoid panic on %s/%c field width above u16::MAX#13883
AlejandroCoronadoN wants to merge 1 commit into
uutils:mainfrom
AlejandroCoronadoN:fix-printf-string-char-width

Conversation

@AlejandroCoronadoN

Copy link
Copy Markdown

Summary

printf panics on a %s or %c field width above u16::MAX:

$ printf '%111111c' A
thread 'main' panicked at .../num_format or spec.rs: Formatting argument out of range
$ printf '%-101727s' hi
thread 'main' panicked at ...: Formatting argument out of range

Root cause

write_padded (in src/uucore/src/lib/features/format/spec.rs) pads with Rust's
dynamic-width formatting (write!(writer, "{: <padlen$}", "")). The standard formatter
caps a dynamic width at u16::MAX, so any padding above 65535 panics. The existing
// TODO: We need to not use Rust's formatting for aligning the output, so that we can just write bytes to stdout without panicking. in the %s arm points at exactly this.

Fix

Write the padding spaces directly as bytes via a small write_spaces helper (in 64-byte
chunks) instead of going through write!. This lets %s/%c field widths up to the
existing MAX_FORMAT_WIDTH work without panicking; the check_width guard is unchanged, so
widths above that still return a clean error. Fixes both %c and %s.

Verification

$ printf '%111111c' A | wc -c      # 111111, no panic
$ printf '%-101727s' hi | wc -c    # 101727, no panic
$ printf '[%5s][%-5s][%3c]' hi hi X   # "[   hi][hi   ][  X]"  (unchanged)
$ cargo test -p coreutils --test tests sub_string_char_width_above   # ok
$ cargo fmt -p uucore -- --check ; cargo clippy -p uucore --features format   # clean

Adds a regression test sub_string_char_width_above_u16_max_no_panic.

Fixes #12593. Fixes #12900.

write_padded padded with write!("{: <padlen$}"), which panics once the
dynamic width exceeds u16::MAX. Write the padding spaces directly as bytes so
that %s and %c field widths above that bound work, resolving the existing TODO
in the string arm. Adds a regression test.

Fixes uutils#12593. Fixes uutils#12900.
@codspeed-hq

codspeed-hq Bot commented Aug 12, 2026

Copy link
Copy Markdown

Merging this PR will degrade performance by 3.79%

⚠️ Different runtime environments detected

Some benchmarks with significant performance changes were compared across different runtime environments,
which may affect the accuracy of the results.

Open the report in CodSpeed to investigate

⚡ 2 improved benchmarks
❌ 1 regressed benchmark
✅ 344 untouched benchmarks
⏩ 46 skipped benchmarks1

Warning

Please fix the performance issues or acknowledge them on CodSpeed.

Performance Changes

Mode Benchmark BASE HEAD Efficiency
Simulation df_with_path 573.7 µs 704.7 µs -18.58%
Simulation du_max_depth_balanced_tree[(6, 4, 10)] 65.2 ms 62.3 ms +4.65%
Simulation du_summarize_balanced_tree[(5, 4, 10)] 16.8 ms 16.1 ms +4.51%

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-string-char-width (8b04fdc) with main (822aa83)

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.

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.

bug(printf): panics when using a number like %-101727s bug: printf panic again with '%111111c'

1 participant