From 9f8fe3f04c468f7a503b58f29067f7392dc94b95 Mon Sep 17 00:00:00 2001 From: Chris Bracken Date: Wed, 7 Feb 2024 00:09:57 +0000 Subject: [PATCH] [Windows] Fix signed/unsigned int comparison (#142341) Previously, we were comparing the signed int `target_length` (returned by WideCharToMultiByte) to a size_t string length, resulting in a signed/unsigned comparison warning as follows: ``` windows\runner\utils.cpp(54,43): warning C4018: '>': signed/unsigned mismatch ``` WideCharToMultiByte returns: * 0 on error * the number of bytes written to the buffer pointed to by its fifth parameter, lpMultiByteStr, on success. As a result it's safe to store the return value in an unsigned int, which eliminates the warning. No changes to tests since this is dependent on end-user project settings/modifications and does not trigger a warning with default project settings. Fixes: https://github.com/flutter/flutter/issues/134227 --- dev/a11y_assessments/windows/runner/utils.cpp | 4 ++-- dev/benchmarks/complex_layout/windows/runner/utils.cpp | 4 ++-- .../flutter_gallery/windows/runner/utils.cpp | 4 ++-- dev/integration_tests/ui/windows/runner/utils.cpp | 4 ++-- .../windows_startup_test/windows/runner/utils.cpp | 4 ++-- dev/manual_tests/windows/runner/utils.cpp | 4 ++-- examples/api/windows/runner/utils.cpp | 4 ++-- examples/flutter_view/windows/runner/utils.cpp | 4 ++-- examples/hello_world/windows/runner/utils.cpp | 4 ++-- examples/layers/windows/runner/utils.cpp | 4 ++-- examples/platform_channel/windows/runner/utils.cpp | 4 ++-- examples/platform_view/windows/runner/utils.cpp | 4 ++-- .../templates/app_shared/windows.tmpl/runner/utils.cpp | 4 ++-- 13 files changed, 26 insertions(+), 26 deletions(-) diff --git a/dev/a11y_assessments/windows/runner/utils.cpp b/dev/a11y_assessments/windows/runner/utils.cpp index f677d8d5f7..02f4362740 100644 --- a/dev/a11y_assessments/windows/runner/utils.cpp +++ b/dev/a11y_assessments/windows/runner/utils.cpp @@ -49,13 +49,13 @@ std::string Utf8FromUtf16(const wchar_t* utf16_string) { if (utf16_string == nullptr) { return std::string(); } - int target_length = ::WideCharToMultiByte( + unsigned int target_length = ::WideCharToMultiByte( CP_UTF8, WC_ERR_INVALID_CHARS, utf16_string, -1, nullptr, 0, nullptr, nullptr) -1; // remove the trailing null character int input_length = (int)wcslen(utf16_string); std::string utf8_string; - if (target_length <= 0 || target_length > utf8_string.max_size()) { + if (target_length == 0 || target_length > utf8_string.max_size()) { return utf8_string; } utf8_string.resize(target_length); diff --git a/dev/benchmarks/complex_layout/windows/runner/utils.cpp b/dev/benchmarks/complex_layout/windows/runner/utils.cpp index b849ce2c32..6abcd65042 100644 --- a/dev/benchmarks/complex_layout/windows/runner/utils.cpp +++ b/dev/benchmarks/complex_layout/windows/runner/utils.cpp @@ -49,12 +49,12 @@ std::string Utf8FromUtf16(const wchar_t* utf16_string) { if (utf16_string == nullptr) { return std::string(); } - int target_length = ::WideCharToMultiByte( + unsigned int target_length = ::WideCharToMultiByte( CP_UTF8, WC_ERR_INVALID_CHARS, utf16_string, -1, nullptr, 0, nullptr, nullptr) -1; // remove the trailing null character std::string utf8_string; - if (target_length <= 0 || target_length > utf8_string.max_size()) { + if (target_length == 0 || target_length > utf8_string.max_size()) { return utf8_string; } utf8_string.resize(target_length); diff --git a/dev/integration_tests/flutter_gallery/windows/runner/utils.cpp b/dev/integration_tests/flutter_gallery/windows/runner/utils.cpp index f677d8d5f7..02f4362740 100644 --- a/dev/integration_tests/flutter_gallery/windows/runner/utils.cpp +++ b/dev/integration_tests/flutter_gallery/windows/runner/utils.cpp @@ -49,13 +49,13 @@ std::string Utf8FromUtf16(const wchar_t* utf16_string) { if (utf16_string == nullptr) { return std::string(); } - int target_length = ::WideCharToMultiByte( + unsigned int target_length = ::WideCharToMultiByte( CP_UTF8, WC_ERR_INVALID_CHARS, utf16_string, -1, nullptr, 0, nullptr, nullptr) -1; // remove the trailing null character int input_length = (int)wcslen(utf16_string); std::string utf8_string; - if (target_length <= 0 || target_length > utf8_string.max_size()) { + if (target_length == 0 || target_length > utf8_string.max_size()) { return utf8_string; } utf8_string.resize(target_length); diff --git a/dev/integration_tests/ui/windows/runner/utils.cpp b/dev/integration_tests/ui/windows/runner/utils.cpp index f677d8d5f7..02f4362740 100644 --- a/dev/integration_tests/ui/windows/runner/utils.cpp +++ b/dev/integration_tests/ui/windows/runner/utils.cpp @@ -49,13 +49,13 @@ std::string Utf8FromUtf16(const wchar_t* utf16_string) { if (utf16_string == nullptr) { return std::string(); } - int target_length = ::WideCharToMultiByte( + unsigned int target_length = ::WideCharToMultiByte( CP_UTF8, WC_ERR_INVALID_CHARS, utf16_string, -1, nullptr, 0, nullptr, nullptr) -1; // remove the trailing null character int input_length = (int)wcslen(utf16_string); std::string utf8_string; - if (target_length <= 0 || target_length > utf8_string.max_size()) { + if (target_length == 0 || target_length > utf8_string.max_size()) { return utf8_string; } utf8_string.resize(target_length); diff --git a/dev/integration_tests/windows_startup_test/windows/runner/utils.cpp b/dev/integration_tests/windows_startup_test/windows/runner/utils.cpp index f677d8d5f7..02f4362740 100644 --- a/dev/integration_tests/windows_startup_test/windows/runner/utils.cpp +++ b/dev/integration_tests/windows_startup_test/windows/runner/utils.cpp @@ -49,13 +49,13 @@ std::string Utf8FromUtf16(const wchar_t* utf16_string) { if (utf16_string == nullptr) { return std::string(); } - int target_length = ::WideCharToMultiByte( + unsigned int target_length = ::WideCharToMultiByte( CP_UTF8, WC_ERR_INVALID_CHARS, utf16_string, -1, nullptr, 0, nullptr, nullptr) -1; // remove the trailing null character int input_length = (int)wcslen(utf16_string); std::string utf8_string; - if (target_length <= 0 || target_length > utf8_string.max_size()) { + if (target_length == 0 || target_length > utf8_string.max_size()) { return utf8_string; } utf8_string.resize(target_length); diff --git a/dev/manual_tests/windows/runner/utils.cpp b/dev/manual_tests/windows/runner/utils.cpp index b849ce2c32..6abcd65042 100644 --- a/dev/manual_tests/windows/runner/utils.cpp +++ b/dev/manual_tests/windows/runner/utils.cpp @@ -49,12 +49,12 @@ std::string Utf8FromUtf16(const wchar_t* utf16_string) { if (utf16_string == nullptr) { return std::string(); } - int target_length = ::WideCharToMultiByte( + unsigned int target_length = ::WideCharToMultiByte( CP_UTF8, WC_ERR_INVALID_CHARS, utf16_string, -1, nullptr, 0, nullptr, nullptr) -1; // remove the trailing null character std::string utf8_string; - if (target_length <= 0 || target_length > utf8_string.max_size()) { + if (target_length == 0 || target_length > utf8_string.max_size()) { return utf8_string; } utf8_string.resize(target_length); diff --git a/examples/api/windows/runner/utils.cpp b/examples/api/windows/runner/utils.cpp index b849ce2c32..6abcd65042 100644 --- a/examples/api/windows/runner/utils.cpp +++ b/examples/api/windows/runner/utils.cpp @@ -49,12 +49,12 @@ std::string Utf8FromUtf16(const wchar_t* utf16_string) { if (utf16_string == nullptr) { return std::string(); } - int target_length = ::WideCharToMultiByte( + unsigned int target_length = ::WideCharToMultiByte( CP_UTF8, WC_ERR_INVALID_CHARS, utf16_string, -1, nullptr, 0, nullptr, nullptr) -1; // remove the trailing null character std::string utf8_string; - if (target_length <= 0 || target_length > utf8_string.max_size()) { + if (target_length == 0 || target_length > utf8_string.max_size()) { return utf8_string; } utf8_string.resize(target_length); diff --git a/examples/flutter_view/windows/runner/utils.cpp b/examples/flutter_view/windows/runner/utils.cpp index b849ce2c32..6abcd65042 100644 --- a/examples/flutter_view/windows/runner/utils.cpp +++ b/examples/flutter_view/windows/runner/utils.cpp @@ -49,12 +49,12 @@ std::string Utf8FromUtf16(const wchar_t* utf16_string) { if (utf16_string == nullptr) { return std::string(); } - int target_length = ::WideCharToMultiByte( + unsigned int target_length = ::WideCharToMultiByte( CP_UTF8, WC_ERR_INVALID_CHARS, utf16_string, -1, nullptr, 0, nullptr, nullptr) -1; // remove the trailing null character std::string utf8_string; - if (target_length <= 0 || target_length > utf8_string.max_size()) { + if (target_length == 0 || target_length > utf8_string.max_size()) { return utf8_string; } utf8_string.resize(target_length); diff --git a/examples/hello_world/windows/runner/utils.cpp b/examples/hello_world/windows/runner/utils.cpp index b849ce2c32..6abcd65042 100644 --- a/examples/hello_world/windows/runner/utils.cpp +++ b/examples/hello_world/windows/runner/utils.cpp @@ -49,12 +49,12 @@ std::string Utf8FromUtf16(const wchar_t* utf16_string) { if (utf16_string == nullptr) { return std::string(); } - int target_length = ::WideCharToMultiByte( + unsigned int target_length = ::WideCharToMultiByte( CP_UTF8, WC_ERR_INVALID_CHARS, utf16_string, -1, nullptr, 0, nullptr, nullptr) -1; // remove the trailing null character std::string utf8_string; - if (target_length <= 0 || target_length > utf8_string.max_size()) { + if (target_length == 0 || target_length > utf8_string.max_size()) { return utf8_string; } utf8_string.resize(target_length); diff --git a/examples/layers/windows/runner/utils.cpp b/examples/layers/windows/runner/utils.cpp index f677d8d5f7..02f4362740 100644 --- a/examples/layers/windows/runner/utils.cpp +++ b/examples/layers/windows/runner/utils.cpp @@ -49,13 +49,13 @@ std::string Utf8FromUtf16(const wchar_t* utf16_string) { if (utf16_string == nullptr) { return std::string(); } - int target_length = ::WideCharToMultiByte( + unsigned int target_length = ::WideCharToMultiByte( CP_UTF8, WC_ERR_INVALID_CHARS, utf16_string, -1, nullptr, 0, nullptr, nullptr) -1; // remove the trailing null character int input_length = (int)wcslen(utf16_string); std::string utf8_string; - if (target_length <= 0 || target_length > utf8_string.max_size()) { + if (target_length == 0 || target_length > utf8_string.max_size()) { return utf8_string; } utf8_string.resize(target_length); diff --git a/examples/platform_channel/windows/runner/utils.cpp b/examples/platform_channel/windows/runner/utils.cpp index a05ed58ca9..d9ca4c32c9 100644 --- a/examples/platform_channel/windows/runner/utils.cpp +++ b/examples/platform_channel/windows/runner/utils.cpp @@ -49,12 +49,12 @@ std::string Utf8FromUtf16(const wchar_t* utf16_string) { if (utf16_string == nullptr) { return std::string(); } - int target_length = ::WideCharToMultiByte( + unsigned int target_length = ::WideCharToMultiByte( CP_UTF8, WC_ERR_INVALID_CHARS, utf16_string, -1, nullptr, 0, nullptr, nullptr) -1; // remove the trailing null character std::string utf8_string; - if (target_length <= 0 || target_length > utf8_string.max_size()) { + if (target_length == 0 || target_length > utf8_string.max_size()) { return utf8_string; } utf8_string.resize(target_length); diff --git a/examples/platform_view/windows/runner/utils.cpp b/examples/platform_view/windows/runner/utils.cpp index b849ce2c32..6abcd65042 100644 --- a/examples/platform_view/windows/runner/utils.cpp +++ b/examples/platform_view/windows/runner/utils.cpp @@ -49,12 +49,12 @@ std::string Utf8FromUtf16(const wchar_t* utf16_string) { if (utf16_string == nullptr) { return std::string(); } - int target_length = ::WideCharToMultiByte( + unsigned int target_length = ::WideCharToMultiByte( CP_UTF8, WC_ERR_INVALID_CHARS, utf16_string, -1, nullptr, 0, nullptr, nullptr) -1; // remove the trailing null character std::string utf8_string; - if (target_length <= 0 || target_length > utf8_string.max_size()) { + if (target_length == 0 || target_length > utf8_string.max_size()) { return utf8_string; } utf8_string.resize(target_length); 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 b2b08734db..3a0b46511a 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 @@ -45,13 +45,13 @@ std::string Utf8FromUtf16(const wchar_t* utf16_string) { if (utf16_string == nullptr) { return std::string(); } - int target_length = ::WideCharToMultiByte( + unsigned int target_length = ::WideCharToMultiByte( CP_UTF8, WC_ERR_INVALID_CHARS, utf16_string, -1, nullptr, 0, nullptr, nullptr) -1; // remove the trailing null character int input_length = (int)wcslen(utf16_string); std::string utf8_string; - if (target_length <= 0 || target_length > utf8_string.max_size()) { + if (target_length == 0 || target_length > utf8_string.max_size()) { return utf8_string; } utf8_string.resize(target_length);