pgrep, pkill: Add check for pattern length

pidwait already has a check for this, copy it to the common
process_matcher implementation.

Some tests need adjustment to not use such long fake names.
This commit is contained in:
Tuomas Tynkkynen
2025-02-10 23:26:08 +02:00
parent b22415c847
commit 74a2ffdddf
3 changed files with 28 additions and 2 deletions
+6
View File
@@ -78,6 +78,12 @@ pub fn get_match_settings(matches: &ArgMatches) -> UResult<Settings> {
));
}
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)
}
+11 -1
View File
@@ -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");
}
+11 -1
View File
@@ -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");
}