Commit f3f509fc authored by Arjen Hiemstra's avatar Arjen Hiemstra Committed by Nate Graham
Browse files

CPU Plugin: Prevent integer overflow of total usage

In some cases, the values we read from the system can result in a
negative difference to the previous values. This was already guarded
partially, but only for system/user/wait usage. Total usage would still
be able to go negative, resulting in overflow of the total usage.

Instead, ensure that all the differences are always at least 0, so if we
add things together we aren't adding negatives.

BUG: 448626
FIXED-IN: 5.24.1
parent 40be1b73
Pipeline #133103 passed with stage
in 44 seconds
......@@ -6,20 +6,29 @@
#include "usagecomputer.h"
#include <algorithm>
void UsageComputer::setTicks(unsigned long long system, unsigned long long user, unsigned long long wait, unsigned long long idle)
{
// according to the documentation some counters can go backwards in some circumstances
auto systemDiff = std::max(system - m_systemTicks, 0ull);
auto userDiff = std::max(user - m_userTicks, 0ull);
auto waitDiff = std::max(wait - m_waitTicks, 0ull);
unsigned long long totalTicks = system + user + wait + idle;
unsigned long long totalDiff = totalTicks - m_totalTicks;
auto totalDiff = std::max(totalTicks - m_totalTicks, 0ull);
auto percentage = [totalDiff] (unsigned long long tickDiff) {
// according to the documentation some counters can go backwards in some circumstances
return tickDiff > 0 ? 100.0 * tickDiff / totalDiff : 0;
if (tickDiff > 0 && totalDiff > 0) {
return 100.0 * tickDiff / totalDiff;
}
return 0.0;
};
systemUsage = percentage(system - m_systemTicks);
userUsage = percentage(user - m_userTicks);
waitUsage = percentage(wait - m_waitTicks);
totalUsage = percentage((system + user + wait) - (m_systemTicks + m_userTicks + m_waitTicks));
systemUsage = percentage(systemDiff);
userUsage = percentage(userDiff);
waitUsage = percentage(waitDiff);
totalUsage = percentage(systemDiff + userDiff + waitDiff);
m_totalTicks = totalTicks;
m_systemTicks = system;
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment