Commit 08965161 authored by Greg Thompson's avatar Greg Thompson Committed by Commit Bot

Ignore switches following "--" when parsing a command line.

BUG=933004
R=wfh@chromium.org

Change-Id: I911be4cbfc38a4d41dec85d85f7fe0f50ddca392
Reviewed-on: https://chromium-review.googlesource.com/c/1481210
Auto-Submit: Greg Thompson <grt@chromium.org>
Commit-Queue: Julian Pastarmov <pastarmovj@chromium.org>
Reviewed-by: default avatarJulian Pastarmov <pastarmovj@chromium.org>
Cr-Commit-Position: refs/heads/master@{#634604}
parent fba5eff0
......@@ -847,13 +847,15 @@ std::vector<std::wstring> TokenizeCommandLineToArray(
std::wstring GetSwitchValueFromCommandLine(const std::wstring& command_line,
const std::wstring& switch_name) {
static constexpr wchar_t kSwitchTerminator[] = L"--";
assert(!command_line.empty());
assert(!switch_name.empty());
std::vector<std::wstring> as_array = TokenizeCommandLineToArray(command_line);
std::wstring switch_with_equal = L"--" + switch_name + L"=";
for (size_t i = 1; i < as_array.size(); ++i) {
const std::wstring& arg = as_array[i];
auto end = std::find(as_array.cbegin(), as_array.cend(), kSwitchTerminator);
for (auto scan = as_array.cbegin(); scan != end; ++scan) {
const std::wstring& arg = *scan;
if (arg.compare(0, switch_with_equal.size(), switch_with_equal) == 0)
return arg.substr(switch_with_equal.size());
}
......
......@@ -275,9 +275,10 @@ std::vector<std::wstring> TokenizeString16(const std::wstring& str,
std::vector<std::wstring> TokenizeCommandLineToArray(
const std::wstring& command_line);
// We assume that the command line |command_line| contains multiple switches
// with the format --<switch name>=<switch value>. This function returns the
// value of the |switch_name| passed in.
// Returns the value of a switch of the form "--<switch name>=<switch value>" in
// |command_line|. An empty switch in |command_line| ("--") denotes the end of
// switches and the beginning of args. Anything of the form --<switch
// name>=<switch value> following "--" is ignored.
std::wstring GetSwitchValueFromCommandLine(const std::wstring& command_line,
const std::wstring& switch_name);
......
......@@ -96,6 +96,10 @@ TEST(InstallStaticTest, GetSwitchValueFromCommandLineTest) {
// Bad command line without closing quotes. Should not crash.
value = GetSwitchValueFromCommandLine(L"\"blah --type=\t\t\t", L"type");
EXPECT_TRUE(value.empty());
// Anything following "--" should be considered args and therfore ignored.
value = GetSwitchValueFromCommandLine(L"blah -- --type=bleh", L"type");
EXPECT_TRUE(value.empty());
}
TEST(InstallStaticTest, SpacesAndQuotesInCommandLineArguments) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment