diff --git a/src/uu/pgrep/src/process_matcher.rs b/src/uu/pgrep/src/process_matcher.rs index e547246..7e3ca24 100644 --- a/src/uu/pgrep/src/process_matcher.rs +++ b/src/uu/pgrep/src/process_matcher.rs @@ -78,6 +78,12 @@ pub fn get_match_settings(matches: &ArgMatches) -> UResult { )); } + if !settings.full && pattern.len() >= 15 { + let msg = format!("pattern that searches for process name longer than 15 characters will result in zero matches\n\ + Try `{} -f' option to match against the complete command line.", uucore::util_name()); + return Err(USimpleError::new(1, msg)); + } + Ok(settings) } diff --git a/tests/by-util/test_pgrep.rs b/tests/by-util/test_pgrep.rs index 19d73f0..0f33494 100644 --- a/tests/by-util/test_pgrep.rs +++ b/tests/by-util/test_pgrep.rs @@ -31,7 +31,7 @@ fn test_no_args() { #[test] fn test_non_matching_pattern() { new_ucmd!() - .arg("THIS_PATTERN_DOES_NOT_MATCH") + .arg("NONMATCHING") .fails() .code_is(1) .no_output(); @@ -368,3 +368,13 @@ fn test_invalid_signal() { .fails() .stderr_contains("Unknown signal 'foo'"); } + +#[test] +#[cfg(target_os = "linux")] +fn test_too_long_pattern() { + new_ucmd!() + .arg("THIS_IS_OVER_16_CHARS") + .fails() + .code_is(1) + .stderr_contains("pattern that searches for process name longer than 15 characters will result in zero matches"); +} diff --git a/tests/by-util/test_pkill.rs b/tests/by-util/test_pkill.rs index 801e617..f9686dd 100644 --- a/tests/by-util/test_pkill.rs +++ b/tests/by-util/test_pkill.rs @@ -20,7 +20,7 @@ fn test_no_args() { #[test] fn test_non_matching_pattern() { new_ucmd!() - .arg("THIS_PATTERN_DOES_NOT_MATCH") + .arg("NONMATCHING") .fails() .code_is(1) .no_output(); @@ -60,3 +60,13 @@ fn test_inverse() { fn test_help() { new_ucmd!().arg("--help").succeeds(); } + +#[test] +#[cfg(target_os = "linux")] +fn test_too_long_pattern() { + new_ucmd!() + .arg("THIS_IS_OVER_16_CHARS") + .fails() + .code_is(1) + .stderr_contains("pattern that searches for process name longer than 15 characters will result in zero matches"); +}