Skip to content

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

Open
koopatroopa787 wants to merge 1 commit 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 1 commit 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

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

Copy link
Copy Markdown

GNU testsuite comparison:

Skipping an intermittent issue tests/date/date-locale-hour (passes in this run but fails in the 'main' branch)
Skip an intermittent issue tests/pr/bounded-memory (was skipped on 'main', now failing)

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

3 participants