Skip to content

Commit

Permalink
fix: handle shortName could not be found case
Browse files Browse the repository at this point in the history
I can't imagine how this could happen, but since old code
does this, why not 🤷‍♂️
  • Loading branch information
owl-from-hogvarts committed Sep 22, 2023
1 parent efd267e commit b309f8f
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions src/processTree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,14 +136,19 @@ export function getProcesses(one: (pid: number, ppid: number, command: string, a
const shortName = substr(line, 14, 20).trim()
const fullCommand = substr(line, 35)

// binaries with spaces in path may not work
// possible solution to read directly from /proc
let pos = fullCommand.indexOf(shortName);
const commandEndPositionMaybe = fullCommand.indexOf(" ", pos + shortName.length)
const commandEndPosition = commandEndPositionMaybe < 0 ? fullCommand.length : commandEndPositionMaybe;

const command = fullCommand.substring(0, commandEndPosition)
const args = fullCommand.substring(commandEndPosition).trimStart()
let command = shortName;
let args = fullCommand;

const pos = fullCommand.indexOf(shortName);
if (pos >= 0) {
// binaries with spaces in path may not work
// possible solution to read directly from /proc
const commandEndPositionMaybe = fullCommand.indexOf(" ", pos + shortName.length);
const commandEndPosition = commandEndPositionMaybe < 0 ? fullCommand.length : commandEndPositionMaybe;
command = fullCommand.substring(0, commandEndPosition)
args = fullCommand.substring(commandEndPosition).trimStart()
}


if (!isNaN(pid) && !isNaN(ppid)) {
one(pid, ppid, command, args);
Expand Down

0 comments on commit b309f8f

Please sign in to comment.