Skip to content

Commit 98576ce

Browse files
Avoid null terminating characters in strings from Utf8FromUtf16() (#109729)
--------- Co-authored-by: schectman <[email protected]>
1 parent 7bacc25 commit 98576ce

File tree

13 files changed

+53
-47
lines changed

13 files changed

+53
-47
lines changed

dev/benchmarks/complex_layout/windows/runner/utils.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,16 +51,16 @@ std::string Utf8FromUtf16(const wchar_t* utf16_string) {
5151
}
5252
int target_length = ::WideCharToMultiByte(
5353
CP_UTF8, WC_ERR_INVALID_CHARS, utf16_string,
54-
-1, nullptr, 0, nullptr, nullptr);
54+
-1, nullptr, 0, nullptr, nullptr)
55+
-1; // remove the trailing null character
5556
std::string utf8_string;
56-
if (target_length == 0 || target_length > utf8_string.max_size()) {
57+
if (target_length <= 0 || target_length > utf8_string.max_size()) {
5758
return utf8_string;
5859
}
5960
utf8_string.resize(target_length);
6061
int converted_length = ::WideCharToMultiByte(
6162
CP_UTF8, WC_ERR_INVALID_CHARS, utf16_string,
62-
-1, utf8_string.data(),
63-
target_length, nullptr, nullptr);
63+
-1, utf8_string.data(), target_length, nullptr, nullptr);
6464
if (converted_length == 0) {
6565
return std::string();
6666
}

dev/integration_tests/flutter_gallery/windows/runner/utils.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,16 +51,17 @@ std::string Utf8FromUtf16(const wchar_t* utf16_string) {
5151
}
5252
int target_length = ::WideCharToMultiByte(
5353
CP_UTF8, WC_ERR_INVALID_CHARS, utf16_string,
54-
-1, nullptr, 0, nullptr, nullptr);
54+
-1, nullptr, 0, nullptr, nullptr)
55+
-1; // remove the trailing null character
56+
int input_length = (int)wcslen(utf16_string);
5557
std::string utf8_string;
56-
if (target_length == 0 || target_length > utf8_string.max_size()) {
58+
if (target_length <= 0 || target_length > utf8_string.max_size()) {
5759
return utf8_string;
5860
}
5961
utf8_string.resize(target_length);
6062
int converted_length = ::WideCharToMultiByte(
6163
CP_UTF8, WC_ERR_INVALID_CHARS, utf16_string,
62-
-1, utf8_string.data(),
63-
target_length, nullptr, nullptr);
64+
input_length, utf8_string.data(), target_length, nullptr, nullptr);
6465
if (converted_length == 0) {
6566
return std::string();
6667
}

dev/integration_tests/ui/windows/runner/utils.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,16 +51,17 @@ std::string Utf8FromUtf16(const wchar_t* utf16_string) {
5151
}
5252
int target_length = ::WideCharToMultiByte(
5353
CP_UTF8, WC_ERR_INVALID_CHARS, utf16_string,
54-
-1, nullptr, 0, nullptr, nullptr);
54+
-1, nullptr, 0, nullptr, nullptr)
55+
-1; // remove the trailing null character
56+
int input_length = (int)wcslen(utf16_string);
5557
std::string utf8_string;
56-
if (target_length == 0 || target_length > utf8_string.max_size()) {
58+
if (target_length <= 0 || target_length > utf8_string.max_size()) {
5759
return utf8_string;
5860
}
5961
utf8_string.resize(target_length);
6062
int converted_length = ::WideCharToMultiByte(
6163
CP_UTF8, WC_ERR_INVALID_CHARS, utf16_string,
62-
-1, utf8_string.data(),
63-
target_length, nullptr, nullptr);
64+
input_length, utf8_string.data(), target_length, nullptr, nullptr);
6465
if (converted_length == 0) {
6566
return std::string();
6667
}

dev/integration_tests/windows_startup_test/lib/main.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ void main() async {
5151
// which will use the UTF16 to UTF8 utility function to convert them to a
5252
// std::string, which should equate to the original expected string.
5353
// TODO(schectman): Remove trailing null from returned string
54-
const String expected = 'ABCℵ\x00';
54+
const String expected = 'ABCℵ';
5555
final Int32List codePoints = Int32List.fromList(expected.codeUnits);
5656
final String converted = await testStringConversion(codePoints);
5757
return (converted == expected)

dev/integration_tests/windows_startup_test/windows/runner/flutter_window.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ bool FlutterWindow::OnCreate() {
114114
for (int32_t code_point : code_points) {
115115
wide_str.push_back((wchar_t)(code_point));
116116
}
117+
wide_str.push_back((wchar_t)0);
117118
const std::string string = Utf8FromUtf16(wide_str.data());
118119
result->Success(string);
119120
} else {

dev/integration_tests/windows_startup_test/windows/runner/utils.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,16 +51,17 @@ std::string Utf8FromUtf16(const wchar_t* utf16_string) {
5151
}
5252
int target_length = ::WideCharToMultiByte(
5353
CP_UTF8, WC_ERR_INVALID_CHARS, utf16_string,
54-
-1, nullptr, 0, nullptr, nullptr);
54+
-1, nullptr, 0, nullptr, nullptr)
55+
-1; // remove the trailing null character
56+
int input_length = (int)wcslen(utf16_string);
5557
std::string utf8_string;
56-
if (target_length == 0 || target_length > utf8_string.max_size()) {
58+
if (target_length <= 0 || target_length > utf8_string.max_size()) {
5759
return utf8_string;
5860
}
5961
utf8_string.resize(target_length);
6062
int converted_length = ::WideCharToMultiByte(
6163
CP_UTF8, WC_ERR_INVALID_CHARS, utf16_string,
62-
-1, utf8_string.data(),
63-
target_length, nullptr, nullptr);
64+
input_length, utf8_string.data(), target_length, nullptr, nullptr);
6465
if (converted_length == 0) {
6566
return std::string();
6667
}

dev/manual_tests/windows/runner/utils.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,16 +51,16 @@ std::string Utf8FromUtf16(const wchar_t* utf16_string) {
5151
}
5252
int target_length = ::WideCharToMultiByte(
5353
CP_UTF8, WC_ERR_INVALID_CHARS, utf16_string,
54-
-1, nullptr, 0, nullptr, nullptr);
54+
-1, nullptr, 0, nullptr, nullptr)
55+
-1; // remove the trailing null character
5556
std::string utf8_string;
56-
if (target_length == 0 || target_length > utf8_string.max_size()) {
57+
if (target_length <= 0 || target_length > utf8_string.max_size()) {
5758
return utf8_string;
5859
}
5960
utf8_string.resize(target_length);
6061
int converted_length = ::WideCharToMultiByte(
6162
CP_UTF8, WC_ERR_INVALID_CHARS, utf16_string,
62-
-1, utf8_string.data(),
63-
target_length, nullptr, nullptr);
63+
-1, utf8_string.data(), target_length, nullptr, nullptr);
6464
if (converted_length == 0) {
6565
return std::string();
6666
}

examples/api/windows/runner/utils.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,16 +51,16 @@ std::string Utf8FromUtf16(const wchar_t* utf16_string) {
5151
}
5252
int target_length = ::WideCharToMultiByte(
5353
CP_UTF8, WC_ERR_INVALID_CHARS, utf16_string,
54-
-1, nullptr, 0, nullptr, nullptr);
54+
-1, nullptr, 0, nullptr, nullptr)
55+
-1; // remove the trailing null character
5556
std::string utf8_string;
56-
if (target_length == 0 || target_length > utf8_string.max_size()) {
57+
if (target_length <= 0 || target_length > utf8_string.max_size()) {
5758
return utf8_string;
5859
}
5960
utf8_string.resize(target_length);
6061
int converted_length = ::WideCharToMultiByte(
6162
CP_UTF8, WC_ERR_INVALID_CHARS, utf16_string,
62-
-1, utf8_string.data(),
63-
target_length, nullptr, nullptr);
63+
-1, utf8_string.data(), target_length, nullptr, nullptr);
6464
if (converted_length == 0) {
6565
return std::string();
6666
}

examples/flutter_view/windows/runner/utils.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,16 +51,16 @@ std::string Utf8FromUtf16(const wchar_t* utf16_string) {
5151
}
5252
int target_length = ::WideCharToMultiByte(
5353
CP_UTF8, WC_ERR_INVALID_CHARS, utf16_string,
54-
-1, nullptr, 0, nullptr, nullptr);
54+
-1, nullptr, 0, nullptr, nullptr)
55+
-1; // remove the trailing null character
5556
std::string utf8_string;
56-
if (target_length == 0 || target_length > utf8_string.max_size()) {
57+
if (target_length <= 0 || target_length > utf8_string.max_size()) {
5758
return utf8_string;
5859
}
5960
utf8_string.resize(target_length);
6061
int converted_length = ::WideCharToMultiByte(
6162
CP_UTF8, WC_ERR_INVALID_CHARS, utf16_string,
62-
-1, utf8_string.data(),
63-
target_length, nullptr, nullptr);
63+
-1, utf8_string.data(), target_length, nullptr, nullptr);
6464
if (converted_length == 0) {
6565
return std::string();
6666
}

examples/hello_world/windows/runner/utils.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,16 +51,16 @@ std::string Utf8FromUtf16(const wchar_t* utf16_string) {
5151
}
5252
int target_length = ::WideCharToMultiByte(
5353
CP_UTF8, WC_ERR_INVALID_CHARS, utf16_string,
54-
-1, nullptr, 0, nullptr, nullptr);
54+
-1, nullptr, 0, nullptr, nullptr)
55+
-1; // remove the trailing null character
5556
std::string utf8_string;
56-
if (target_length == 0 || target_length > utf8_string.max_size()) {
57+
if (target_length <= 0 || target_length > utf8_string.max_size()) {
5758
return utf8_string;
5859
}
5960
utf8_string.resize(target_length);
6061
int converted_length = ::WideCharToMultiByte(
6162
CP_UTF8, WC_ERR_INVALID_CHARS, utf16_string,
62-
-1, utf8_string.data(),
63-
target_length, nullptr, nullptr);
63+
-1, utf8_string.data(), target_length, nullptr, nullptr);
6464
if (converted_length == 0) {
6565
return std::string();
6666
}

examples/platform_channel/windows/runner/utils.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,17 +49,18 @@ std::string Utf8FromUtf16(const wchar_t* utf16_string) {
4949
if (utf16_string == nullptr) {
5050
return std::string();
5151
}
52-
int target_length =
53-
::WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, utf16_string, -1,
54-
nullptr, 0, nullptr, nullptr);
52+
int target_length = ::WideCharToMultiByte(
53+
CP_UTF8, WC_ERR_INVALID_CHARS, utf16_string,
54+
-1, nullptr, 0, nullptr, nullptr)
55+
-1; // remove the trailing null character
5556
std::string utf8_string;
56-
if (target_length == 0 || target_length > utf8_string.max_size()) {
57+
if (target_length <= 0 || target_length > utf8_string.max_size()) {
5758
return utf8_string;
5859
}
5960
utf8_string.resize(target_length);
6061
int converted_length = ::WideCharToMultiByte(
61-
CP_UTF8, WC_ERR_INVALID_CHARS, utf16_string, -1, utf8_string.data(),
62-
target_length, nullptr, nullptr);
62+
CP_UTF8, WC_ERR_INVALID_CHARS, utf16_string,
63+
-1, utf8_string.data(), target_length, nullptr, nullptr);
6364
if (converted_length == 0) {
6465
return std::string();
6566
}

examples/platform_view/windows/runner/utils.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,16 +51,16 @@ std::string Utf8FromUtf16(const wchar_t* utf16_string) {
5151
}
5252
int target_length = ::WideCharToMultiByte(
5353
CP_UTF8, WC_ERR_INVALID_CHARS, utf16_string,
54-
-1, nullptr, 0, nullptr, nullptr);
54+
-1, nullptr, 0, nullptr, nullptr)
55+
-1; // remove the trailing null character
5556
std::string utf8_string;
56-
if (target_length == 0 || target_length > utf8_string.max_size()) {
57+
if (target_length <= 0 || target_length > utf8_string.max_size()) {
5758
return utf8_string;
5859
}
5960
utf8_string.resize(target_length);
6061
int converted_length = ::WideCharToMultiByte(
6162
CP_UTF8, WC_ERR_INVALID_CHARS, utf16_string,
62-
-1, utf8_string.data(),
63-
target_length, nullptr, nullptr);
63+
-1, utf8_string.data(), target_length, nullptr, nullptr);
6464
if (converted_length == 0) {
6565
return std::string();
6666
}

packages/flutter_tools/templates/app_shared/windows.tmpl/runner/utils.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,16 +47,17 @@ std::string Utf8FromUtf16(const wchar_t* utf16_string) {
4747
}
4848
int target_length = ::WideCharToMultiByte(
4949
CP_UTF8, WC_ERR_INVALID_CHARS, utf16_string,
50-
-1, nullptr, 0, nullptr, nullptr);
50+
-1, nullptr, 0, nullptr, nullptr)
51+
-1; // remove the trailing null character
52+
int input_length = (int)wcslen(utf16_string);
5153
std::string utf8_string;
52-
if (target_length == 0 || target_length > utf8_string.max_size()) {
54+
if (target_length <= 0 || target_length > utf8_string.max_size()) {
5355
return utf8_string;
5456
}
5557
utf8_string.resize(target_length);
5658
int converted_length = ::WideCharToMultiByte(
5759
CP_UTF8, WC_ERR_INVALID_CHARS, utf16_string,
58-
-1, utf8_string.data(),
59-
target_length, nullptr, nullptr);
60+
input_length, utf8_string.data(), target_length, nullptr, nullptr);
6061
if (converted_length == 0) {
6162
return std::string();
6263
}

0 commit comments

Comments
 (0)