Skip to content

Commit

Permalink
reader_linux: only access latestBuf when set
Browse files Browse the repository at this point in the history
.. and avoid nil pointer dereference. It can happen that
`getWalkedProcPid` is called before the first `performWalk` finished.
  • Loading branch information
schu committed Mar 15, 2017
1 parent e55ee29 commit 44c7c11
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions probe/endpoint/procspy/reader_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down

0 comments on commit 44c7c11

Please sign in to comment.