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
6 changes: 4 additions & 2 deletions src/uu/nl/src/helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,11 @@ 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) {
// The upper bound is enforced by clap (see the argument definition); only
// the zero case is reported here, to match GNU's message for it.
match opts.get_one::<u64>(options::NUMBER_WIDTH) {
None => {}
Some(num) if *num > 0 => settings.number_width = *num,
Some(num) if *num > 0 => settings.number_width = *num as usize,
Some(_) => errs.push(translate!("nl-error-invalid-line-width", "value" => "0")),
}
if let Some(num) = opts.get_one::<u64>(options::JOIN_BLANK_LINES) {
Expand Down
5 changes: 4 additions & 1 deletion src/uu/nl/src/nl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,10 @@ pub fn uu_app() -> Command {
.long(options::NUMBER_WIDTH)
.help(translate!("nl-help-number-width"))
.value_name("NUMBER")
.value_parser(clap::value_parser!(usize)),
// Bound the width to a C int like GNU. This rejects a huge width
// up front instead of letting a later `" ".repeat(width + 1)`
// abort with a capacity overflow (#13347).

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.

If test is added already, the comment is unnecessary.

.value_parser(clap::value_parser!(u64).range(..=i32::MAX as u64)),

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.

Suggested change
.value_parser(clap::value_parser!(u64).range(..=i32::MAX as u64)),
.value_parser(clap::value_parser!(u64).range(1..=i32::MAX as u64)),

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.

We don't make stderr 100% same with GNU too.

)
}

Expand Down
11 changes: 11 additions & 0 deletions tests/by-util/test_nl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,17 @@ fn test_number_width_zero() {
}
}

#[test]
fn test_number_width_above_int_max_is_rejected() {
// A field width above i32::MAX is rejected up front by clap, matching GNU's
// C-int bound, instead of aborting with a capacity overflow (#13347).
new_ucmd!()
.args(&["-w", "2147483648"])
.pipe_in("x\n")
.fails()
.stderr_contains("is not in 0..=2147483647");

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.

I would drop .stderr_contains and kep fails only since it is clap's message instead of GNU's one.

}

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