From 567f875f06581923c25b1726dfe3f1ad2d38bbdd Mon Sep 17 00:00:00 2001 From: "[CST] Bas" Date: Sun, 22 Feb 2015 20:18:07 +0800 Subject: [PATCH] src/system/sysload.cpp: fix possible divide by zero bug, stats are not guarantueed to be different for each sample. Origin commit data ------------------ Branch: ni/coolstream Commit: https://github.com/neutrino-images/ni-neutrino/commit/6fdaa869d748c0af845c150592d533a9cbafb86e Author: [CST] Bas Date: 2015-02-22 (Sun, 22 Feb 2015) ------------------ No further description and justification available within origin commit message! ------------------ This commit was generated by Migit --- src/system/sysload.cpp | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/system/sysload.cpp b/src/system/sysload.cpp index 5338adcc9..341a8c189 100644 --- a/src/system/sysload.cpp +++ b/src/system/sysload.cpp @@ -62,8 +62,11 @@ void *cSysLoad::Run(void *arg) while (getline(in, line)) { unsigned long _stat_user, _stat_nice, _stat_system, _stat_idle; if (4 == sscanf(line.c_str(), "cpu %lu %lu %lu %lu", &_stat_user, &_stat_nice, &_stat_system, &_stat_idle)) { + unsigned long _stat_total = _stat_user + _stat_nice + _stat_system + _stat_idle; if (stat_total) { - unsigned long div = _stat_user + _stat_nice + _stat_system + _stat_idle - stat_total; + unsigned long div = _stat_total - stat_total; + if (!div) // prevent division by zero if previous stat_total is equal to new. + break; caller->data_last = (int)(1000 - 1000 * (_stat_idle - stat_idle) / div); if (caller->data_avail < caller->data_size) { caller->data[caller->data_avail++] = caller->data_last; @@ -73,7 +76,7 @@ void *cSysLoad::Run(void *arg) } } stat_idle = _stat_idle; - stat_total = _stat_user + _stat_nice + _stat_system + _stat_idle; + stat_total = _stat_total; break; } }