Skip to content

Commit

Permalink
Merge pull request #3160 from STEllAR-GROUP/fixing_commandline_handling
Browse files Browse the repository at this point in the history
Fixing the handling of quoted command line arguments.
  • Loading branch information
hkaiser committed Feb 10, 2018
2 parents c45c82e + 95bb5d8 commit a469efe
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
2 changes: 1 addition & 1 deletion hpx/util/parse_command_line.hpp
Expand Up @@ -67,7 +67,7 @@ namespace hpx { namespace util
{
inline std::string enquote(std::string const& arg)
{
if (arg.find_first_of(" \t") != std::string::npos)
if (arg.find_first_of(" \t\"") != std::string::npos)
return std::string("\"") + arg + "\"";
return arg;
}
Expand Down
22 changes: 16 additions & 6 deletions src/util/command_line_handling.cpp
Expand Up @@ -41,6 +41,7 @@
#include <memory>
#include <sstream>
#include <string>
#include <utility>
#include <vector>

namespace hpx { namespace util
Expand All @@ -67,13 +68,14 @@ namespace hpx { namespace util
}

///////////////////////////////////////////////////////////////////////
inline void encode (std::string &str, char s, char const *r)
inline void encode(
std::string& str, char s, char const* r, std::size_t inc = 1ull)
{
std::string::size_type pos = 0;
while ((pos = str.find_first_of(s, pos)) != std::string::npos)
{
str.replace (pos, 1, r);
++pos;
pos += inc;
}
}

Expand All @@ -83,6 +85,12 @@ namespace hpx { namespace util
return str;
}

inline std::string encode_and_enquote(std::string str)
{
encode(str, '\"', "\\\"", 2);
return detail::enquote(std::move(str));
}

///////////////////////////////////////////////////////////////////////
void report_thread_warning(std::string const& batch_name,
std::size_t threads, std::size_t batch_threads)
Expand Down Expand Up @@ -986,7 +994,7 @@ namespace hpx { namespace util
{
// quote only if it contains whitespace
std::string arg(argv[i]); //-V108
cmd_line += detail::enquote(arg);
cmd_line += detail::encode_and_enquote(arg);

if ((i + 1) != argc)
cmd_line += " ";
Expand All @@ -1010,15 +1018,17 @@ namespace hpx { namespace util

iterator_type end = unregistered_options.end();
for (iterator_type it = unregistered_options.begin(); it != end; ++it)
unregistered_options_cmd_line += " " + detail::enquote(*it);
unregistered_options_cmd_line +=
" " + detail::encode_and_enquote(*it);

ini_config_ += "hpx.unknown_cmd_line!=" +
detail::enquote(cmd_name) + unregistered_options_cmd_line;
detail::encode_and_enquote(cmd_name) +
unregistered_options_cmd_line;
}

ini_config_ += "hpx.program_name!=" + cmd_name;
ini_config_ += "hpx.reconstructed_cmd_line!=" +
detail::enquote(cmd_name) + " " +
detail::encode_and_enquote(cmd_name) + " " +
util::reconstruct_command_line(vm_) + " " +
unregistered_options_cmd_line;
}
Expand Down

0 comments on commit a469efe

Please sign in to comment.