Skip to content

Commit

Permalink
MINOR: kern_procargs more robust on darwin
Browse files Browse the repository at this point in the history
Under some circonstance the code can fail, the root cause is not clear
at the moment, but we should still make the code more robust when
retrieving information for a specific process.

The problem is the `bytes.SplitN` can return an array of one element
making the process panic when we access the other element.

This commit make the code a bit more robust and check to make sure we
successfully retrieved 2 elements instead of one and return an error
if it failed.

Reference: elastic/beats#5337
  • Loading branch information
ph committed Oct 11, 2017
1 parent 5a9ad8e commit 876f407
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ This project adheres to [Semantic Versioning](http://semver.org/).
### Added

### Changed
- Make kern_procargs more robust under darwin when we cannot retrieve
all the information about a process. #78

### Deprecated

Expand Down
7 changes: 6 additions & 1 deletion sigar_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,12 @@ func kern_procargs(pid int,
return fmt.Errorf("Error reading args: %v", err)
}
pair := bytes.SplitN(chop(line), delim, 2)
env(string(pair[0]), string(pair[1]))

if len(pair) == 2 {
env(string(pair[0]), string(pair[1]))
} else {
return fmt.Errorf("Error reading process information for PID: %d", pid)
}
}

return nil
Expand Down

0 comments on commit 876f407

Please sign in to comment.