mirror of
https://github.com/tw93/Mole.git
synced 2026-02-04 15:04:42 +00:00
fix: handle uint64 overflow in battery power metrics
- Fix display of invalid large values like 18446744073709544W - macOS ioreg returns negative values as uint64 (two's complement) - Detect overflow pattern (value > 2^63) and convert to signed - Validate reasonable power range (-200W to 200W) for battery - Add range validation for SystemPower (0 to 1000W) Root cause: During charging, battery power is negative but ioreg formats it as unsigned uint64, causing overflow. This fix properly converts these values back to signed integers.
This commit is contained in:
@@ -227,20 +227,30 @@ func collectThermal() ThermalStatus {
|
|||||||
valStr, _, _ = strings.Cut(valStr, ",")
|
valStr, _, _ = strings.Cut(valStr, ",")
|
||||||
valStr, _, _ = strings.Cut(valStr, "}")
|
valStr, _, _ = strings.Cut(valStr, "}")
|
||||||
valStr = strings.TrimSpace(valStr)
|
valStr = strings.TrimSpace(valStr)
|
||||||
if powerMW, err := strconv.ParseFloat(valStr, 64); err == nil && powerMW > 0 {
|
if powerMW, err := strconv.ParseFloat(valStr, 64); err == nil {
|
||||||
thermal.SystemPower = powerMW / 1000.0
|
// SystemPower should always be positive, reject invalid values
|
||||||
|
if powerMW >= 0 && powerMW < 1000000 { // 0 to 1000W
|
||||||
|
thermal.SystemPower = powerMW / 1000.0
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Battery power (mW -> W, positive = discharging).
|
// Battery power (mW -> W, positive = discharging, negative = charging).
|
||||||
if _, after, found := strings.Cut(line, "\"BatteryPower\"="); found {
|
if _, after, found := strings.Cut(line, "\"BatteryPower\"="); found {
|
||||||
valStr := strings.TrimSpace(after)
|
valStr := strings.TrimSpace(after)
|
||||||
valStr, _, _ = strings.Cut(valStr, ",")
|
valStr, _, _ = strings.Cut(valStr, ",")
|
||||||
valStr, _, _ = strings.Cut(valStr, "}")
|
valStr, _, _ = strings.Cut(valStr, "}")
|
||||||
valStr = strings.TrimSpace(valStr)
|
valStr = strings.TrimSpace(valStr)
|
||||||
// Parse as int64 first to handle negative values (charging)
|
if powerMW, err := strconv.ParseFloat(valStr, 64); err == nil {
|
||||||
if powerMW, err := strconv.ParseInt(valStr, 10, 64); err == nil {
|
// macOS ioreg may return negative values as uint64 (two's complement overflow)
|
||||||
thermal.BatteryPower = float64(powerMW) / 1000.0
|
// If value > 2^63, convert from uint64 representation to proper signed value
|
||||||
|
if powerMW > float64(1<<63) {
|
||||||
|
powerMW = powerMW - 18446744073709551616.0 // 2^64
|
||||||
|
}
|
||||||
|
// Validate reasonable battery power range: -200W to 200W
|
||||||
|
if powerMW > -200000 && powerMW < 200000 {
|
||||||
|
thermal.BatteryPower = powerMW / 1000.0
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user