瀏覽代碼

HPCC-21485 Fix potential divide by zero in new CpuInfo::getPercentCpu()

Signed-off-by: Gavin Halliday <gavin.halliday@lexisnexis.com>
Gavin Halliday 6 年之前
父節點
當前提交
6acff288af
共有 1 個文件被更改,包括 17 次插入5 次删除
  1. 17 5
      system/jlib/jdebug.cpp

+ 17 - 5
system/jlib/jdebug.cpp

@@ -895,7 +895,7 @@ unsigned CpuInfo::getPercentCpu() const
     __uint64 total = getTotal();
     __uint64 total = getTotal();
     if (total == 0)
     if (total == 0)
         return 0;
         return 0;
-    unsigned percent = (unsigned)(((total - idle) * 100) / idle);
+    unsigned percent = (unsigned)(((total - idle) * 100) / total);
     if (percent > 100)
     if (percent > 100)
         percent = 100;
         percent = 100;
     return percent;
     return percent;
@@ -918,22 +918,34 @@ __uint64 CpuInfo::getTotalNs() const
 
 
 unsigned CpuInfo::getIdlePercent() const
 unsigned CpuInfo::getIdlePercent() const
 {
 {
-    return (unsigned)((idle * 100) / getTotal());
+    __uint64 total = getTotal();
+    if (total == 0)
+        return 0;
+    return (unsigned)((idle * 100) / total);
 }
 }
 
 
 unsigned CpuInfo::getIoWaitPercent() const
 unsigned CpuInfo::getIoWaitPercent() const
 {
 {
-    return (unsigned)((iowait * 100) / getTotal());
+    __uint64 total = getTotal();
+    if (total == 0)
+        return 0;
+    return (unsigned)((iowait * 100) / total);
 }
 }
 
 
 unsigned CpuInfo::getSystemPercent() const
 unsigned CpuInfo::getSystemPercent() const
 {
 {
-    return (unsigned)((system * 100) / getTotal());
+    __uint64 total = getTotal();
+    if (total == 0)
+        return 0;
+    return (unsigned)((system * 100) / total);
 }
 }
 
 
 unsigned CpuInfo::getUserPercent() const
 unsigned CpuInfo::getUserPercent() const
 {
 {
-    return (unsigned)((user * 100) / getTotal());
+    __uint64 total = getTotal();
+    if (total == 0)
+        return 0;
+    return (unsigned)((user * 100) / total);
 }
 }
 
 
 //===========================================================================
 //===========================================================================