Browse Source

fix: wide string concatenation (#40909)

* fix: wide string concatenation

Co-authored-by: clavin <[email protected]>

* Use wstring_views to keep length in context

Co-authored-by: clavin <[email protected]>

* forgot a space, oopsies

Co-authored-by: clavin <[email protected]>

* chore: update patches

* use AsWStringPiece
This code is being merged to a branch before the change from crrev.com/c/5010979

---------

Co-authored-by: trop[bot] <37223003+trop[bot]@users.noreply.github.com>
Co-authored-by: clavin <[email protected]>
Co-authored-by: PatchUp <73610968+patchup[bot]@users.noreply.github.com>
trop[bot] 1 year ago
parent
commit
4120b8f72a
2 changed files with 13 additions and 13 deletions
  1. 9 10
      shell/browser/browser_win.cc
  2. 4 3
      shell/browser/relauncher_win.cc

+ 9 - 10
shell/browser/browser_win.cc

@@ -19,8 +19,8 @@
 #include "base/files/file_path.h"
 #include "base/logging.h"
 #include "base/path_service.h"
+#include "base/strings/strcat_win.h"
 #include "base/strings/string_util.h"
-#include "base/strings/stringprintf.h"
 #include "base/strings/utf_string_conversions.h"
 #include "base/win/registry.h"
 #include "base/win/win_util.h"
@@ -68,13 +68,13 @@ bool GetProtocolLaunchPath(gin::Arguments* args, std::wstring* exe) {
 
   // Read in optional args arg
   std::vector<std::wstring> launch_args;
-  if (args->GetNext(&launch_args) && !launch_args.empty())
-    *exe = base::UTF8ToWide(
-        base::StringPrintf("\"%ls\" \"%ls\" \"%%1\"", exe->c_str(),
-                           base::JoinString(launch_args, L"\"  \"").c_str()));
-  else
-    *exe =
-        base::UTF8ToWide(base::StringPrintf("\"%ls\" \"%%1\"", exe->c_str()));
+  if (args->GetNext(&launch_args) && !launch_args.empty()) {
+    std::wstring joined_args = base::JoinString(launch_args, L"\" \"");
+    *exe = base::StrCat({L"\"", *exe, L"\" \"", joined_args, L"\" \"%1\""});
+  } else {
+    *exe = base::StrCat({L"\"", *exe, L"\" \"%1\""});
+  }
+
   return true;
 }
 
@@ -142,8 +142,7 @@ bool FormatCommandLineString(std::wstring* exe,
 
   if (!launch_args.empty()) {
     std::u16string joined_launch_args = base::JoinString(launch_args, u" ");
-    *exe = base::UTF8ToWide(base::StringPrintf(
-        "%ls %ls", exe->c_str(), base::as_wcstr(joined_launch_args)));
+    *exe = base::StrCat({*exe, L" ", base::AsWStringPiece(joined_launch_args)});
   }
 
   return true;

+ 4 - 3
shell/browser/relauncher_win.cc

@@ -8,7 +8,8 @@
 
 #include "base/logging.h"
 #include "base/process/launch.h"
-#include "base/strings/stringprintf.h"
+#include "base/strings/strcat_win.h"
+#include "base/strings/string_number_conversions_win.h"
 #include "base/strings/utf_string_conversions.h"
 #include "base/win/scoped_handle.h"
 #include "sandbox/win/src/nt_internals.h"
@@ -109,8 +110,8 @@ StringType AddQuoteForArg(const StringType& arg) {
 }  // namespace
 
 StringType GetWaitEventName(base::ProcessId pid) {
-  return base::UTF8ToWide(
-      base::StringPrintf("%ls-%d", kWaitEventName, static_cast<int>(pid)));
+  return base::StrCat(
+      {kWaitEventName, L"-", base::NumberToWString(static_cast<int>(pid))});
 }
 
 StringType ArgvToCommandLineString(const StringVector& argv) {