Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 23 additions & 22 deletions src/util/run.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,17 @@ Date: August 2012
// clang-format on
#else

#include <cstring>
#include <cerrno>
#include <cstdio>
#include <cstdlib>

#include <fcntl.h>
#include <signal.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <unistd.h>
# include <sys/stat.h>
# include <sys/wait.h>

# include <cctype>
# include <cerrno>
# include <cstdio>
# include <cstdlib>
# include <cstring>
# include <fcntl.h>
# include <signal.h>
# include <unistd.h>

#endif

Expand Down Expand Up @@ -486,18 +487,18 @@ std::string shell_quote(const std::string &src)

// first check if quoting is needed at all

if(src.find(' ')==std::string::npos &&
src.find('"')==std::string::npos &&
src.find('*')==std::string::npos &&
src.find('$')==std::string::npos &&
src.find('\\')==std::string::npos &&
src.find('?')==std::string::npos &&
src.find('&')==std::string::npos &&
src.find('|')==std::string::npos &&
src.find('>')==std::string::npos &&
src.find('<')==std::string::npos &&
src.find('^')==std::string::npos &&
src.find('\'')==std::string::npos)
bool quotes_needed = false;

if(src.empty())
quotes_needed = true;
else
{
for(auto &ch : src)
if(!isalnum(ch) && ch != '_' && ch != '.' && ch != '/' && ch != '-')
quotes_needed = true;
}

if(!quotes_needed)
{
// seems fine -- return as is
return src;
Expand Down
18 changes: 18 additions & 0 deletions unit/util/run.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,24 @@ Author: Michael Tautschnig

#include <fstream>

SCENARIO("shell_quote() escaping", "[core][util][run]")
{
#ifdef _WIN32
REQUIRE(shell_quote("foo.bar") == "foo.bar");
REQUIRE(shell_quote("foo&bar") == "\"foo&bar\"");
REQUIRE(shell_quote("foo(bar)") == "\"foo(bar)\"");
REQUIRE(shell_quote("foo\"bar") == "\"foo\"\"bar\"");
#else
REQUIRE(shell_quote("foo.bar") == "foo.bar");
REQUIRE(shell_quote("foo/bar") == "foo/bar");
REQUIRE(shell_quote("--foo") == "--foo");
REQUIRE(shell_quote("") == "''");
REQUIRE(shell_quote("foo\nbar") == "'foo\nbar'");
REQUIRE(shell_quote("foo(bar)") == "'foo(bar)'");
REQUIRE(shell_quote("foo'bar") == "'foo'\\'''bar'");
#endif
}

SCENARIO("run() error reporting", "[core][util][run]")
{
GIVEN("A command invoking a non-existent executable")
Expand Down
Loading