Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions src/uu/nl/src/helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use std::ffi::OsString;

use crate::options;
use uucore::translate;

// parse_options loads the options into the settings, returning an array of
// error messages.
Expand Down Expand Up @@ -61,10 +60,8 @@ pub fn parse_options(settings: &mut crate::Settings, opts: &clap::ArgMatches) ->
Some(Ok(style)) => settings.footer_numbering = style,
Some(Err(message)) => errs.push(message),
}
match opts.get_one::<usize>(options::NUMBER_WIDTH) {
None => {}
Some(num) if *num > 0 => settings.number_width = *num,
Some(_) => errs.push(translate!("nl-error-invalid-line-width", "value" => "0")),
if let Some(&num) = opts.get_one::<u64>(options::NUMBER_WIDTH) {
settings.number_width = num as usize;
}
if let Some(num) = opts.get_one::<u64>(options::JOIN_BLANK_LINES) {
settings.join_blank_lines = *num;
Expand Down
2 changes: 1 addition & 1 deletion src/uu/nl/src/nl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ pub fn uu_app() -> Command {
.long(options::NUMBER_WIDTH)
.help(translate!("nl-help-number-width"))
.value_name("NUMBER")
.value_parser(clap::value_parser!(usize)),
.value_parser(clap::value_parser!(u64).range(1..=(i32::MAX as u64))),
)
}

Expand Down
24 changes: 23 additions & 1 deletion tests/by-util/test_nl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,10 +183,32 @@
new_ucmd!()
.arg(arg)
.fails()
.stderr_contains("Invalid line number field width: ‘0’: Numerical result out of range");
.stderr_contains("is not in 1..=2147483647");
}
}

#[test]
fn test_number_width_too_large() {
// Values > i32::MAX must be rejected to match GNU nl behaviour and avoid

Check failure on line 192 in tests/by-util/test_nl.rs

View workflow job for this annotation

GitHub Actions / Style/spelling (ubuntu-latest, feat_os_unix)

ERROR: `cspell`: Unknown word 'behaviour' (file:'tests/by-util/test_nl.rs', line:192)
// a capacity-overflow panic in " ".repeat(number_width + 1).
for arg in ["-w2147483648", "--number-width=2147483648"] {
new_ucmd!()
.arg(arg)
.pipe_in("")
.fails()
.stderr_contains("is not in 1..=2147483647");
}
}

#[test]
fn test_number_width_max_i32() {
// i32::MAX (2147483647) is the largest value GNU nl accepts; it must not panic.
new_ucmd!()
.args(&["-w", "2147483647"])
.pipe_in("")
.succeeds();
}

#[test]
fn test_invalid_number_width() {
for arg in ["-winvalid", "--number-width=invalid"] {
Expand Down
Loading