Skip to content

Commit

Permalink
Bug Fix: Slice out of bound in filterOfflineCpus (#534)
Browse files Browse the repository at this point in the history
Signed-off-by: taherk <tkkathana@gmail.com>
  • Loading branch information
taherkk authored Jun 9, 2023
1 parent 5d2473f commit abbfda0
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 10 deletions.
15 changes: 8 additions & 7 deletions sysfs/system_cpu.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions sysfs/system_cpu_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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()
Expand Down
5 changes: 4 additions & 1 deletion testdata/fixtures.ttar
Original file line number Diff line number Diff line change
Expand Up @@ -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 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit abbfda0

Please sign in to comment.