yibn2008/find-process

Matching by 'name' returns true if args field is empty

stefanhaller opened this issue · 0 comments

On my machine (darwin), calling find('name', 'whatever') always returns a non-empty result, no matter what I pass as a search pattern.

The reason is that on my machine there's a process running whose args field is empty in the ps output:

$ ps ax -ww -o pid,ppid,uid,gid,args
  ...
  867     1     0     0 /usr/libexec/ioupsd
  871     1    55    55 /System/Library/CoreServices/appleeventsd --server
  894     1   441   441 /usr/libexec/rosetta/oahd
  897     1     0     0  
  901     1     0     0 /Library/PrivilegedHelperTools/com.docker.vmnetd
  904     1     0     0 /usr/libexec/taskgated
  909     1    89    89 /usr/sbin/distnoted agent
  ...

As you can see, the process with pid 897 has an empty args field. (I don't quite understand how it's possible to launch a process like this, but that's not the question here.)

Now, the code that filters the process list will skip the name match (because column[4] is empty) and falls through to the return !!column[0], which returns true.

As far as I can see, the following patch will fix the problem:

diff --git a/lib/find_process.js b/lib/find_process.js
index 34188ee..74c4a4b 100644
--- a/lib/find_process.js
+++ b/lib/find_process.js
@@ -84,7 +84,7 @@ const finders = {
           const columns = utils.extractColumns(data, [0, 1, 2, 3, 4], 5).filter(column => {
             if (column[0] && cond.pid) {
               return column[0] === String(cond.pid)
-            } else if (column[4] && cond.name) {
+            } else if (cond.name) {
               return matchName(column[4], cond.name)
             } else {
               return !!column[0]

I think this is correct because matchName correctly takes care of returning false if the input text is empty.

(The same change should probably be made in the "android" section below, but I have no way to test that.)