Commit b46f6f20 authored by Will Harris's avatar Will Harris Committed by Commit Bot

Fix crash in TokenizeCommandLineToArray.

Previously, a command line where the first argument had an unterminated
double-quote would cause a crash.

This CL also has some cleanup of dead code.

BUG=869194

Change-Id: I20fa0814e35c5d10ab3ad76047c8233297f14a8d
Reviewed-on: https://chromium-review.googlesource.com/1155991
Commit-Queue: Will Harris <wfh@chromium.org>
Reviewed-by: default avatarGreg Thompson <grt@chromium.org>
Cr-Commit-Position: refs/heads/master@{#579526}
parent 761ea190
......@@ -731,11 +731,16 @@ std::vector<std::wstring> TokenizeCommandLineToArray(
// The first argument (the program) is delimited by whitespace or quotes based
// on its first character.
int argv0_length = 0;
if (p[0] == L'"')
argv0_length = wcschr(++p, L'"') - (command_line.c_str() + 1);
else
size_t argv0_length = 0;
if (p[0] == L'"') {
const wchar_t* closing = wcschr(++p, L'"');
if (!closing)
argv0_length = command_line.size() - 1; // Skip the opening quote.
else
argv0_length = closing - (command_line.c_str() + 1);
} else {
argv0_length = wcscspn(p, kSpaceTab);
}
result.emplace_back(p, argv0_length);
if (p[argv0_length] == 0)
return result;
......@@ -748,11 +753,8 @@ std::vector<std::wstring> TokenizeCommandLineToArray(
p += wcsspn(p, kSpaceTab);
// End of arguments.
if (p[0] == 0) {
if (!token.empty())
result.push_back(token);
if (p[0] == 0)
break;
}
state = SpecialChars::kInterpret;
......
......@@ -93,6 +93,10 @@ TEST(InstallStaticTest, GetSwitchValueFromCommandLineTest) {
value = GetSwitchValueFromCommandLine(L"c:\\temp\\bleh.exe --type=\t\t\t",
L"type");
EXPECT_TRUE(value.empty());
// Bad command line without closing quotes. Should not crash.
value = GetSwitchValueFromCommandLine(L"\"blah --type=\t\t\t", L"type");
EXPECT_TRUE(value.empty());
}
TEST(InstallStaticTest, SpacesAndQuotesInCommandLineArguments) {
......@@ -156,15 +160,20 @@ TEST(InstallStaticTest, SpacesAndQuotesInCommandLineArguments) {
tokenized = TokenizeCommandLineToArray(
L"\"C:\\with space\\b.exe\" --stuff=\"d:\\stuff and things\"");
EXPECT_EQ(2u, tokenized.size());
ASSERT_EQ(2u, tokenized.size());
EXPECT_EQ(L"C:\\with space\\b.exe", tokenized[0]);
EXPECT_EQ(L"--stuff=d:\\stuff and things", tokenized[1]);
tokenized = TokenizeCommandLineToArray(
L"\"C:\\with space\\b.exe\" \\\\\\\"\"");
EXPECT_EQ(2u, tokenized.size());
ASSERT_EQ(2u, tokenized.size());
EXPECT_EQ(L"C:\\with space\\b.exe", tokenized[0]);
EXPECT_EQ(L"\\\"", tokenized[1]);
tokenized =
TokenizeCommandLineToArray(L"\"blah --type=\t\t\t no closing quote");
ASSERT_EQ(1u, tokenized.size());
EXPECT_EQ(L"blah --type=\t\t\t no closing quote", tokenized[0]);
}
// Test cases from
......
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