gpu(amd): add workaround for misreported sysfs filesize (#1799)

This commit is contained in:
henrygd
2026-03-09 13:49:37 -04:00
parent 35d0e792ad
commit 6b1ff264f2
2 changed files with 30 additions and 13 deletions

View File

@@ -1,6 +1,7 @@
package utils
import (
"io"
"math"
"os"
"strconv"
@@ -50,6 +51,23 @@ func ReadStringFileOK(path string) (string, bool) {
return strings.TrimSpace(string(b)), true
}
// ReadStringFileLimited reads a file into a string with a maximum size (in bytes) to avoid
// allocating large buffers and potential panics with pseudo-files when the size is misreported.
func ReadStringFileLimited(path string, maxSize int) (string, error) {
f, err := os.Open(path)
if err != nil {
return "", err
}
defer f.Close()
buf := make([]byte, maxSize)
n, err := f.Read(buf)
if err != nil && err != io.EOF {
return "", err
}
return strings.TrimSpace(string(buf[:n])), nil
}
// FileExists reports whether the given path exists.
func FileExists(path string) bool {
_, err := os.Stat(path)