From 5262e0765de4b117dc717e7011ddc8d7c8601894 Mon Sep 17 00:00:00 2001 From: Michael Schubert Date: Wed, 15 Mar 2017 15:33:49 +0100 Subject: [PATCH] reader_linux: only access latestBuf when set .. and avoid nil pointer dereference. It can happen that `getWalkedProcPid` is called before the first `performWalk` finished. --- probe/endpoint/procspy/reader_linux.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/probe/endpoint/procspy/reader_linux.go b/probe/endpoint/procspy/reader_linux.go index 359ca08b5..92b43ca3c 100644 --- a/probe/endpoint/procspy/reader_linux.go +++ b/probe/endpoint/procspy/reader_linux.go @@ -51,11 +51,13 @@ func (br *backgroundReader) getWalkedProcPid(buf *bytes.Buffer) (map[uint64]*Pro br.mtx.Lock() defer br.mtx.Unlock() + var err error // Don't access latestBuf directly but create a reader. In this way, // the buffer will not be empty in the next call of getWalkedProcPid // and it can be copied again. - _, err := io.Copy(buf, bytes.NewReader(br.latestBuf.Bytes())) - + if br.latestBuf != nil { + _, err = io.Copy(buf, bytes.NewReader(br.latestBuf.Bytes())) + } return br.latestSockets, err }