From 8441d5e124658ff226cfed6ac640832d82f816a5 Mon Sep 17 00:00:00 2001 From: Alex Date: Sun, 5 Jul 2026 10:10:10 +0200 Subject: [PATCH] config: harden output-dimensions parsing against malformed values The parser assumed each entry was ' '. An entry with no space caused str.substr((size_t)-1) to throw out_of_range, and a non-integer value made std::stoi throw invalid_argument, failing the whole bar on that output. Validate spaces with find()!=npos and wrap stoi in try/catch; log a warning and skip malformed entries instead of throwing. --- src/config.cpp | 33 +++++++++++++++++++++++++++------ 1 file changed, 27 insertions(+), 6 deletions(-) diff --git a/src/config.cpp b/src/config.cpp index ad136339..800b6b02 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -240,12 +240,33 @@ bool isValidOutput(const Json::Value& config, const std::string& name, continue; } std::string str = config_output_dimension.asString(); - int i = str.find(" "); - std::string dimension = str.substr(0, i); - str = str.substr(i + 1); - i = str.find(" "); - std::string comparator = str.substr(0, i); - int value = std::stoi(str.substr(i)); + auto first_space = str.find(' '); + if (first_space == std::string::npos) { + spdlog::warn( + "Ignoring malformed 'output-dimensions' entry (expected ' " + "'): '{}'", + str); + continue; + } + std::string dimension = str.substr(0, first_space); + str = str.substr(first_space + 1); + auto second_space = str.find(' '); + if (second_space == std::string::npos) { + spdlog::warn( + "Ignoring malformed 'output-dimensions' entry (expected ' " + "'): '{}'", + config_output_dimension.asString()); + continue; + } + std::string comparator = str.substr(0, second_space); + int value; + try { + value = std::stoi(str.substr(second_space + 1)); + } catch (const std::exception& e) { + spdlog::warn("Ignoring 'output-dimensions' entry with non-integer value: '{}'", + config_output_dimension.asString()); + continue; + } int comparison_value; if (dimension == "height") {