From 82ebad5e7a1bada94ce4e93f43bbd67d71e778c0 Mon Sep 17 00:00:00 2001
From: Patrick Ziegler
Date: Sat, 5 Dec 2020 22:58:38 +0100
Subject: [PATCH] fix(timer_module): Ensure that interval > 0 (#2274)
Since 3.5.0, we use m_interval for a modulo operation, this crashes the
bar if the interval is 0. A non-positive interval shouldn't be allowed
anyway, so we now throw an exception in that case.
Fixes #2273
---
include/modules/meta/timer_module.hpp | 14 ++++++++++++++
src/modules/counter.cpp | 4 ++--
src/modules/cpu.cpp | 2 +-
src/modules/date.cpp | 2 +-
src/modules/fs.cpp | 2 +-
src/modules/github.cpp | 2 +-
src/modules/memory.cpp | 2 +-
src/modules/network.cpp | 2 +-
src/modules/temperature.cpp | 2 +-
9 files changed, 23 insertions(+), 9 deletions(-)
diff --git a/include/modules/meta/timer_module.hpp b/include/modules/meta/timer_module.hpp
index d4f1a4a2..2cd5f3bf 100644
--- a/include/modules/meta/timer_module.hpp
+++ b/include/modules/meta/timer_module.hpp
@@ -17,6 +17,20 @@ namespace modules {
}
protected:
+ /**
+ * Loads and sets the interval for this module.
+ *
+ * Will throw an exception if a non-positive (<= 0) number is given.
+ */
+ void set_interval(interval_t def) {
+ m_interval = this->m_conf.template get(this->name(), "interval", def);
+
+ if (m_interval <= 0s) {
+ throw module_error(
+ this->name() + ": 'interval' must be larger than 0 (got '" + to_string(m_interval.count()) + "s')");
+ }
+ }
+
void runner() {
this->m_log.trace("%s: Thread id = %i", this->name(), concurrency_util::thread_id(this_thread::get_id()));
diff --git a/src/modules/counter.cpp b/src/modules/counter.cpp
index 25c7ae9e..3abd5e40 100644
--- a/src/modules/counter.cpp
+++ b/src/modules/counter.cpp
@@ -9,7 +9,7 @@ namespace modules {
counter_module::counter_module(const bar_settings& bar, string name_)
: timer_module(bar, move(name_)) {
- m_interval = m_conf.get(name(), "interval", m_interval);
+ set_interval(1s);
m_formatter->add(DEFAULT_FORMAT, TAG_COUNTER, {TAG_COUNTER});
}
@@ -25,6 +25,6 @@ namespace modules {
}
return false;
}
-}
+} // namespace modules
POLYBAR_NS_END
diff --git a/src/modules/cpu.cpp b/src/modules/cpu.cpp
index 78686a2d..3e3363df 100644
--- a/src/modules/cpu.cpp
+++ b/src/modules/cpu.cpp
@@ -16,7 +16,7 @@ namespace modules {
template class module;
cpu_module::cpu_module(const bar_settings& bar, string name_) : timer_module(bar, move(name_)) {
- m_interval = m_conf.get(name(), "interval", 1s);
+ set_interval(1s);
m_ramp_padding = m_conf.get(name(), "ramp-coreload-spacing", 1);
diff --git a/src/modules/date.cpp b/src/modules/date.cpp
index 127e7832..5bb4b6b0 100644
--- a/src/modules/date.cpp
+++ b/src/modules/date.cpp
@@ -22,7 +22,7 @@ namespace modules {
throw module_error("No date or time format specified");
}
- m_interval = m_conf.get(name(), "interval", 1s);
+ set_interval(1s);
m_formatter->add(DEFAULT_FORMAT, TAG_LABEL, {TAG_LABEL, TAG_DATE});
diff --git a/src/modules/fs.cpp b/src/modules/fs.cpp
index ad0f8820..ed1cc677 100644
--- a/src/modules/fs.cpp
+++ b/src/modules/fs.cpp
@@ -30,7 +30,7 @@ namespace modules {
m_remove_unmounted = m_conf.get(name(), "remove-unmounted", m_remove_unmounted);
m_fixed = m_conf.get(name(), "fixed-values", m_fixed);
m_spacing = m_conf.get(name(), "spacing", m_spacing);
- m_interval = m_conf.get(name(), "interval", 30s);
+ set_interval(30s);
// Add formats and elements
m_formatter->add(
diff --git a/src/modules/github.cpp b/src/modules/github.cpp
index 29ef5d1c..f40b15ac 100644
--- a/src/modules/github.cpp
+++ b/src/modules/github.cpp
@@ -23,7 +23,7 @@ namespace modules {
m_api_url += '/';
}
- m_interval = m_conf.get(name(), "interval", 60s);
+ set_interval(60s);
m_empty_notifications = m_conf.get(name(), "empty-notifications", m_empty_notifications);
m_formatter->add(DEFAULT_FORMAT, TAG_LABEL, {TAG_LABEL});
diff --git a/src/modules/memory.cpp b/src/modules/memory.cpp
index c07c06fc..5882da0f 100644
--- a/src/modules/memory.cpp
+++ b/src/modules/memory.cpp
@@ -16,7 +16,7 @@ namespace modules {
template class module;
memory_module::memory_module(const bar_settings& bar, string name_) : timer_module(bar, move(name_)) {
- m_interval = m_conf.get(name(), "interval", 1s);
+ set_interval(1s);
m_formatter->add(DEFAULT_FORMAT, TAG_LABEL, {TAG_LABEL, TAG_BAR_USED, TAG_BAR_FREE, TAG_RAMP_USED, TAG_RAMP_FREE,
TAG_BAR_SWAP_USED, TAG_BAR_SWAP_FREE, TAG_RAMP_SWAP_USED, TAG_RAMP_SWAP_FREE});
diff --git a/src/modules/network.cpp b/src/modules/network.cpp
index 21138777..cf7d2bd3 100644
--- a/src/modules/network.cpp
+++ b/src/modules/network.cpp
@@ -19,7 +19,7 @@ namespace modules {
m_ping_nth_update = m_conf.get(name(), "ping-interval", m_ping_nth_update);
m_udspeed_minwidth = m_conf.get(name(), "udspeed-minwidth", m_udspeed_minwidth);
m_accumulate = m_conf.get(name(), "accumulate-stats", m_accumulate);
- m_interval = m_conf.get(name(), "interval", 1s);
+ set_interval(1s);
m_unknown_up = m_conf.get(name(), "unknown-as-up", false);
m_conf.warn_deprecated(name(), "udspeed-minwidth", "%downspeed:min:max% and %upspeed:min:max%");
diff --git a/src/modules/temperature.cpp b/src/modules/temperature.cpp
index 8d2a50ca..98b195e5 100644
--- a/src/modules/temperature.cpp
+++ b/src/modules/temperature.cpp
@@ -19,7 +19,7 @@ namespace modules {
m_path = m_conf.get(name(), "hwmon-path", ""s);
m_tempbase = m_conf.get(name(), "base-temperature", 0);
m_tempwarn = m_conf.get(name(), "warn-temperature", 80);
- m_interval = m_conf.get(name(), "interval", 1s);
+ set_interval(1s);
m_units = m_conf.get(name(), "units", m_units);
if (m_path.empty()) {