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: 5 additions & 2 deletions src/uu/nl/src/helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,11 @@ pub fn parse_options(settings: &mut crate::Settings, opts: &clap::ArgMatches) ->
}
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")),
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()
)),
}
if let Some(num) = opts.get_one::<u64>(options::JOIN_BLANK_LINES) {
settings.join_blank_lines = *num;
Expand Down
22 changes: 22 additions & 0 deletions tests/by-util/test_nl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,28 @@
}
}

#[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("Invalid line number field width: '2147483648': Numerical result out of range");
}
}

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