From b5d3c43280bba8bc5f57ef63798a577b97558be0 Mon Sep 17 00:00:00 2001 From: Chris Bracken Date: Mon, 7 Mar 2022 19:57:36 -0800 Subject: [PATCH] Check string size before Win32 MultiByte <-> WideChar conversions (#99729) This patch adds an additional check to ensure the target length of a string is within the supported maximum string length prior to calling WideCharToMultiByte/MultiByteToWideChar in the Windows runner template. This is to prevent resize() from failing if called with a count > std::string::max_size(). According to Win32 API docs (WideCharToMultiByte, MultiByteToWideChar) it's the caller responsibility to make sure the buffers are correctly allocated. Authored by: Tomasz Gucio --- .../flutter_gallery/windows/runner/utils.cpp | 6 +++--- dev/manual_tests/windows/runner/utils.cpp | 6 +++--- examples/api/windows/runner/utils.cpp | 6 +++--- .../templates/app_shared/windows.tmpl/runner/utils.cpp | 6 +++--- 4 files changed, 12 insertions(+), 12 deletions(-) diff --git a/dev/integration_tests/flutter_gallery/windows/runner/utils.cpp b/dev/integration_tests/flutter_gallery/windows/runner/utils.cpp index d19bdbbcc3..f5bf9fa0f5 100644 --- a/dev/integration_tests/flutter_gallery/windows/runner/utils.cpp +++ b/dev/integration_tests/flutter_gallery/windows/runner/utils.cpp @@ -48,10 +48,10 @@ std::string Utf8FromUtf16(const wchar_t* utf16_string) { int target_length = ::WideCharToMultiByte( CP_UTF8, WC_ERR_INVALID_CHARS, utf16_string, -1, nullptr, 0, nullptr, nullptr); - if (target_length == 0) { - return std::string(); - } std::string utf8_string; + if (target_length == 0 || target_length > utf8_string.max_size()) { + return utf8_string; + } utf8_string.resize(target_length); int converted_length = ::WideCharToMultiByte( CP_UTF8, WC_ERR_INVALID_CHARS, utf16_string, diff --git a/dev/manual_tests/windows/runner/utils.cpp b/dev/manual_tests/windows/runner/utils.cpp index d19bdbbcc3..f5bf9fa0f5 100644 --- a/dev/manual_tests/windows/runner/utils.cpp +++ b/dev/manual_tests/windows/runner/utils.cpp @@ -48,10 +48,10 @@ std::string Utf8FromUtf16(const wchar_t* utf16_string) { int target_length = ::WideCharToMultiByte( CP_UTF8, WC_ERR_INVALID_CHARS, utf16_string, -1, nullptr, 0, nullptr, nullptr); - if (target_length == 0) { - return std::string(); - } std::string utf8_string; + if (target_length == 0 || target_length > utf8_string.max_size()) { + return utf8_string; + } utf8_string.resize(target_length); int converted_length = ::WideCharToMultiByte( CP_UTF8, WC_ERR_INVALID_CHARS, utf16_string, diff --git a/examples/api/windows/runner/utils.cpp b/examples/api/windows/runner/utils.cpp index d19bdbbcc3..f5bf9fa0f5 100644 --- a/examples/api/windows/runner/utils.cpp +++ b/examples/api/windows/runner/utils.cpp @@ -48,10 +48,10 @@ std::string Utf8FromUtf16(const wchar_t* utf16_string) { int target_length = ::WideCharToMultiByte( CP_UTF8, WC_ERR_INVALID_CHARS, utf16_string, -1, nullptr, 0, nullptr, nullptr); - if (target_length == 0) { - return std::string(); - } std::string utf8_string; + if (target_length == 0 || target_length > utf8_string.max_size()) { + return utf8_string; + } utf8_string.resize(target_length); int converted_length = ::WideCharToMultiByte( CP_UTF8, WC_ERR_INVALID_CHARS, utf16_string, diff --git a/packages/flutter_tools/templates/app_shared/windows.tmpl/runner/utils.cpp b/packages/flutter_tools/templates/app_shared/windows.tmpl/runner/utils.cpp index d19bdbbcc3..f5bf9fa0f5 100644 --- a/packages/flutter_tools/templates/app_shared/windows.tmpl/runner/utils.cpp +++ b/packages/flutter_tools/templates/app_shared/windows.tmpl/runner/utils.cpp @@ -48,10 +48,10 @@ std::string Utf8FromUtf16(const wchar_t* utf16_string) { int target_length = ::WideCharToMultiByte( CP_UTF8, WC_ERR_INVALID_CHARS, utf16_string, -1, nullptr, 0, nullptr, nullptr); - if (target_length == 0) { - return std::string(); - } std::string utf8_string; + if (target_length == 0 || target_length > utf8_string.max_size()) { + return utf8_string; + } utf8_string.resize(target_length); int converted_length = ::WideCharToMultiByte( CP_UTF8, WC_ERR_INVALID_CHARS, utf16_string,