From e2e184b8d4e5f59d09810f492613566cf22492f3 Mon Sep 17 00:00:00 2001 From: Tomas Babej Date: Sat, 21 Aug 2021 00:45:12 -0400 Subject: [PATCH] CmdConfig: Be more strict when matching confiuration variables Allow only leading spaces in front of configuration variables, as opposed to arbitrary strings. This prevents matching variables like "report.list.context=" when one is seeking to modify "context=". --- src/commands/CmdConfig.cpp | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/src/commands/CmdConfig.cpp b/src/commands/CmdConfig.cpp index c4270115b..03fc6e6ba 100644 --- a/src/commands/CmdConfig.cpp +++ b/src/commands/CmdConfig.cpp @@ -64,23 +64,27 @@ bool CmdConfig::setConfigVariable ( for (auto& line : contents) { + // Get l-trimmed version of the line + auto trimmed_line = trim (line, " "); + // If there is a comment on the line, it must follow the pattern. auto comment = line.find ('#'); - auto pos = line.find (name + '='); + auto pos = trimmed_line.find (name + '='); - if (pos != std::string::npos && - (comment == std::string::npos || - comment > pos)) + // TODO: Use std::regex here + if (pos == 0) { found = true; if (!confirmation || confirm (format ("Are you sure you want to change the value of '{1}' from '{2}' to '{3}'?", name, Context::getContext ().config.get (name), value))) { - line = name + '=' + json::encode (value); + auto new_line = line.substr (0, pos + name.length () + 1) + json::encode (value); if (comment != std::string::npos) line += ' ' + line.substr (comment); + // Rewrite the line + line = new_line; change = true; } } @@ -115,13 +119,13 @@ int CmdConfig::unsetConfigVariable (const std::string& name, bool confirmation / { auto lineDeleted = false; - // If there is a comment on the line, it must follow the pattern. - auto comment = line->find ('#'); - auto pos = line->find (name + '='); + // Get l-trimmed version of the line - if (pos != std::string::npos && - (comment == std::string::npos || - comment > pos)) + // If there is a comment on the line, it must follow the pattern. + auto pos = trim (*line, " ").find (name + '='); + + // TODO: Use std::regex here + if (pos == 0) { found = true;