This commit is contained in:
Günter Obiltschnig 2020-02-14 09:28:21 +01:00
parent 888d8e6fb9
commit 76012717b0
3 changed files with 24 additions and 6 deletions

View File

@ -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;
}

View File

@ -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);

View File

@ -24,6 +24,7 @@ public:
ProcessTest(const std::string& name);
~ProcessTest();
void testEscapeArgs();
void testLaunch();
void testLaunchRedirectIn();
void testLaunchRedirectOut();