diff --git a/src/uu/split/src/split.rs b/src/uu/split/src/split.rs index 9ad81481ed..88478182f4 100644 --- a/src/uu/split/src/split.rs +++ b/src/uu/split/src/split.rs @@ -76,6 +76,7 @@ fn handle_obsolete(args: impl uucore::Args) -> (Vec, Option) { let mut obs_lines = None; let mut preceding_long_opt_req_value = false; let mut preceding_short_opt_req_value = false; + let mut after_double_dash = false; let filtered_args = args .filter_map(|os_slice| { @@ -84,6 +85,7 @@ fn handle_obsolete(args: impl uucore::Args) -> (Vec, Option) { &mut obs_lines, &mut preceding_long_opt_req_value, &mut preceding_short_opt_req_value, + &mut after_double_dash, ) }) .collect(); @@ -98,9 +100,19 @@ fn filter_args( obs_lines: &mut Option, preceding_long_opt_req_value: &mut bool, preceding_short_opt_req_value: &mut bool, + after_double_dash: &mut bool, ) -> Option { let filter: Option; if let Some(slice) = os_slice.to_str() { + if *after_double_dash { + // Past `--` everything is an operand, so `split -- -1` names a file + // rather than setting the line count. + return Some(OsString::from(slice)); + } + if slice == "--" { + *after_double_dash = true; + return Some(OsString::from(slice)); + } if should_extract_obs_lines( slice, *preceding_long_opt_req_value, diff --git a/src/uu/uniq/src/uniq.rs b/src/uu/uniq/src/uniq.rs index 27b9d4473b..d9dfc2cc21 100644 --- a/src/uu/uniq/src/uniq.rs +++ b/src/uu/uniq/src/uniq.rs @@ -375,6 +375,7 @@ fn handle_obsolete(args: impl uucore::Args) -> (Vec, Option, Op let mut skip_chars_old = None; let mut preceding_long_opt_req_value = false; let mut preceding_short_opt_req_value = false; + let mut after_double_dash = false; let filtered_args = args .filter_map(|os_slice| { @@ -384,6 +385,7 @@ fn handle_obsolete(args: impl uucore::Args) -> (Vec, Option, Op &mut skip_chars_old, &mut preceding_long_opt_req_value, &mut preceding_short_opt_req_value, + &mut after_double_dash, ) }) .collect(); @@ -403,9 +405,19 @@ fn filter_args( skip_chars_old: &mut Option, preceding_long_opt_req_value: &mut bool, preceding_short_opt_req_value: &mut bool, + after_double_dash: &mut bool, ) -> Option { let filter: Option; if let Some(slice) = os_slice.to_str() { + if *after_double_dash { + // Past `--` everything is an operand, so `uniq -- -1` names a file + // rather than skipping a field. + return Some(OsString::from(slice)); + } + if slice == "--" { + *after_double_dash = true; + return Some(OsString::from(slice)); + } if should_extract_obs_skip_fields( slice, *preceding_long_opt_req_value, diff --git a/tests/by-util/test_split.rs b/tests/by-util/test_split.rs index 345ce27145..0e31278e95 100644 --- a/tests/by-util/test_split.rs +++ b/tests/by-util/test_split.rs @@ -2218,3 +2218,13 @@ split: invalid number of bytes: '7zq' .stderr_is("split: invalid number of bytes: '7zq'\n"); } } + +#[test] +fn test_obsolete_lines_not_read_after_double_dash() { + // After `--` there are no more options, so `-1` names a file rather than + // being taken as the obsolete `split -1` line-count spelling. + new_ucmd!() + .args(&["--", "-1"]) + .fails() + .stderr_contains("cannot open '-1' for reading"); +} diff --git a/tests/by-util/test_uniq.rs b/tests/by-util/test_uniq.rs index a50534194d..db44a60cc9 100644 --- a/tests/by-util/test_uniq.rs +++ b/tests/by-util/test_uniq.rs @@ -1220,3 +1220,13 @@ fn test_failed_write_is_reported() { .fails() .stderr_is("uniq: write error: No space left on device\n"); } + +#[test] +fn test_obsolete_skip_fields_not_read_after_double_dash() { + // After `--` there are no more options, so `-1` names a file rather than + // being taken as the obsolete "skip 1 field" spelling. + new_ucmd!() + .args(&["--", "-1"]) + .fails() + .stderr_contains("-1"); +}