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
4 changes: 3 additions & 1 deletion src/uu/uniq/src/uniq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,9 @@ fn opt_parsed(opt_name: &str, matches: &ArgMatches) -> UResult<Option<usize>> {
Some(arg_str) => match arg_str.parse::<usize>() {
Ok(v) => Ok(Some(v)),
Err(e) => match e.kind() {
IntErrorKind::PosOverflow => Ok(Some(usize::MAX)),
IntErrorKind::PosOverflow if arg_str.chars().all(|c| c.is_ascii_digit()) => {

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.

this now rejects +18446744073709551616 while +5 is accepted (and GNU accepts both)
please skip a leading + before the digit check and add a test for it

Ok(Some(usize::MAX))
}
_ => Err(USimpleError::new(
1,
translate!("uniq-error-invalid-argument", "opt_name" => opt_name, "arg" => arg_str.maybe_quote()),
Expand Down
29 changes: 29 additions & 0 deletions tests/by-util/test_uniq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1351,3 +1351,32 @@ fn test_obsolete_skip_fields_not_read_after_double_dash() {
.fails()
.stderr_contains("-1");
}

#[test]
fn test_overflowing_numeric_options_with_trailing_garbage_rejected() {
// Large values that overflow usize but have trailing non-digits must be rejected (issue #16122)
new_ucmd!()

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.

almost the same code 3 times, could you please loop over ["-f", "-s", "-w"]?

.args(&["-f", "18446744073709551616x"])
.pipe_in("first\nsecond\nthird\n")
.fails_with_code(1)
.stderr_contains("Invalid argument");

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.

please check the full error message, not just "Invalid argument"


new_ucmd!()
.args(&["-s", "18446744073709551616x"])
.pipe_in("first\nsecond\nthird\n")
.fails_with_code(1)
.stderr_contains("Invalid argument");

new_ucmd!()
.args(&["-w", "18446744073709551616x"])
.pipe_in("first\nsecond\nthird\n")
.fails_with_code(1)
.stderr_contains("Invalid argument");

// Purely numeric overflowing values remain accepted (clamped to usize::MAX)
new_ucmd!()
.args(&["-f", "18446744073709551616"])
.pipe_in("first\nsecond\nthird\n")
.succeeds()
.stdout_is("first\n");
}
Loading