From be2dab5a40672681752c58500958886133a1db91 Mon Sep 17 00:00:00 2001 From: raviparimi Date: Thu, 3 Mar 2016 15:00:23 -0800 Subject: [PATCH] Provide correct length for slice creation. - Creating a slice of length numFDs and later appending elements to it results in a slice whose first numFDs elements are all nil. It is sufficient to create a slice of zero length since we are appending elements to it. - The current allocation will make ret a slice of length 0. What's needed is a slice of length len(ofs). --- process/process_linux.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/process/process_linux.go b/process/process_linux.go index e9f3fca..e32cc9b 100644 --- a/process/process_linux.go +++ b/process/process_linux.go @@ -246,7 +246,7 @@ func (p *Process) OpenFiles() ([]OpenFilesStat, error) { if err != nil { return nil, err } - ret := make([]OpenFilesStat, 0, len(ofs)) + ret := make([]OpenFilesStat, len(ofs)) for i, o := range ofs { ret[i] = *o } @@ -361,7 +361,7 @@ func (p *Process) fillFromfd() (int32, []*OpenFilesStat, error) { fnames, err := d.Readdirnames(-1) numFDs := int32(len(fnames)) - openfiles := make([]*OpenFilesStat, numFDs) + openfiles := make([]*OpenFilesStat, 0) for _, fd := range fnames { fpath := filepath.Join(statPath, fd) filepath, err := os.Readlink(fpath)