From abbfda0b3c04c4536faa158b44a4047e357ede23 Mon Sep 17 00:00:00 2001 From: Taher Kathanawala <28436188+taherkk@users.noreply.github.com> Date: Fri, 9 Jun 2023 17:53:03 +0530 Subject: [PATCH] Bug Fix: Slice out of bound in filterOfflineCpus (#534) Signed-off-by: taherk --- sysfs/system_cpu.go | 15 ++++++++------- sysfs/system_cpu_test.go | 4 ++-- testdata/fixtures.ttar | 5 ++++- 3 files changed, 14 insertions(+), 10 deletions(-) diff --git a/sysfs/system_cpu.go b/sysfs/system_cpu.go index 9e158581..fbecc190 100644 --- a/sysfs/system_cpu.go +++ b/sysfs/system_cpu.go @@ -170,19 +170,20 @@ func binSearch(elem uint16, elemSlice *[]uint16) bool { return false } -func filterOfflineCPUs(offlineCpus *[]uint16, cpus *[]string) error { - for i, cpu := range *cpus { +func filterOfflineCPUs(offlineCpus *[]uint16, cpus *[]string) ([]string, error) { + var filteredCPUs []string + for _, cpu := range *cpus { cpuName := strings.TrimPrefix(filepath.Base(cpu), "cpu") cpuNameUint16, err := strconv.Atoi(cpuName) if err != nil { - return err + return nil, err } - if binSearch(uint16(cpuNameUint16), offlineCpus) { - *cpus = append((*cpus)[:i], (*cpus)[i+1:]...) + if !binSearch(uint16(cpuNameUint16), offlineCpus) { + filteredCPUs = append(filteredCPUs, cpu) } } - return nil + return filteredCPUs, nil } // SystemCpufreq returns CPU frequency metrics for all CPUs. @@ -206,7 +207,7 @@ func (fs FS) SystemCpufreq() ([]SystemCPUCpufreqStats, error) { } if len(offlineCPUs) > 0 { - err = filterOfflineCPUs(&offlineCPUs, &cpus) + cpus, err = filterOfflineCPUs(&offlineCPUs, &cpus) if err != nil { return nil, err } diff --git a/sysfs/system_cpu_test.go b/sysfs/system_cpu_test.go index 864e2eac..9fee2a1a 100644 --- a/sysfs/system_cpu_test.go +++ b/sysfs/system_cpu_test.go @@ -35,7 +35,7 @@ func TestCPUTopology(t *testing.T) { if err != nil { t.Fatal(err) } - if want, have := 2, len(cpus); want != have { + if want, have := 3, len(cpus); want != have { t.Errorf("incorrect number of CPUs, have %v, want %v", want, have) } if want, have := "0", cpus[0].Number(); want != have { @@ -72,7 +72,7 @@ func TestCPUThermalThrottle(t *testing.T) { if err != nil { t.Fatal(err) } - if want, have := 2, len(cpus); want != have { + if want, have := 3, len(cpus); want != have { t.Errorf("incorrect number of CPUs, have %v, want %v", want, have) } cpu0Throttle, err := cpus[0].ThermalThrottle() diff --git a/testdata/fixtures.ttar b/testdata/fixtures.ttar index fd2b9e54..cc2ff45d 100644 --- a/testdata/fixtures.ttar +++ b/testdata/fixtures.ttar @@ -13152,6 +13152,9 @@ Lines: 1 1,5 Mode: 444 # ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +Directory: fixtures/sys/devices/system/cpu/cpu2 +Mode: 775 +# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Directory: fixtures/sys/devices/system/cpu/cpufreq Mode: 775 # ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -13228,7 +13231,7 @@ Mode: 664 # ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Path: fixtures/sys/devices/system/cpu/offline Lines: 1 -2,12-15 +2 Mode: 664 # ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Directory: fixtures/sys/devices/system/node