From b9dbf3b0505e07e578dc4671ba89061bb9ea55f3 Mon Sep 17 00:00:00 2001 From: Pier-Hugues Pellerin Date: Tue, 10 Oct 2017 14:36:27 -0400 Subject: [PATCH] MINOR: kern_procargs more robust on darwin 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: https://github.com/elastic/beats/issues/5337 --- CHANGELOG.md | 2 ++ sigar_darwin.go | 5 +++++ 2 files changed, 7 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9ab2941e3..1679a0617 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/sigar_darwin.go b/sigar_darwin.go index d90c15eeb..f989f5160 100644 --- a/sigar_darwin.go +++ b/sigar_darwin.go @@ -420,6 +420,11 @@ func kern_procargs(pid int, return fmt.Errorf("Error reading args: %v", err) } pair := bytes.SplitN(chop(line), delim, 2) + + if len(pair) != 2 { + return fmt.Errorf("Error reading process information for PID: %d", pid) + } + env(string(pair[0]), string(pair[1])) }