From af3c868a5b63b1e77b56801ca558f76afcc21ed6 Mon Sep 17 00:00:00 2001 From: Matthias Richter Date: Wed, 21 Apr 2021 12:15:25 +0200 Subject: [PATCH] Catch exception on erroneous rules std::regex and std::regex_replace may throw an std::regex_error if the expression or replacement contain errors. Log this error and carry on with the next rule, so that the title is shown even if the config contains errors. --- src/modules/sway/window.cpp | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/modules/sway/window.cpp b/src/modules/sway/window.cpp index 3b424c2f..04910394 100644 --- a/src/modules/sway/window.cpp +++ b/src/modules/sway/window.cpp @@ -141,9 +141,15 @@ std::string Window::rewriteTitle(const std::string& title) for (auto it = rules.begin(); it != rules.end(); ++it) { if (it.key().isString() && it->isString()) { - const std::regex rule{it.key().asString()}; - if (std::regex_match(title, rule)) { - return std::regex_replace(title, rule, it->asString()); + try { + // malformated regexes will cause an exception. + // in this case, log error and try the next rule. + const std::regex rule{it.key().asString()}; + if (std::regex_match(title, rule)) { + return std::regex_replace(title, rule, it->asString()); + } + } catch (const std::regex_error& e) { + spdlog::error("Invalid rule {}: {}", it.key().asString(), e.what()); } } }