Skip to content

printf: fix panic on width >65535 for numeric conversions - #14185

Closed
Talha-Dmr wants to merge 1 commit into
uutils:mainfrom
Talha-Dmr:fix/printf-large-width-num-format
Closed

printf: fix panic on width >65535 for numeric conversions#14185
Talha-Dmr wants to merge 1 commit into
uutils:mainfrom
Talha-Dmr:fix/printf-large-width-num-format

Conversation

@Talha-Dmr

Copy link
Copy Markdown

Fixes #13850

printf panicked with 'Formatting argument out of range' for numeric widths above 65535. Rust's formatter limits width to u16::MAX. Use manual padding in write_output instead of format! dynamic width, similar to zero_pad_to for large precisions.

Test: printf '%65536d' 5 now outputs 65536 bytes (was panic), printf tests pass.

@Talha-Dmr
Talha-Dmr force-pushed the fix/printf-large-width-num-format branch from 878d191 to c8f0bc3 Compare August 27, 2026 15:44
@Talha-Dmr
Talha-Dmr marked this pull request as ready for review August 27, 2026 15:44
Copilot AI lite review requested due to automatic review settings August 27, 2026 15:44

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Warning

Copilot couldn't run its full agentic review because it didn't start before the timeout. Make sure your repository has a runner available, or add a copilot-code-review.yml file specifying one with the runs-on attribute. See the docs for more details.

Fixes printf panics when formatting numeric conversions with widths larger than u16::MAX by avoiding Rust’s dynamic-width formatter limitation and adding regression coverage for large numeric widths.

Changes:

  • Add regression tests ensuring %65536d / %65536x no longer panic and produce correctly padded output.
  • Replace write! dynamic-width formatting in numeric output with manual padding logic for large widths.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
tests/by-util/test_printf.rs Adds regression tests for numeric widths above u16::MAX to prevent panics.
src/uucore/src/lib/features/format/num_format.rs Implements manual padding/zero-padding in write_output to support large widths without formatter panics.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread src/uucore/src/lib/features/format/num_format.rs Outdated
Comment thread src/uucore/src/lib/features/format/num_format.rs Outdated
Comment thread src/uucore/src/lib/features/format/num_format.rs Outdated
Copilot AI review requested due to automatic review settings August 27, 2026 16:08
@Talha-Dmr
Talha-Dmr force-pushed the fix/printf-large-width-num-format branch from c8f0bc3 to 56fa417 Compare August 27, 2026 16:08

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

Comment thread src/uucore/src/lib/features/format/num_format.rs Outdated
Comment thread src/uucore/src/lib/features/format/num_format.rs
Copilot AI review requested due to automatic review settings August 27, 2026 16:28
@Talha-Dmr
Talha-Dmr force-pushed the fix/printf-large-width-num-format branch from 56fa417 to 34f61b0 Compare August 27, 2026 16:28

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.

Comment thread src/uucore/src/lib/features/format/num_format.rs Outdated
Comment thread src/uucore/src/lib/features/format/num_format.rs Outdated
Comment thread src/uucore/src/lib/features/format/num_format.rs
Comment thread src/uucore/src/lib/features/format/num_format.rs Outdated
Copilot AI review requested due to automatic review settings August 27, 2026 16:49
@Talha-Dmr
Talha-Dmr force-pushed the fix/printf-large-width-num-format branch from 34f61b0 to 838c2cd Compare August 27, 2026 16:49
@sylvestre

Copy link
Copy Markdown
Contributor

please close the issues that have been addressed

@Talha-Dmr

Copy link
Copy Markdown
Author

Thanks @sylvestre — resolved the addressed Copilot threads (chunked padding, DRY/unused var cleanup, checked_add + target width check, generic pad_byte handling, and InvalidInput kind). The remaining &mut writer threads were kept as is — write_output(mut writer: impl Write) is owned, so &mut writer is a single &mut W layer — but happy to change the outer signature to &mut W if you prefer.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

Comment thread src/uucore/src/lib/features/format/num_format.rs Outdated
Comment thread src/uucore/src/lib/features/format/num_format.rs Outdated
Copilot AI review requested due to automatic review settings August 27, 2026 17:04
@Talha-Dmr
Talha-Dmr force-pushed the fix/printf-large-width-num-format branch from 838c2cd to 87320d2 Compare August 27, 2026 17:04

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 5 comments.

Comment thread src/uucore/src/lib/features/format/num_format.rs Outdated
Comment thread src/uucore/src/lib/features/format/num_format.rs
Comment thread src/uucore/src/lib/features/format/num_format.rs
Comment thread src/uucore/src/lib/features/format/num_format.rs
Comment thread src/uucore/src/lib/features/format/num_format.rs
@github-actions

github-actions Bot commented Aug 27, 2026

Copy link
Copy Markdown

GNU testsuite comparison:

GNU test failed: tests/cp/cp-a-selinux. tests/cp/cp-a-selinux is passing on 'main'. Maybe you have to rebase?
Skip an intermittent issue tests/cut/bounded-memory (fails in this run but passes in the 'main' branch)
Skip an intermittent issue tests/tail/tail-n0f (fails in this run but passes in the 'main' branch)
Congrats! The gnu test tests/tail/flush-initial is no longer failing!

@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 Talha-Dmr:fix/printf-large-width-num-format (38a5d7b) 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.

Copilot AI review requested due to automatic review settings August 27, 2026 18:23
@Talha-Dmr
Talha-Dmr force-pushed the fix/printf-large-width-num-format branch from 87320d2 to 73f9755 Compare August 27, 2026 18:23

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

Comment thread src/uucore/src/lib/features/format/num_format.rs Outdated
Copilot AI review requested due to automatic review settings August 27, 2026 18:30
@Talha-Dmr
Talha-Dmr force-pushed the fix/printf-large-width-num-format branch from 73f9755 to bc7a726 Compare August 27, 2026 18:30

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated no new comments.

Avoid Rust formatter panic 'Formatting argument out of range' when width
exceeds u16::MAX. Use manual chunked padding in write_output instead of
format! dynamic width, similar to zero_pad_to for large precisions. Handles
Left, RightSpace and RightZero alignments up to MAX_FORMAT_WIDTH without
allocating a large buffer. Avoid allocation in sign branch and validate
requested width directly. Fixes uutils#13850.
Copilot AI review requested due to automatic review settings August 27, 2026 20:12
@Talha-Dmr
Talha-Dmr force-pushed the fix/printf-large-width-num-format branch from bc7a726 to 38a5d7b Compare August 27, 2026 20:12

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated no new comments.

@cakebaker

Copy link
Copy Markdown
Contributor

Duplicate of #13857

@cakebaker cakebaker marked this as a duplicate of #13857 Aug 28, 2026
@cakebaker cakebaker closed this Aug 28, 2026
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)

4 participants