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 <superq@gmail.com>
  • Loading branch information
SuperQ committed Sep 27, 2024
1 parent 71d9b6c commit c9ad416
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", file)
}

return b[:n], nil
}
Expand Down

0 comments on commit c9ad416

Please sign in to comment.