From a0bc682b9b2ab4eee1687977adda8f751c98c17b Mon Sep 17 00:00:00 2001 From: Tuomas Katila Date: Tue, 5 Dec 2023 15:21:45 +0200 Subject: [PATCH 1/2] labeler: fix codeql issues Signed-off-by: Tuomas Katila --- cmd/internal/labeler/labeler.go | 13 +++++++-- cmd/internal/labeler/labeler_test.go | 42 ++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 2 deletions(-) diff --git a/cmd/internal/labeler/labeler.go b/cmd/internal/labeler/labeler.go index 08c407cd9..5a827cc17 100644 --- a/cmd/internal/labeler/labeler.go +++ b/cmd/internal/labeler/labeler.go @@ -16,6 +16,7 @@ package labeler import ( "fmt" + "math" "os" "os/signal" "path" @@ -205,12 +206,18 @@ func (l *labeler) getNumaNode(gpuName string) int { return -1 } - numa, err := strconv.ParseInt(strings.TrimSpace(string(data)), 10, 64) + numa, err := strconv.ParseInt(strings.TrimSpace(string(data)), 10, 32) if err != nil { klog.Warning("Can't convert numa_node: ", err) return -1 } + if numa > math.MaxInt16 { + klog.Warning("Too large numa: ", numa) + + return -1 + } + return int(numa) } @@ -305,7 +312,9 @@ func (l *labeler) createLabels() error { numaMapping[numaNode] = numaList } - l.labels.addNumericLabel(labelNamespace+"memory.max", int64(memoryAmount)) + if memoryAmount < math.MaxInt64 { + l.labels.addNumericLabel(labelNamespace+"memory.max", int64(memoryAmount)) + } } gpuCount := len(gpuNumList) diff --git a/cmd/internal/labeler/labeler_test.go b/cmd/internal/labeler/labeler_test.go index 9833eabe7..31186e224 100644 --- a/cmd/internal/labeler/labeler_test.go +++ b/cmd/internal/labeler/labeler_test.go @@ -520,6 +520,48 @@ func getTestCases() []testcase { "gpu.intel.com/tiles": "27", }, }, + { + sysfsdirs: []string{ + "card1/device/drm/card1", + "card1/gt/gt0", + }, + sysfsfiles: map[string][]byte{ + "card1/device/vendor": []byte("0x8086"), + "card1/lmem_total_bytes": []byte("8000"), + "card1/device/numa_node": []byte("2147483648"), // max int32 + 1 + }, + name: "too large numa node", + memoryOverride: 16000000000, + expectedRetval: nil, + expectedLabels: labelMap{ + "gpu.intel.com/millicores": "1000", + "gpu.intel.com/memory.max": "8000", + "gpu.intel.com/gpu-numbers": "1", + "gpu.intel.com/cards": "card1", + "gpu.intel.com/tiles": "1", + }, + }, + { + sysfsdirs: []string{ + "card1/device/drm/card1", + "card1/gt/gt0", + }, + sysfsfiles: map[string][]byte{ + "card1/device/vendor": []byte("0x8086"), + "card1/lmem_total_bytes": []byte("8000"), + "card1/device/numa_node": []byte("32768"), // max int16 + 1 + }, + name: "too large numa node", + memoryOverride: 16000000000, + expectedRetval: nil, + expectedLabels: labelMap{ + "gpu.intel.com/millicores": "1000", + "gpu.intel.com/memory.max": "8000", + "gpu.intel.com/gpu-numbers": "1", + "gpu.intel.com/cards": "card1", + "gpu.intel.com/tiles": "1", + }, + }, } } From df83e1bb7c21c2ae24e1cb5f21b824b47ccf3c0b Mon Sep 17 00:00:00 2001 From: Tuomas Katila Date: Tue, 5 Dec 2023 15:50:04 +0200 Subject: [PATCH 2/2] fpga: fix codeql issues Signed-off-by: Tuomas Katila --- pkg/fpga/dfl_linux.go | 2 +- pkg/fpga/intel_fpga_linux.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/fpga/dfl_linux.go b/pkg/fpga/dfl_linux.go index 4ca793d65..9a68f36d2 100644 --- a/pkg/fpga/dfl_linux.go +++ b/pkg/fpga/dfl_linux.go @@ -236,7 +236,7 @@ func (f *DflFME) GetPortsNum() int { } n, err := strconv.ParseUint(f.PortsNum, 10, 32) - if err != nil || n >= math.MaxInt { + if err != nil || n > math.MaxInt32 { return -1 } diff --git a/pkg/fpga/intel_fpga_linux.go b/pkg/fpga/intel_fpga_linux.go index fa45d1a3b..234a097d0 100644 --- a/pkg/fpga/intel_fpga_linux.go +++ b/pkg/fpga/intel_fpga_linux.go @@ -241,7 +241,7 @@ func (f *IntelFpgaFME) GetPortsNum() int { } n, err := strconv.ParseUint(f.PortsNum, 10, 32) - if err != nil || n >= math.MaxInt { + if err != nil || n > math.MaxInt32 { return -1 }