Merge pull request #7092 from Icinga/bugfix/command-escape-windows-4849

Fix Windows command escape for \"
This commit is contained in:
Alexander Aleksandrovič Klimov 2021-02-01 11:20:44 +01:00 committed by GitHub
commit dbdfe189c0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 37 additions and 1 deletions

View File

@ -1148,7 +1148,7 @@ String Utility::EscapeCreateProcessArg(const String& arg)
result.Append(numBackslashes * 2, '\\');
break;
} else if (*it == '"') {
result.Append(numBackslashes * 2, '\\');
result.Append(numBackslashes * 2 + 1, '\\');
result.Append(1, *it);
} else {
result.Append(numBackslashes, '\\');

View File

@ -114,6 +114,7 @@ add_boost_test(base
base_utility/comparepasswords_works
base_utility/comparepasswords_issafe
base_utility/validateutf8
base_utility/EscapeCreateProcessArg
base_value/scalar
base_value/convert
base_value/format

View File

@ -4,6 +4,12 @@
#include <chrono>
#include <BoostTestTargetConfig.h>
#ifdef _WIN32
# include <windows.h>
# include <shellapi.h>
# include <atlstr.h>
#endif /* _WIN32 */
using namespace icinga;
BOOST_AUTO_TEST_SUITE(base_utility)
@ -75,4 +81,33 @@ BOOST_AUTO_TEST_CASE(validateutf8)
BOOST_CHECK(Utility::ValidateUTF8("\xC3\xA4") == "\xC3\xA4");
}
BOOST_AUTO_TEST_CASE(EscapeCreateProcessArg)
{
#ifdef _WIN32
std::vector<std::string> testdata = {
R"(foobar)",
R"(foo bar)",
R"(foo"bar)",
R"("foo bar")",
R"(" \" \\" \\\" \\\\")",
R"( !"#$$%&'()*+,-./09:;<=>?@AZ[\]^_`az{|}~ " \" \\" \\\" \\\\")",
"'foo\nbar'",
};
for (const auto& t : testdata) {
// Prepend some fake exec name as the first argument is handled differently.
std::string escaped = "some.exe " + Utility::EscapeCreateProcessArg(t);
int argc;
std::shared_ptr<LPWSTR> argv(CommandLineToArgvW(CA2W(escaped.c_str()), &argc), LocalFree);
BOOST_CHECK_MESSAGE(argv != nullptr, "CommandLineToArgvW() should not return nullptr for " << t);
BOOST_CHECK_MESSAGE(argc == 2, "CommandLineToArgvW() should find 2 arguments for " << t);
if (argc >= 2) {
std::string unescaped = CW2A(argv.get()[1]);
BOOST_CHECK_MESSAGE(unescaped == t,
"CommandLineToArgvW() should return original value for " << t << " (got: " << unescaped << ")");
}
}
#endif /* _WIN32 */
}
BOOST_AUTO_TEST_SUITE_END()