Skip to content

nl: reject -w values > i32::MAX to avoid capacity-overflow panic#13348

Open
koopatroopa787 wants to merge 2 commits into
uutils:mainfrom
koopatroopa787:fix-nl-number-width-overflow
Open

nl: reject -w values > i32::MAX to avoid capacity-overflow panic#13348
koopatroopa787 wants to merge 2 commits into
uutils:mainfrom
koopatroopa787:fix-nl-number-width-overflow

Conversation

@koopatroopa787

Copy link
Copy Markdown
Contributor

Summary

printf '\n' | nl -w 9223372036854775807 aborts with:

thread 'main' panicked at 'capacity overflow'

because " ".repeat(settings.number_width + 1) in nl.rs triggers a capacity overflow when number_width is near usize::MAX.

GNU nl bounds --number-width/-w to [1, 2147483647] (i32::MAX) and exits with:

nl: invalid line number field width: '2147483648': Numerical result out of range

Fix

In src/uu/nl/src/helper.rs, tighten the existing NUMBER_WIDTH match arm to also reject values > i32::MAX, reusing the existing nl-error-invalid-line-width translation (which already says "Numerical result out of range"):

Some(num) if *num > 0 && *num <= i32::MAX as usize => settings.number_width = *num,
Some(num) => errs.push(translate!("nl-error-invalid-line-width", "value" => num.to_string())),

Tests

Two new tests in tests/by-util/test_nl.rs:

  • test_number_width_too_large — verifies -w 2147483648 fails with the expected error
  • test_number_width_max_i32 — verifies -w 2147483647 succeeds (boundary)

Fixes #13347

Copilot AI review requested due to automatic review settings July 10, 2026 13:36

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@oech3

oech3 commented Jul 10, 2026

Copy link
Copy Markdown
Contributor

Please validate it by clap native way instead.

@github-actions

github-actions Bot commented Jul 10, 2026

Copy link
Copy Markdown

GNU testsuite comparison:

GNU test failed: tests/tail/truncate. tests/tail/truncate is passing on 'main'. Maybe you have to rebase?
Skipping an intermittent issue tests/cut/bounded-memory (passes in this run but fails in the 'main' branch)
Skipping an intermittent issue tests/date/date-locale-hour (passes in this run but fails in the 'main' branch)
Skipping an intermittent issue tests/misc/io-errors (passes in this run but fails in the 'main' branch)
Skipping an intermittent issue tests/tail/retry (passes in this run but fails in the 'main' branch)
Skipping an intermittent issue tests/tail/tail-n0f (passes in this run but fails in the 'main' branch)

@sylvestre

Copy link
Copy Markdown
Contributor

And please fix the failing jobs

GNU nl bounds --number-width/-w to [1, 2147483647] and exits with
"Numerical result out of range" for larger values.  Without this guard,
`printf '\n' | nl -w 9223372036854775807` panics with
"capacity overflow" because `" ".repeat(number_width + 1)` overflows
isize.

Fixes uutils#13347
Per reviewer request, validate --number-width via clap's native range
parser (1..=2147483647) instead of a post-parse guard.

Changes:
- nl.rs: switch value_parser to clap::value_parser!(u64).range(1..=(i32::MAX as u64))
- helper.rs: simplify to a plain if-let since the range is already enforced;
  remove the now-unused translate! import
- test_nl.rs: update error-message assertions to match clap's range error format
  ("is not in 1..=2147483647") instead of the old translation string

Fixes uutils#13347
@koopatroopa787 koopatroopa787 force-pushed the fix-nl-number-width-overflow branch from a00dfde to 91586f2 Compare July 14, 2026 18:38
@koopatroopa787

Copy link
Copy Markdown
Contributor Author

Done — switched to clap's native range validator (clap::value_parser!(u64).range(1..=(i32::MAX as u64))) as suggested. The post-parse guard in helper.rs and the now-unused translate! import are both removed. Tests updated to check for clap's range error format (is not in 1..=2147483647). Also rebased on latest main.

@sylvestre

Copy link
Copy Markdown
Contributor

The lint job can be fixed

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.

nl: capacity-overflow abort on a large -w/--number-width

4 participants