From bdfdd92a5a3c5396c73782d76e34bdc5fbe5b4da Mon Sep 17 00:00:00 2001 From: Jakob Erdmann Date: Thu, 12 Dec 2024 10:47:22 +0100 Subject: [PATCH] fix #15899 --- src/netedit/tools/GNEPythonTool.cpp | 4 ++-- src/utils/common/StringUtils.cpp | 7 +++++++ src/utils/common/StringUtils.h | 5 +++++ 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/netedit/tools/GNEPythonTool.cpp b/src/netedit/tools/GNEPythonTool.cpp index 4ba3bc257338..09fdfc616a20 100644 --- a/src/netedit/tools/GNEPythonTool.cpp +++ b/src/netedit/tools/GNEPythonTool.cpp @@ -150,7 +150,7 @@ GNEPythonTool::getCommand() const { } arguments += " "; } else { - arguments += ("\"" + option.second->getValueString() + "\" "); + arguments += ("\"" + StringUtils::escapeShell(option.second->getValueString()) + "\" "); } } } @@ -209,7 +209,7 @@ GNEPythonTool::saveConfiguration(const std::string& file) const { if (option.second->isBool()) { command += ("--" + option.first + " "); } else { - command += ("--" + option.first + " \"" + option.second->getValueString() + "\" "); + command += ("--" + option.first + " \"" + StringUtils::escapeShell(option.second->getValueString()) + "\" "); } } } diff --git a/src/utils/common/StringUtils.cpp b/src/utils/common/StringUtils.cpp index b13c47c5a525..af36ee49d9d4 100644 --- a/src/utils/common/StringUtils.cpp +++ b/src/utils/common/StringUtils.cpp @@ -246,6 +246,13 @@ StringUtils::escapeXML(const std::string& orig, const bool maskDoubleHyphen) { } +std::string +StringUtils::escapeShell(const std::string& orig) { + std::string result = replace(orig, "\"", "\\\""); + return result; +} + + std::string StringUtils::urlEncode(const std::string& toEncode, const std::string encodeWhich) { std::ostringstream out; diff --git a/src/utils/common/StringUtils.h b/src/utils/common/StringUtils.h index bad77f5d3362..5b596ff2c219 100644 --- a/src/utils/common/StringUtils.h +++ b/src/utils/common/StringUtils.h @@ -82,6 +82,11 @@ class StringUtils { */ static std::string escapeXML(const std::string& orig, const bool maskDoubleHyphen = false); + /** + * @brief Escape special characters with backslash + */ + static std::string escapeShell(const std::string& orig); + /// @brief An empty string static std::string emptyString;