Skip to content

Commit b03e8ce

Browse files
authored
Merge pull request #328 from dezgeg/pid_match_bugfix
process_matcher: Fix accidentally matching against pid
2 parents a2d49d6 + 43f7412 commit b03e8ce

File tree

2 files changed

+10
-9
lines changed

2 files changed

+10
-9
lines changed

src/uu/pgrep/src/process_matcher.rs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -160,18 +160,12 @@ fn collect_matched_pids(settings: &Settings) -> Vec<ProcessInformation> {
160160
name.into()
161161
};
162162
let pattern_matched = {
163-
let want = if settings.exact {
164-
// Equals `Name` in /proc/<pid>/status
165-
// The `unwrap` operation must succeed
166-
// because the REGEX has been verified as correct in `uumain`.
167-
&name
168-
} else if settings.full {
163+
let want = if settings.full {
169164
// Equals `cmdline` in /proc/<pid>/cmdline
170165
&pid.cmdline
171166
} else {
172-
// From manpage:
173-
// The process name used for matching is limited to the 15 characters present in the output of /proc/pid/stat.
174-
&pid.proc_stat()[..15]
167+
// Equals `Name` in /proc/<pid>/status
168+
&name
175169
};
176170

177171
settings.regex.is_match(want)

tests/by-util/test_pgrep.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,13 @@ fn test_invalid_signal() {
369369
.stderr_contains("Unknown signal 'foo'");
370370
}
371371

372+
#[test]
373+
#[cfg(target_os = "linux")]
374+
fn test_does_not_match_pid() {
375+
let our_pid = std::process::id();
376+
new_ucmd!().arg(our_pid.to_string()).fails();
377+
}
378+
372379
#[test]
373380
#[cfg(target_os = "linux")]
374381
fn test_too_long_pattern() {

0 commit comments

Comments
 (0)