Skip to content

Commit f613787

Browse files
authored
fix: resolve index out of bounds in diskio stat (#260)
## What does this PR do? it's possible on linux to get an empty slice if the file is empty protect against that by checking the slice len. It's impossible on darwin but let's not rely on implementation details and also check there. ## Why is it important? prevent index out of bounds error ## Checklist <!-- Mandatory Add a checklist of things that are required to be reviewed in order to have the PR approved List here all the items you have verified BEFORE sending this PR. Please DO NOT remove any item, striking through those that do not apply. (Just in case, strikethrough uses two tildes. ~~Scratch this.~~) --> - [ ] My code follows the style guidelines of this project - [ ] I have commented my code, particularly in hard-to-understand areas - [ ] I have added tests that prove my fix is effective or that my feature works - [ ] I have added an entry in `CHANGELOG.md` ## Author's Checklist <!-- Recommended Add a checklist of things that are required to be reviewed in order to have the PR approved --> - [ ] ## Related issues <!-- Recommended Link related issues below. Insert the issue link or reference after the word "Closes" if merging this should automatically close it. - Closes #123 - Relates #123 - Requires #123 - Superseds #123 --> -
1 parent 4099ce9 commit f613787

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

metric/cpu/metrics_darwin.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,11 @@ func Get(m *Monitor) (CPUMetrics, error) {
4444
for _, cpu := range perCPU {
4545
cpulist = append(cpulist, fillCPU(cpu))
4646
}
47-
return CPUMetrics{totals: fillCPU(sum[0]), list: cpulist}, nil
47+
cpuTotal := CPU{}
48+
if len(sum) > 0 {
49+
cpuTotal = fillCPU(sum[0])
50+
}
51+
return CPUMetrics{totals: cpuTotal, list: cpulist}, nil
4852
}
4953

5054
func fillCPU(raw cpu.TimesStat) CPU {

metric/system/diskio/diskstat_linux.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,9 @@ func (stat *IOStat) OpenSampling() error {
5858
if err != nil {
5959
return err
6060
}
61-
stat.curCPU = times[0]
61+
if len(times) > 0 {
62+
stat.curCPU = times[0]
63+
}
6264
return nil
6365
}
6466

0 commit comments

Comments
 (0)