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 <superq@gmail.com>
This commit is contained in:
Ben Kochie 2024-09-26 00:00:04 +02:00
parent 71d9b6c061
commit 3ed088616a
No known key found for this signature in database
GPG key ID: C646B23C9E3245F1

View file

@ -107,6 +107,9 @@ func sysReadFile(file string) ([]byte, error) {
if err != nil {
return nil, err
}
if n < 0 {
return nil, fmt.Errorf("failed to read file: %w", file)
}
return b[:n], nil
}