From c5e42b972f0d49a8e50c937304f2aac23dbd1252 Mon Sep 17 00:00:00 2001 From: Brian Ryner Date: Wed, 7 Dec 2022 14:27:20 +1100 Subject: [PATCH 1/4] Truncate the result of Getfsstat to the item count that is returned. This count may be less than what was returned by the first call to Getfsstat. --- disk/disk_darwin.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/disk/disk_darwin.go b/disk/disk_darwin.go index 0877b76..15c0934 100644 --- a/disk/disk_darwin.go +++ b/disk/disk_darwin.go @@ -20,9 +20,11 @@ func PartitionsWithContext(ctx context.Context, all bool) ([]PartitionStat, erro return ret, err } fs := make([]unix.Statfs_t, count) - if _, err = unix.Getfsstat(fs, unix.MNT_WAIT); err != nil { + count, err := unix.Getfsstat(fs, unix.MNT_WAIT) + if err != nil { return ret, err } + fs = fs[:count] // Actual count may be less than from the first call. for _, stat := range fs { opts := []string{"rw"} if stat.Flags&unix.MNT_RDONLY != 0 { From 34da06e9b77b862a095fc30bdb1098c587f29676 Mon Sep 17 00:00:00 2001 From: Brian Ryner Date: Wed, 7 Dec 2022 14:31:09 +1100 Subject: [PATCH 2/4] fix --- disk/disk_darwin.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/disk/disk_darwin.go b/disk/disk_darwin.go index 15c0934..fea6927 100644 --- a/disk/disk_darwin.go +++ b/disk/disk_darwin.go @@ -20,7 +20,7 @@ func PartitionsWithContext(ctx context.Context, all bool) ([]PartitionStat, erro return ret, err } fs := make([]unix.Statfs_t, count) - count, err := unix.Getfsstat(fs, unix.MNT_WAIT) + count, err = unix.Getfsstat(fs, unix.MNT_WAIT) if err != nil { return ret, err } From c2fa2cbf8ef783863fe2d1f0e7c23580367035c4 Mon Sep 17 00:00:00 2001 From: Brian Ryner Date: Sun, 18 Dec 2022 14:23:01 +1100 Subject: [PATCH 3/4] Update comment --- disk/disk_darwin.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/disk/disk_darwin.go b/disk/disk_darwin.go index fea6927..555ac36 100644 --- a/disk/disk_darwin.go +++ b/disk/disk_darwin.go @@ -24,7 +24,10 @@ func PartitionsWithContext(ctx context.Context, all bool) ([]PartitionStat, erro if err != nil { return ret, err } - fs = fs[:count] // Actual count may be less than from the first call. + // On 10.14, and possibly other OS versions, the actual count may + // be less than from the first call. Truncate to the returned count + // to prevent accessing uninitialized entries. + fs = fs[:count] for _, stat := range fs { opts := []string{"rw"} if stat.Flags&unix.MNT_RDONLY != 0 { From 8d22915181074ba3cc954dae752a64385b57070b Mon Sep 17 00:00:00 2001 From: shirou Date: Sun, 18 Dec 2022 13:11:56 +0900 Subject: [PATCH 4/4] [disk][darwin]: add issue URL --- disk/disk_darwin.go | 1 + 1 file changed, 1 insertion(+) diff --git a/disk/disk_darwin.go b/disk/disk_darwin.go index 555ac36..933cb04 100644 --- a/disk/disk_darwin.go +++ b/disk/disk_darwin.go @@ -27,6 +27,7 @@ func PartitionsWithContext(ctx context.Context, all bool) ([]PartitionStat, erro // On 10.14, and possibly other OS versions, the actual count may // be less than from the first call. Truncate to the returned count // to prevent accessing uninitialized entries. + // https://github.com/shirou/gopsutil/issues/1390 fs = fs[:count] for _, stat := range fs { opts := []string{"rw"}