From 37630a61282af710a1efc4828e7c2e907485a104 Mon Sep 17 00:00:00 2001 From: Calvin Date: Sun, 7 Jan 2024 22:02:20 -0800 Subject: [PATCH] fix: wide string concatenation (#40892) * fix: wide string concatenation * Use wstring_views to keep length in context * forgot a space, oopsies --- shell/browser/browser_win.cc | 19 +++++++++---------- shell/browser/relauncher_win.cc | 7 ++++--- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/shell/browser/browser_win.cc b/shell/browser/browser_win.cc index 8595a5e575ce..a585882eaca1 100644 --- a/shell/browser/browser_win.cc +++ b/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 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::AsWStringView(joined_launch_args)}); } return true; diff --git a/shell/browser/relauncher_win.cc b/shell/browser/relauncher_win.cc index 2e7b0150ad0b..f6f7a6526f27 100644 --- a/shell/browser/relauncher_win.cc +++ b/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(pid))); + return base::StrCat( + {kWaitEventName, L"-", base::NumberToWString(static_cast(pid))}); } StringType ArgvToCommandLineString(const StringVector& argv) {