Skip to content

Commit 22bb007

Browse files
committed
fix: improve error handling and input validation
Signed-off-by: Harper, Jason M <[email protected]>
1 parent 5274276 commit 22bb007

File tree

1 file changed

+45
-29
lines changed

1 file changed

+45
-29
lines changed

internal/report/table_helpers.go

Lines changed: 45 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -303,18 +303,10 @@ func getSpecFrequencyBuckets(outputs map[string]script.ScriptOutput) ([][]string
303303
for _, isaHex := range values[1:] {
304304
var isaFreqs []string
305305
var freqs []int
306-
if isaHex != "0" {
307-
var err error
308-
freqs, err = getFrequenciesFromHex(isaHex)
309-
if err != nil {
310-
return nil, fmt.Errorf("failed to get frequencies from Hex string: %w", err)
311-
}
312-
} else {
313-
// if the ISA is not supported, set the frequency to zero for all buckets
314-
freqs = make([]int, len(bucketCoreCounts))
315-
for i := range freqs {
316-
freqs[i] = 0
317-
}
306+
var err error
307+
freqs, err = getFrequenciesFromHex(isaHex)
308+
if err != nil {
309+
return nil, fmt.Errorf("failed to get frequencies from Hex string: %w", err)
318310
}
319311
if len(freqs) != len(bucketCoreCounts) {
320312
freqs, err = padFrequencies(freqs, len(bucketCoreCounts))
@@ -339,6 +331,9 @@ func getSpecFrequencyBuckets(outputs map[string]script.ScriptOutput) ([][]string
339331
}
340332
// add fieldNames for ISAs that have frequencies
341333
for i := range allIsaFreqs {
334+
if len(allIsaFreqs[i]) < 1 {
335+
return nil, fmt.Errorf("no frequencies found for isa")
336+
}
342337
if allIsaFreqs[i][0] == "0.0" {
343338
continue
344339
}
@@ -348,20 +343,25 @@ func getSpecFrequencyBuckets(outputs map[string]script.ScriptOutput) ([][]string
348343
row := make([]string, 0, len(allIsaFreqs)+2)
349344
// add the total core buckets for multi-die architectures
350345
if archMultiplier > 1 {
346+
if i >= len(totalCoreBuckets) {
347+
return nil, fmt.Errorf("index out of range for total core buckets")
348+
}
351349
row = append(row, totalCoreBuckets[i])
352350
}
353351
// add the die core buckets
354352
row = append(row, bucket)
355353
// add the frequencies for each ISA
356354
for _, isaFreqs := range allIsaFreqs {
355+
if len(isaFreqs) < 1 {
356+
return nil, fmt.Errorf("no frequencies found for isa")
357+
}
357358
if isaFreqs[0] == "0.0" {
358359
continue
359-
} else {
360-
if i >= len(isaFreqs) {
361-
return nil, fmt.Errorf("index out of range for isa frequencies")
362-
}
363-
row = append(row, isaFreqs[i])
364360
}
361+
if i >= len(isaFreqs) {
362+
return nil, fmt.Errorf("index out of range for isa frequencies")
363+
}
364+
row = append(row, isaFreqs[i])
365365
}
366366
specCoreFreqs = append(specCoreFreqs, row)
367367
}
@@ -895,7 +895,11 @@ func elcFieldValuesFromOutput(outputs map[string]script.ScriptOutput) (fieldValu
895895
values := []string{}
896896
// value rows
897897
for _, row := range rows[1:] {
898-
values = append(values, row[fieldNamesIndex])
898+
if fieldNamesIndex < len(row) {
899+
values = append(values, row[fieldNamesIndex])
900+
} else {
901+
values = append(values, "")
902+
}
899903
}
900904
fieldValues = append(fieldValues, Field{Name: fieldName, Values: values})
901905
}
@@ -1010,12 +1014,16 @@ func eppFromOutput(outputs map[string]script.ScriptOutput) string {
10101014
}
10111015
// check if the epp valid bit is set and consistent across all cores
10121016
var eppValid string
1013-
for i, line := range strings.Split(outputs[script.EppValidScriptName].Stdout, "\n") { // MSR 0x774, bit 60
1017+
for line := range strings.SplitSeq(outputs[script.EppValidScriptName].Stdout, "\n") { // MSR 0x774, bit 60
10141018
if line == "" {
10151019
continue
10161020
}
1017-
currentEpbValid := strings.TrimSpace(strings.Split(line, ":")[1])
1018-
if i == 0 {
1021+
parts := strings.Split(line, ":")
1022+
if len(parts) < 2 {
1023+
continue
1024+
}
1025+
currentEpbValid := strings.TrimSpace(parts[1])
1026+
if eppValid == "" {
10191027
eppValid = currentEpbValid
10201028
continue
10211029
}
@@ -1026,12 +1034,16 @@ func eppFromOutput(outputs map[string]script.ScriptOutput) string {
10261034
}
10271035
// check if epp package control bit is set and consistent across all cores
10281036
var eppPkgCtrl string
1029-
for i, line := range strings.Split(outputs[script.EppPackageControlScriptName].Stdout, "\n") { // MSR 0x774, bit 42
1037+
for line := range strings.SplitSeq(outputs[script.EppPackageControlScriptName].Stdout, "\n") { // MSR 0x774, bit 42
10301038
if line == "" {
10311039
continue
10321040
}
1033-
currentEppPkgCtrl := strings.TrimSpace(strings.Split(line, ":")[1])
1034-
if i == 0 {
1041+
parts := strings.Split(line, ":")
1042+
if len(parts) < 2 {
1043+
continue
1044+
}
1045+
currentEppPkgCtrl := strings.TrimSpace(parts[1])
1046+
if eppPkgCtrl == "" {
10351047
eppPkgCtrl = currentEppPkgCtrl
10361048
continue
10371049
}
@@ -1050,12 +1062,16 @@ func eppFromOutput(outputs map[string]script.ScriptOutput) string {
10501062
return eppValToLabel(int(msr))
10511063
} else {
10521064
var epp string
1053-
for i, line := range strings.Split(outputs[script.EppScriptName].Stdout, "\n") { // MSR 0x774, bits 24-31 (per-core)
1065+
for line := range strings.SplitSeq(outputs[script.EppScriptName].Stdout, "\n") { // MSR 0x774, bits 24-31 (per-core)
10541066
if line == "" {
10551067
continue
10561068
}
1057-
currentEpp := strings.TrimSpace(strings.Split(line, ":")[1])
1058-
if i == 0 {
1069+
parts := strings.Split(line, ":")
1070+
if len(parts) < 2 {
1071+
continue
1072+
}
1073+
currentEpp := strings.TrimSpace(parts[1])
1074+
if epp == "" {
10591075
epp = currentEpp
10601076
continue
10611077
}
@@ -1461,7 +1477,7 @@ func filesystemFieldValuesFromOutput(outputs map[string]script.ScriptOutput) []F
14611477
}
14621478
fields := strings.Fields(line)
14631479
// "Mounted On" gets split into two fields, rejoin
1464-
if i == 0 && fields[len(fields)-2] == "Mounted" && fields[len(fields)-1] == "on" {
1480+
if i == 0 && len(fields) >= 2 && fields[len(fields)-2] == "Mounted" && fields[len(fields)-1] == "on" {
14651481
fields[len(fields)-2] = "Mounted on"
14661482
fields = fields[:len(fields)-1]
14671483
for _, field := range fields {
@@ -1720,7 +1736,7 @@ func getPCIDevices(class string, outputs map[string]script.ScriptOutput) (device
17201736
continue
17211737
}
17221738
match := re.FindStringSubmatch(line)
1723-
if len(match) > 0 {
1739+
if len(match) >= 3 {
17241740
key := match[1]
17251741
value := match[2]
17261742
device[key] = value

0 commit comments

Comments
 (0)