From 7c35abe30e375024b53bbb4b72f99e7c0dda1b3f Mon Sep 17 00:00:00 2001 From: GT610 <79314033+GT-610@users.noreply.github.com> Date: Tue, 6 Jan 2026 12:48:13 +0800 Subject: [PATCH] fix(cpu): Resolved boundary condition issues when calculating CPU utilization (#988) Added checks for coreIdx out-of-bounds and totalDelta being zero to prevent array out-of-bounds and division-by-zero errors --- lib/data/model/server/cpu.dart | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/data/model/server/cpu.dart b/lib/data/model/server/cpu.dart index 1e8c8e12..78f22b21 100644 --- a/lib/data/model/server/cpu.dart +++ b/lib/data/model/server/cpu.dart @@ -33,9 +33,11 @@ class Cpus extends TimeSeq { double usedPercent({int coreIdx = 0}) { if (now.length != pre.length) return 0; if (now.isEmpty) return 0; + if (coreIdx >= now.length) return 0; try { final idleDelta = now[coreIdx].idle - pre[coreIdx].idle; final totalDelta = now[coreIdx].total - pre[coreIdx].total; + if (totalDelta == 0) return 0; final used = idleDelta / totalDelta; return used.isNaN ? 0 : 100 - used * 100; } catch (e, s) {