Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1256)

Side by Side Diff: chrome/install_static/install_util.cc

Issue 2543503003: Handle spaces and quotes in chrome_elf command line parser (Closed)
Patch Set: refactor argv0 a bit Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « chrome/install_static/install_util.h ('k') | chrome/install_static/install_util_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/install_static/install_util.h" 5 #include "chrome/install_static/install_util.h"
6 6
7 #include <windows.h> 7 #include <windows.h>
8 #include <assert.h> 8 #include <assert.h>
9 #include <stdlib.h> 9 #include <stdlib.h>
10 #include <string.h> 10 #include <string.h>
(...skipping 563 matching lines...) Expand 10 before | Expand all | Expand 10 after
574 bool trim_spaces) { 574 bool trim_spaces) {
575 return TokenizeStringT<std::string>(str, delimiter, trim_spaces); 575 return TokenizeStringT<std::string>(str, delimiter, trim_spaces);
576 } 576 }
577 577
578 std::vector<std::wstring> TokenizeString16(const std::wstring& str, 578 std::vector<std::wstring> TokenizeString16(const std::wstring& str,
579 wchar_t delimiter, 579 wchar_t delimiter,
580 bool trim_spaces) { 580 bool trim_spaces) {
581 return TokenizeStringT<std::wstring>(str, delimiter, trim_spaces); 581 return TokenizeStringT<std::wstring>(str, delimiter, trim_spaces);
582 } 582 }
583 583
584 std::vector<std::wstring> TokenizeCommandLineToArray(
585 const std::wstring& command_line) {
586 // This is baroquely complex to do properly, see e.g.
587 // https://blogs.msdn.microsoft.com/oldnewthing/20100917-00/?p=12833
588 // http://www.windowsinspired.com/how-a-windows-programs-splits-its-command-li ne-into-individual-arguments/
589 // and many others. We cannot use CommandLineToArgvW() in chrome_elf, because
590 // it's in shell32.dll. Previously, __wgetmainargs() in the CRT was available,
591 // and it's still documented for VS 2015 at
592 // https://msdn.microsoft.com/en-us/library/ff770599.aspx but unfortunately,
593 // isn't actually available.
594 //
595 // This parsing matches CommandLineToArgvW()s for arguments, rather than the
596 // CRTs. These are different only in the most obscure of cases and will not
597 // matter in any practical situation. See the windowsinspired.com post above
598 // for details.
599 //
600 // Indicates whether or not space and tab are interpreted as token separators.
601 enum class SpecialChars {
602 // Space or tab, if encountered, delimit tokens.
603 kInterpret,
604
605 // Space or tab, if encountered, are part of the current token.
606 kIgnore,
607 } state;
608
609 std::vector<std::wstring> result;
610 const wchar_t* p = command_line.c_str();
611
612 // The first argument (the program) is handled differently than other
grt (UTC plus 2) 2016/12/02 10:18:03 is the the same? // The first argument (the pro
scottmg 2016/12/02 21:25:54 Almost! With a couple tweaks passes all the tests
613 // arguments, roughly just what's between double quotes without backslash
614 // interpretation.
615 if (p[0] == L'"') {
616 state = SpecialChars::kIgnore;
617 ++p;
618 } else {
619 state = SpecialChars::kInterpret;
620 }
621
622 std::wstring argv0_token;
623 for (;;) {
624 if (p[0] == 0) {
625 result.push_back(argv0_token);
626 return result;
627 } else if ((state == SpecialChars::kIgnore && p[0] == L'"') ||
628 (state == SpecialChars::kInterpret && wcschr(L" \t", p[0]))) {
grt (UTC plus 2) 2016/12/02 10:18:03 since L" \t" appears a bunch, how about static c
scottmg 2016/12/02 21:25:55 Done.
629 ++p;
630 result.push_back(argv0_token);
631 break;
632 } else {
633 argv0_token += *p++;
634 }
635 }
636
637 std::wstring token;
638 // This loops the entire string, with a subloop for each argument.
639 for (;;) {
640 // Advance past leading whitespace (only space and tab are handled).
641 p += wcsspn(p, L" \t");
642
643 // End of arguments.
644 if (p[0] == 0) {
645 if (!token.empty())
646 result.push_back(token);
647 break;
648 }
649
650 state = SpecialChars::kInterpret;
651
652 // Scan an argument.
653 for (;;) {
654 // Count and advance past collections of backslashes, which have special
655 // meaning when followed by a double quote.
656 int num_backslashes = 0;
Nico 2016/12/02 01:02:33 nit: int num_backslashes = wstrspn(p, L"\\"); p +
scottmg 2016/12/02 21:25:54 Thanks, done. I guess I just don't know C very wel
657 while (p[0] == L'\\') {
658 ++p;
659 ++num_backslashes;
660 }
661
662 if (p[0] == L'"') {
663 // Emit a backslash for each pair of backslashes found. A non-paired
664 // "extra" backslash is handled below.
665 token.append(num_backslashes / 2, L'\\');
666
667 if (num_backslashes % 2 == 1) {
668 // An odd number of backslashes followed by a quote is treated as
669 // pairs of protected backslashes, followed by the protected quote.
670 token += L'"';
671 } else if (p[1] == L'"' && state == SpecialChars::kIgnore) {
672 // Special case for consecutive double quotes in a string: emit one
grt (UTC plus 2) 2016/12/02 10:18:03 nit: it's for consecutive double quotes within a q
scottmg 2016/12/02 21:25:54 Yup, done. I mean, isn't it totally obvious, and c
673 // for the pair, and switch back to interpreting special characters.
674 ++p;
675 token += L'"';
676 state = SpecialChars::kInterpret;
677 } else {
678 state = state == SpecialChars::kInterpret ? SpecialChars::kIgnore
679 : SpecialChars::kInterpret;
680 }
681 } else {
682 // Emit backslashes that do not precede a quote verbatim.
683 token.append(num_backslashes, L'\\');
684 if (p[0] == 0 ||
685 (state == SpecialChars::kInterpret && wcschr(L" \t", p[0]))) {
686 result.push_back(token);
687 token.clear();
688 break;
689 }
690
691 token += *p;
692 }
693
694 ++p;
695 }
696 }
697
698 return result;
699 }
700
584 std::wstring GetSwitchValueFromCommandLine(const std::wstring& command_line, 701 std::wstring GetSwitchValueFromCommandLine(const std::wstring& command_line,
585 const std::wstring& switch_name) { 702 const std::wstring& switch_name) {
586 assert(!command_line.empty()); 703 assert(!command_line.empty());
587 assert(!switch_name.empty()); 704 assert(!switch_name.empty());
588 705
589 std::wstring command_line_copy = command_line; 706 std::vector<std::wstring> as_array = TokenizeCommandLineToArray(command_line);
590 // Remove leading and trailing spaces. 707 std::wstring switch_with_equal = L"--" + switch_name + L"=";
591 TrimT<std::wstring>(&command_line_copy); 708 for (size_t i = 1; i < as_array.size(); ++i) {
709 const std::wstring& arg = as_array[i];
710 if (arg.compare(0, switch_with_equal.size(), switch_with_equal) == 0)
711 return arg.substr(switch_with_equal.size());
712 }
592 713
593 // Find the switch in the command line. If we don't find the switch, return 714 return std::wstring();
594 // an empty string.
595 std::wstring switch_token = L"--";
596 switch_token += switch_name;
597 switch_token += L"=";
598 size_t switch_offset = command_line_copy.find(switch_token);
599 if (switch_offset == std::string::npos)
600 return std::wstring();
601
602 // The format is "--<switch name>=blah". Look for a space after the
603 // "--<switch name>=" string. If we don't find a space assume that the switch
604 // value ends at the end of the command line.
605 size_t switch_value_start_offset = switch_offset + switch_token.length();
606 if (std::wstring(kWhiteSpaces16).find(
607 command_line_copy[switch_value_start_offset]) != std::wstring::npos) {
608 switch_value_start_offset = command_line_copy.find_first_not_of(
609 GetWhiteSpacesForType<std::wstring>(), switch_value_start_offset);
610 if (switch_value_start_offset == std::wstring::npos)
611 return std::wstring();
612 }
613 size_t switch_value_end_offset =
614 command_line_copy.find_first_of(GetWhiteSpacesForType<std::wstring>(),
615 switch_value_start_offset);
616 if (switch_value_end_offset == std::wstring::npos)
617 switch_value_end_offset = command_line_copy.length();
618
619 std::wstring switch_value = command_line_copy.substr(
620 switch_value_start_offset,
621 switch_value_end_offset - (switch_offset + switch_token.length()));
622 TrimT<std::wstring>(&switch_value);
623 return switch_value;
624 } 715 }
625 716
626 bool RecursiveDirectoryCreate(const std::wstring& full_path) { 717 bool RecursiveDirectoryCreate(const std::wstring& full_path) {
627 // If the path exists, we've succeeded if it's a directory, failed otherwise. 718 // If the path exists, we've succeeded if it's a directory, failed otherwise.
628 const wchar_t* full_path_str = full_path.c_str(); 719 const wchar_t* full_path_str = full_path.c_str();
629 DWORD file_attributes = ::GetFileAttributes(full_path_str); 720 DWORD file_attributes = ::GetFileAttributes(full_path_str);
630 if (file_attributes != INVALID_FILE_ATTRIBUTES) { 721 if (file_attributes != INVALID_FILE_ATTRIBUTES) {
631 if ((file_attributes & FILE_ATTRIBUTE_DIRECTORY) != 0) { 722 if ((file_attributes & FILE_ATTRIBUTE_DIRECTORY) != 0) {
632 Trace(L"%hs( %ls directory exists )\n", __func__, full_path_str); 723 Trace(L"%hs( %ls directory exists )\n", __func__, full_path_str);
633 return true; 724 return true;
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
683 case ChannelStrategy::ADDITIONAL_PARAMETERS: 774 case ChannelStrategy::ADDITIONAL_PARAMETERS:
684 return ChannelFromAdditionalParameters(mode, system_level, multi_install); 775 return ChannelFromAdditionalParameters(mode, system_level, multi_install);
685 case ChannelStrategy::FIXED: 776 case ChannelStrategy::FIXED:
686 return mode.default_channel_name; 777 return mode.default_channel_name;
687 } 778 }
688 779
689 return std::wstring(); 780 return std::wstring();
690 } 781 }
691 782
692 } // namespace install_static 783 } // namespace install_static
OLDNEW
« no previous file with comments | « chrome/install_static/install_util.h ('k') | chrome/install_static/install_util_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698