Skip to content

Commit abbfda0

Browse files
authored
Bug Fix: Slice out of bound in filterOfflineCpus (prometheus#534)
Signed-off-by: taherk <[email protected]>
1 parent 5d2473f commit abbfda0

File tree

3 files changed

+14
-10
lines changed

3 files changed

+14
-10
lines changed

sysfs/system_cpu.go

+8-7
Original file line numberDiff line numberDiff line change
@@ -170,19 +170,20 @@ func binSearch(elem uint16, elemSlice *[]uint16) bool {
170170
return false
171171
}
172172

173-
func filterOfflineCPUs(offlineCpus *[]uint16, cpus *[]string) error {
174-
for i, cpu := range *cpus {
173+
func filterOfflineCPUs(offlineCpus *[]uint16, cpus *[]string) ([]string, error) {
174+
var filteredCPUs []string
175+
for _, cpu := range *cpus {
175176
cpuName := strings.TrimPrefix(filepath.Base(cpu), "cpu")
176177
cpuNameUint16, err := strconv.Atoi(cpuName)
177178
if err != nil {
178-
return err
179+
return nil, err
179180
}
180-
if binSearch(uint16(cpuNameUint16), offlineCpus) {
181-
*cpus = append((*cpus)[:i], (*cpus)[i+1:]...)
181+
if !binSearch(uint16(cpuNameUint16), offlineCpus) {
182+
filteredCPUs = append(filteredCPUs, cpu)
182183
}
183184
}
184185

185-
return nil
186+
return filteredCPUs, nil
186187
}
187188

188189
// SystemCpufreq returns CPU frequency metrics for all CPUs.
@@ -206,7 +207,7 @@ func (fs FS) SystemCpufreq() ([]SystemCPUCpufreqStats, error) {
206207
}
207208

208209
if len(offlineCPUs) > 0 {
209-
err = filterOfflineCPUs(&offlineCPUs, &cpus)
210+
cpus, err = filterOfflineCPUs(&offlineCPUs, &cpus)
210211
if err != nil {
211212
return nil, err
212213
}

sysfs/system_cpu_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func TestCPUTopology(t *testing.T) {
3535
if err != nil {
3636
t.Fatal(err)
3737
}
38-
if want, have := 2, len(cpus); want != have {
38+
if want, have := 3, len(cpus); want != have {
3939
t.Errorf("incorrect number of CPUs, have %v, want %v", want, have)
4040
}
4141
if want, have := "0", cpus[0].Number(); want != have {
@@ -72,7 +72,7 @@ func TestCPUThermalThrottle(t *testing.T) {
7272
if err != nil {
7373
t.Fatal(err)
7474
}
75-
if want, have := 2, len(cpus); want != have {
75+
if want, have := 3, len(cpus); want != have {
7676
t.Errorf("incorrect number of CPUs, have %v, want %v", want, have)
7777
}
7878
cpu0Throttle, err := cpus[0].ThermalThrottle()

testdata/fixtures.ttar

+4-1
Original file line numberDiff line numberDiff line change
@@ -13152,6 +13152,9 @@ Lines: 1
1315213152
1,5
1315313153
Mode: 444
1315413154
# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
13155+
Directory: fixtures/sys/devices/system/cpu/cpu2
13156+
Mode: 775
13157+
# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1315513158
Directory: fixtures/sys/devices/system/cpu/cpufreq
1315613159
Mode: 775
1315713160
# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
@@ -13228,7 +13231,7 @@ Mode: 664
1322813231
# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1322913232
Path: fixtures/sys/devices/system/cpu/offline
1323013233
Lines: 1
13231-
2,12-15
13234+
2
1323213235
Mode: 664
1323313236
# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1323413237
Directory: fixtures/sys/devices/system/node

0 commit comments

Comments
 (0)