From 94fb1e44a7c2a5c98067761dc6f42057c3c76def Mon Sep 17 00:00:00 2001 From: Ben Kochie Date: Thu, 26 Sep 2024 00:00:04 +0200 Subject: [PATCH] Fix: Add safety check to hwmon read Avoid panic for accessing slice out of range in hwmon. Fixes: https://github.com/prometheus/node_exporter/issues/3108 Signed-off-by: Ben Kochie --- collector/hwmon_linux.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/collector/hwmon_linux.go b/collector/hwmon_linux.go index 3d53995c8d..9c0e065c3f 100644 --- a/collector/hwmon_linux.go +++ b/collector/hwmon_linux.go @@ -18,6 +18,7 @@ package collector import ( "errors" + "fmt" "log/slog" "os" "path/filepath" @@ -107,6 +108,9 @@ func sysReadFile(file string) ([]byte, error) { if err != nil { return nil, err } + if n < 0 { + return nil, fmt.Errorf("failed to read file: %q, read returned negative bytes value: %d", file, n) + } return b[:n], nil }