Skip to content

printf: handle field widths above u16::MAX without panicking - #14162

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

printf: handle field widths above u16::MAX without panicking#14162
MadeNavaneeth wants to merge 1 commit into
uutils:mainfrom
MadeNavaneeth:fix/printf-large-field-width

Conversation

@MadeNavaneeth

Copy link
Copy Markdown
Contributor

Summary

Fixes #13850

The problem

Rusts write! macro panics with "Formatting argument out of range" when the format width exceeds u16::MAX (65535). GNU printf accepts arbitrarily large field widths, so %65536d, %1000000d, etc. must not panic.

$ printf "%65536d" 5
thread main panicked at num_format.rs:781: Formatting argument out of range

The fix

Replace the write! macro calls in write_output() with manual padding using chunk-based write_spaces/write_zeros helpers — the same approach already used in spec.rs for %s/%c formatting.

Verification

  • ✅ 139/139 printf tests pass
  • ✅ 5/5 uucore format tests pass
  • cargo clippy -p uucore -- -D warnings clean

@codspeed-hq

codspeed-hq Bot commented Aug 26, 2026

Copy link
Copy Markdown

Merging this PR will degrade performance by 5.52%

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

⚡ 1 improved benchmark
❌ 6 regressed benchmarks
✅ 350 untouched benchmarks
🆕 4 new benchmarks
⏩ 50 skipped benchmarks1

Warning

Please fix the performance issues or acknowledge them on CodSpeed.

Performance Changes

Mode Benchmark BASE HEAD Efficiency
Simulation true_consecutive_calls 294.7 ns 350.2 ns -15.86%
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 µs -4.52%
Simulation uniq_case_insensitive[10000] 2.8 ms 3 ms -4.17%
Simulation uniq_with_count[10000] 3.7 ms 3.8 ms -3.31%
Simulation false_consecutive_calls 350.2 ns 294.7 ns +18.85%
🆕 Memory base16_encode_to_dev_null N/A 22.6 KB N/A
🆕 Memory base16_encode_to_file N/A 22.6 KB N/A
🆕 Simulation base16_encode_to_dev_null N/A 184.3 ms N/A
🆕 Simulation base16_encode_to_file N/A 184.2 ms N/A

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-large-field-width (3ee9e26) with main (1a6fb19)2

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.

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

@sylvestre

Copy link
Copy Markdown
Contributor

it needs a test
thanks

@github-actions

github-actions Bot commented Aug 26, 2026

Copy link
Copy Markdown

GNU testsuite comparison:

Skip an intermittent issue tests/cut/bounded-memory (fails in this run but passes in the 'main' branch)
Skipping an intermittent issue tests/tail/retry (passes in this run but fails in the 'main' branch)
Note: The gnu test tests/seq/seq-epipe is now being skipped but was previously passing.

@MadeNavaneeth
MadeNavaneeth force-pushed the fix/printf-large-field-width branch 2 times, most recently from 85f5fec to c1446b6 Compare August 27, 2026 04:16
Rust's write! macro panics with "Formatting argument out of range" when
the width exceeds u16::MAX (65535). GNU printf accepts arbitrarily large
field widths, so %65536d, %1000000d, etc. must not panic.

Replace the write! macro calls in write_output() with manual padding
using chunk-based write_spaces/write_zeros helpers, matching the
approach already used in spec.rs for %s/%c formatting.

Fixes uutils#13850
@MadeNavaneeth
MadeNavaneeth force-pushed the fix/printf-large-field-width branch from c1446b6 to 3ee9e26 Compare August 27, 2026 05:03
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)

2 participants