Prevent segfault in getCpuFrequency

When parseCpuFrequencies returns an empty vector, getCpuFrequency
would attempt to dereference an invalid iterator.

Return early from getCpuFrequency when parseCpuFrequencies returns an
empty vector.

Resolves a segfault when waybar is run within a VM on apple silicon.
pull/1563/head
Ed Ralston 2022-05-19 20:01:57 -04:00
parent 9bc821bdac
commit 7abcbe80e5
No known key found for this signature in database
GPG Key ID: F474EA339972086B
1 changed files with 4 additions and 0 deletions

View File

@ -94,6 +94,10 @@ std::tuple<std::vector<uint16_t>, std::string> waybar::modules::Cpu::getCpuUsage
std::tuple<float, float, float> waybar::modules::Cpu::getCpuFrequency() { std::tuple<float, float, float> waybar::modules::Cpu::getCpuFrequency() {
std::vector<float> frequencies = parseCpuFrequencies(); std::vector<float> frequencies = parseCpuFrequencies();
if (frequencies.empty())
{
return {0.f, 0.f, 0.f};
}
auto [min, max] = std::minmax_element(std::begin(frequencies), std::end(frequencies)); auto [min, max] = std::minmax_element(std::begin(frequencies), std::end(frequencies));
float avg_frequency = float avg_frequency =
std::accumulate(std::begin(frequencies), std::end(frequencies), 0.0) / frequencies.size(); std::accumulate(std::begin(frequencies), std::end(frequencies), 0.0) / frequencies.size();