Skip to content

Commit

Permalink
Fix: Add safety check to hwmon read
Browse files Browse the repository at this point in the history
Avoid panic for accessing slice out of range in hwmon.

Fixes: #3108

Signed-off-by: Ben Kochie <[email protected]>
  • Loading branch information
SuperQ authored and discordianfish committed Sep 30, 2024
1 parent ebf3cbf commit 1b332ed
Showing 1 changed file with 4 additions and 0 deletions.
4 changes: 4 additions & 0 deletions collector/hwmon_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package collector

import (
"errors"
"fmt"
"log/slog"
"os"
"path/filepath"
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit 1b332ed

Please sign in to comment.