Skip to content

Commit f0c7301

Browse files
committed
pgrep: optimize length detection
When calculating the length, the ^$ character needs to be removed if the -x parameter is present. Closes: #635
1 parent e74801e commit f0c7301

2 files changed

Lines changed: 41 additions & 2 deletions

File tree

src/uu/pgrep/src/process_matcher.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,20 @@ pub fn get_match_settings(matches: &ArgMatches) -> UResult<Settings> {
151151
pub fn find_matching_pids(settings: &Settings) -> UResult<Vec<ProcessInformation>> {
152152
let mut pids = collect_matched_pids(settings)?;
153153

154+
let is_long_match = if settings.exact {
155+
settings
156+
.regex
157+
.as_str()
158+
.trim_matches('^')
159+
.trim_matches('$')
160+
.len()
161+
> 15
162+
} else {
163+
settings.regex.as_str().len() > 15
164+
};
165+
154166
if pids.is_empty() {
155-
if !settings.full && settings.regex.as_str().len() > 15 {
167+
if !settings.full && is_long_match {
156168
let msg = format!("pattern that searches for process name longer than 15 characters will result in zero matches\n\
157169
Try `{} -f' option to match against the complete command line.", uucore::util_name());
158170
return Err(USimpleError::new(1, msg));

tests/by-util/test_pgrep.rs

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -654,7 +654,6 @@ fn test_pidfile_fcntl_locked() {
654654

655655
// spawn a flock process that locks the file
656656
let mut flock_process = Command::new("flock")
657-
.arg("--fcntl")
658657
.arg(temp_file.path())
659658
.arg("sleep")
660659
.arg("2")
@@ -718,3 +717,31 @@ fn test_env_multiple_filters() {
718717
// Multiple filters use OR logic
719718
new_ucmd!().arg("--env=PATH,NONEXISTENT").succeeds();
720719
}
720+
721+
#[test]
722+
#[cfg(target_os = "linux")]
723+
fn test_exact_long_pattern_no_match() {
724+
new_ucmd!()
725+
.arg("-x")
726+
.arg("12345678901234")
727+
.fails()
728+
.code_is(1);
729+
}
730+
731+
#[test]
732+
fn test_pattern_longer_than_15_characters() {
733+
new_ucmd!()
734+
.arg("1234567890123456")
735+
.fails()
736+
.code_is(1)
737+
.stderr_contains("longer than 15 characters");
738+
}
739+
740+
#[test]
741+
#[cfg(target_os = "linux")]
742+
fn test_pool_workqueue_release() {
743+
new_ucmd!()
744+
.arg("pool_workqueue_release")
745+
.succeeds()
746+
.stdout_matches(&Regex::new(MULTIPLE_PIDS).unwrap());
747+
}

0 commit comments

Comments
 (0)