fix(agent): add safety check for read returning negative bytes (#1799)

This commit is contained in:
henrygd
2026-04-07 18:41:22 -04:00
parent c3dffff5e4
commit ea80f3c5a2
2 changed files with 5 additions and 0 deletions

View File

@@ -156,6 +156,7 @@ func (gm *GPUManager) updateAmdGpuData(cardPath string) bool {
func readSysfsFloat(path string) (float64, error) {
val, err := utils.ReadStringFileLimited(path, 64)
if err != nil {
slog.Debug("Failed to read sysfs value", "path", path, "error", err)
return 0, err
}
return strconv.ParseFloat(val, 64)

View File

@@ -1,6 +1,7 @@
package utils
import (
"fmt"
"io"
"math"
"os"
@@ -68,6 +69,9 @@ func ReadStringFileLimited(path string, maxSize int) (string, error) {
if err != nil && err != io.EOF {
return "", err
}
if n < 0 {
return "", fmt.Errorf("%s returned negative bytes: %d", path, n)
}
return strings.TrimSpace(string(buf[:n])), nil
}