diff --git a/Foundation/src/Process_WIN32U.cpp b/Foundation/src/Process_WIN32U.cpp index d26ebd04a..2a553fd90 100644 --- a/Foundation/src/Process_WIN32U.cpp +++ b/Foundation/src/Process_WIN32U.cpp @@ -176,14 +176,14 @@ bool ProcessImpl::mustEscapeArg(const std::string& arg) inQuotes = !inQuotes; } } - return result && !inQuotes; + return result || inQuotes; } // Based on code from https://blogs.msdn.microsoft.com/twistylittlepassagesallalike/2011/04/23/everyone-quotes-command-line-arguments-the-wrong-way/ std::string ProcessImpl::escapeArg(const std::string& arg) { - if (mostEscapeArg(arg)) + if (mustEscapeArg(arg)) { std::string quotedArg("\""); for (std::string::const_iterator it = arg.begin(); ; ++it) @@ -214,10 +214,7 @@ std::string ProcessImpl::escapeArg(const std::string& arg) quotedArg.push_back('"'); return quotedArg; } - else - { - return arg; - } + else return arg; } diff --git a/Foundation/testsuite/src/ProcessTest.cpp b/Foundation/testsuite/src/ProcessTest.cpp index 1681a774f..cac8718e8 100644 --- a/Foundation/testsuite/src/ProcessTest.cpp +++ b/Foundation/testsuite/src/ProcessTest.cpp @@ -16,6 +16,7 @@ #include "Poco/PipeStream.h" +using namespace std::string_literals; using Poco::Process; using Poco::ProcessHandle; using Poco::Pipe; @@ -33,6 +34,24 @@ ProcessTest::~ProcessTest() } +void ProcessTest::testEscapeArgs() +{ +#if defined(_WIN32) + assertTrue (Poco::ProcessImpl::mustEscapeArg("a b")); + assertFalse (Poco::ProcessImpl::mustEscapeArg("abc")); + assertFalse (Poco::ProcessImpl::mustEscapeArg("\"a b \"")); + assertFalse (Poco::ProcessImpl::mustEscapeArg("\"abc\"")); + assertTrue (Poco::ProcessImpl::mustEscapeArg("\"a b ")); + assertFalse (Poco::ProcessImpl::mustEscapeArg("/arg=\"a b c\"")); + + assertEquals ("abc"s, Poco::ProcessImpl::escapeArg("abc")); + assertEquals ("\"a b c\""s, Poco::ProcessImpl::escapeArg("a b c")); + assertEquals ("\"a b \\\" c\""s, Poco::ProcessImpl::escapeArg("a b \" c")); + assertEquals ("/arg=\"a b c\""s, Poco::ProcessImpl::escapeArg("/arg=\"a b c\"")); +#endif +} + + void ProcessTest::testLaunch() { std::string name("TestApp"); @@ -253,6 +272,7 @@ CppUnit::Test* ProcessTest::suite() { CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("ProcessTest"); + CppUnit_addTest(pSuite, ProcessTest, testEscapeArgs); CppUnit_addTest(pSuite, ProcessTest, testLaunch); CppUnit_addTest(pSuite, ProcessTest, testLaunchRedirectIn); CppUnit_addTest(pSuite, ProcessTest, testLaunchRedirectOut); diff --git a/Foundation/testsuite/src/ProcessTest.h b/Foundation/testsuite/src/ProcessTest.h index 130f4ad22..a99a77645 100644 --- a/Foundation/testsuite/src/ProcessTest.h +++ b/Foundation/testsuite/src/ProcessTest.h @@ -24,6 +24,7 @@ public: ProcessTest(const std::string& name); ~ProcessTest(); + void testEscapeArgs(); void testLaunch(); void testLaunchRedirectIn(); void testLaunchRedirectOut();