From 9dcddfedc7d733a4bc9f1d6e1f0b1eaf95a43447 Mon Sep 17 00:00:00 2001 From: NitroCao Date: Sat, 7 Jun 2025 21:16:53 +0800 Subject: [PATCH] fix: don't copy elements of OpenFilesStat slice again --- process/process_linux.go | 16 ++++------------ process/process_linux_test.go | 4 ++-- 2 files changed, 6 insertions(+), 14 deletions(-) diff --git a/process/process_linux.go b/process/process_linux.go index 7b10e34..f44f6bc 100644 --- a/process/process_linux.go +++ b/process/process_linux.go @@ -372,15 +372,7 @@ func (p *Process) ChildrenWithContext(ctx context.Context) ([]*Process, error) { func (p *Process) OpenFilesWithContext(ctx context.Context) ([]OpenFilesStat, error) { _, ofs, err := p.fillFromfdWithContext(ctx) - if err != nil { - return nil, err - } - ret := make([]OpenFilesStat, len(ofs)) - for i, o := range ofs { - ret[i] = *o - } - - return ret, nil + return ofs, err } func (p *Process) ConnectionsWithContext(ctx context.Context) ([]net.ConnectionStat, error) { @@ -629,14 +621,14 @@ func (p *Process) fillFromfdListWithContext(ctx context.Context) (string, []stri } // Get num_fds from /proc/(pid)/fd -func (p *Process) fillFromfdWithContext(ctx context.Context) (int32, []*OpenFilesStat, error) { +func (p *Process) fillFromfdWithContext(ctx context.Context) (int32, []OpenFilesStat, error) { statPath, fnames, err := p.fillFromfdListWithContext(ctx) if err != nil { return 0, nil, err } numFDs := int32(len(fnames)) - openfiles := make([]*OpenFilesStat, 0, numFDs) + openfiles := make([]OpenFilesStat, 0, numFDs) for _, fd := range fnames { fpath := filepath.Join(statPath, fd) path, err := common.Readlink(fpath) @@ -647,7 +639,7 @@ func (p *Process) fillFromfdWithContext(ctx context.Context) (int32, []*OpenFile if err != nil { return numFDs, openfiles, err } - o := &OpenFilesStat{ + o := OpenFilesStat{ Path: path, Fd: t, } diff --git a/process/process_linux_test.go b/process/process_linux_test.go index 4a0812d..c915d85 100644 --- a/process/process_linux_test.go +++ b/process/process_linux_test.go @@ -18,7 +18,7 @@ import ( func TestFillFromfdWithContext(t *testing.T) { type expect struct { numFDs int32 - openFiles []*OpenFilesStat + openFiles []OpenFilesStat err error } type testCase struct { @@ -33,7 +33,7 @@ func TestFillFromfdWithContext(t *testing.T) { pid: 1, expected: &expect{ numFDs: 3, - openFiles: []*OpenFilesStat{ + openFiles: []OpenFilesStat{ { Path: "/foo", Fd: 0,