Skip to content

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

Open
MadeNavaneeth wants to merge 1 commit into
uutils:mainfrom
MadeNavaneeth:fix/printf-width-u16-max
Open

printf: avoid panic on numeric field width above u16::MAX#14184
MadeNavaneeth wants to merge 1 commit into
uutils:mainfrom
MadeNavaneeth:fix/printf-width-u16-max

Conversation

@MadeNavaneeth

Copy link
Copy Markdown
Contributor

Fixes #13850.

printf '%65536d' 5 panicked with "Formatting argument out of range" because Rust's write! macro only accepts a width that fits in a u16. The boundary is exactly u16::MAX: %65535d worked, %65536d panicked (and any width up to the existing MAX_FORMAT_WIDTH guard of 1_000_000).

This replaces the write!-based padding in num_format::write_output with manual chunked space/zero padding (write_spaces/write_zeros), while keeping the existing check_width guard so widths above MAX_FORMAT_WIDTH still fail gracefully (matching GNU's behavior of erroring rather than hanging).

Verified: the new regression tests test_width_65536_does_not_panic and test_width_above_u16_max_succeeds pass, and the full test_printf suite (142 tests) passes with no regressions.

Widths between 65536 and MAX_FORMAT_WIDTH previously panicked with
"Formatting argument out of range" because Rust's write! macro only
accepts a u16 width. Replace the write! padding with manual space/zero
padding (chunked) and keep the existing check_width guard, so widths
above u16::MAX now format correctly and widths above the 1M guard still
fail gracefully.

Fixes uutils#13850
@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)
Congrats! The gnu test tests/csplit/csplit-heap is now passing!

@codspeed-hq

codspeed-hq Bot commented Aug 27, 2026

Copy link
Copy Markdown

Merging this PR will degrade performance by 10.16%

⚠️ 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

❌ 3 regressed benchmarks
✅ 358 untouched benchmarks
⏩ 50 skipped benchmarks1

Warning

Please fix the performance issues or acknowledge them on CodSpeed.

Performance Changes

Mode Benchmark BASE HEAD Efficiency
Simulation split_lines 9.2 ms 10.6 ms -13.16%
Simulation split_numeric_suffix 9.7 ms 11.1 ms -12.55%
Simulation split_bytes 644.5 µs 675.1 µs -4.53%

Tip

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


Comparing MadeNavaneeth:fix/printf-width-u16-max (dd3e4ae) with main (0d8310c)

Open in CodSpeed

Footnotes

  1. 50 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.

printf: panics on a numeric field width above 65535 (%65536d)

1 participant