Skip to content

Commit 1b97f5d

Browse files
authored
Merge pull request #204 from nix-community/fix/parsing-cpu-info
fix: sorting CPU entries by physical id
2 parents ebe1aa3 + 3ceaa27 commit 1b97f5d

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

pkg/facter/hardware.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -243,16 +243,29 @@ func (h *Hardware) add(device hwinfo.HardwareDevice) error {
243243

244244
// We insert by physical id, as we only want one entry per core.
245245
requiredSize := int(cpu.PhysicalID) + 1 //nolint:gosec
246+
246247
if len(h.CPU) < requiredSize {
247248
newItems := make([]*hwinfo.DetailCPU, requiredSize-len(h.CPU))
248249
h.CPU = append(h.CPU, newItems...)
249250
}
250251

252+
// add our new entry
251253
h.CPU[cpu.PhysicalID] = cpu
252254

253-
// Sort in ascending order to ensure a stable output
255+
// Sort in ascending order to ensure a stable output.
256+
// It's possible that the CPU list contains nil pointers as we are not guaranteed to read the CPU's in order of
257+
// physical id.
254258
slices.SortFunc(h.CPU, func(a, b *hwinfo.DetailCPU) int {
255-
return int(a.PhysicalID - b.PhysicalID) //nolint:gosec
259+
switch {
260+
case a == nil && b == nil:
261+
return 0
262+
case a == nil && b != nil:
263+
return -1
264+
case a != nil && b == nil:
265+
return 1
266+
default:
267+
return int(a.PhysicalID - b.PhysicalID) //nolint:gosec
268+
}
256269
})
257270

258271
case hwinfo.HardwareClassDisk:

0 commit comments

Comments
 (0)