Unverified Commit 98576cef authored by Tomasz Gucio's avatar Tomasz Gucio Committed by GitHub

Avoid null terminating characters in strings from Utf8FromUtf16() (#109729)

---------
Co-authored-by: 's avatarschectman <schectman@google.com>
parent 7bacc25e
...@@ -51,16 +51,16 @@ std::string Utf8FromUtf16(const wchar_t* utf16_string) { ...@@ -51,16 +51,16 @@ std::string Utf8FromUtf16(const wchar_t* utf16_string) {
} }
int target_length = ::WideCharToMultiByte( int target_length = ::WideCharToMultiByte(
CP_UTF8, WC_ERR_INVALID_CHARS, utf16_string, CP_UTF8, WC_ERR_INVALID_CHARS, utf16_string,
-1, nullptr, 0, nullptr, nullptr); -1, nullptr, 0, nullptr, nullptr)
-1; // remove the trailing null character
std::string utf8_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; return utf8_string;
} }
utf8_string.resize(target_length); utf8_string.resize(target_length);
int converted_length = ::WideCharToMultiByte( int converted_length = ::WideCharToMultiByte(
CP_UTF8, WC_ERR_INVALID_CHARS, utf16_string, CP_UTF8, WC_ERR_INVALID_CHARS, utf16_string,
-1, utf8_string.data(), -1, utf8_string.data(), target_length, nullptr, nullptr);
target_length, nullptr, nullptr);
if (converted_length == 0) { if (converted_length == 0) {
return std::string(); return std::string();
} }
......
...@@ -51,16 +51,17 @@ std::string Utf8FromUtf16(const wchar_t* utf16_string) { ...@@ -51,16 +51,17 @@ std::string Utf8FromUtf16(const wchar_t* utf16_string) {
} }
int target_length = ::WideCharToMultiByte( int target_length = ::WideCharToMultiByte(
CP_UTF8, WC_ERR_INVALID_CHARS, utf16_string, CP_UTF8, WC_ERR_INVALID_CHARS, utf16_string,
-1, nullptr, 0, nullptr, nullptr); -1, nullptr, 0, nullptr, nullptr)
-1; // remove the trailing null character
int input_length = (int)wcslen(utf16_string);
std::string utf8_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; return utf8_string;
} }
utf8_string.resize(target_length); utf8_string.resize(target_length);
int converted_length = ::WideCharToMultiByte( int converted_length = ::WideCharToMultiByte(
CP_UTF8, WC_ERR_INVALID_CHARS, utf16_string, CP_UTF8, WC_ERR_INVALID_CHARS, utf16_string,
-1, utf8_string.data(), input_length, utf8_string.data(), target_length, nullptr, nullptr);
target_length, nullptr, nullptr);
if (converted_length == 0) { if (converted_length == 0) {
return std::string(); return std::string();
} }
......
...@@ -51,16 +51,17 @@ std::string Utf8FromUtf16(const wchar_t* utf16_string) { ...@@ -51,16 +51,17 @@ std::string Utf8FromUtf16(const wchar_t* utf16_string) {
} }
int target_length = ::WideCharToMultiByte( int target_length = ::WideCharToMultiByte(
CP_UTF8, WC_ERR_INVALID_CHARS, utf16_string, CP_UTF8, WC_ERR_INVALID_CHARS, utf16_string,
-1, nullptr, 0, nullptr, nullptr); -1, nullptr, 0, nullptr, nullptr)
-1; // remove the trailing null character
int input_length = (int)wcslen(utf16_string);
std::string utf8_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; return utf8_string;
} }
utf8_string.resize(target_length); utf8_string.resize(target_length);
int converted_length = ::WideCharToMultiByte( int converted_length = ::WideCharToMultiByte(
CP_UTF8, WC_ERR_INVALID_CHARS, utf16_string, CP_UTF8, WC_ERR_INVALID_CHARS, utf16_string,
-1, utf8_string.data(), input_length, utf8_string.data(), target_length, nullptr, nullptr);
target_length, nullptr, nullptr);
if (converted_length == 0) { if (converted_length == 0) {
return std::string(); return std::string();
} }
......
...@@ -51,7 +51,7 @@ void main() async { ...@@ -51,7 +51,7 @@ void main() async {
// which will use the UTF16 to UTF8 utility function to convert them to a // which will use the UTF16 to UTF8 utility function to convert them to a
// std::string, which should equate to the original expected string. // std::string, which should equate to the original expected string.
// TODO(schectman): Remove trailing null from returned string // TODO(schectman): Remove trailing null from returned string
const String expected = 'ABCℵ\x00'; const String expected = 'ABCℵ';
final Int32List codePoints = Int32List.fromList(expected.codeUnits); final Int32List codePoints = Int32List.fromList(expected.codeUnits);
final String converted = await testStringConversion(codePoints); final String converted = await testStringConversion(codePoints);
return (converted == expected) return (converted == expected)
......
...@@ -114,6 +114,7 @@ bool FlutterWindow::OnCreate() { ...@@ -114,6 +114,7 @@ bool FlutterWindow::OnCreate() {
for (int32_t code_point : code_points) { for (int32_t code_point : code_points) {
wide_str.push_back((wchar_t)(code_point)); wide_str.push_back((wchar_t)(code_point));
} }
wide_str.push_back((wchar_t)0);
const std::string string = Utf8FromUtf16(wide_str.data()); const std::string string = Utf8FromUtf16(wide_str.data());
result->Success(string); result->Success(string);
} else { } else {
......
...@@ -51,16 +51,17 @@ std::string Utf8FromUtf16(const wchar_t* utf16_string) { ...@@ -51,16 +51,17 @@ std::string Utf8FromUtf16(const wchar_t* utf16_string) {
} }
int target_length = ::WideCharToMultiByte( int target_length = ::WideCharToMultiByte(
CP_UTF8, WC_ERR_INVALID_CHARS, utf16_string, CP_UTF8, WC_ERR_INVALID_CHARS, utf16_string,
-1, nullptr, 0, nullptr, nullptr); -1, nullptr, 0, nullptr, nullptr)
-1; // remove the trailing null character
int input_length = (int)wcslen(utf16_string);
std::string utf8_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; return utf8_string;
} }
utf8_string.resize(target_length); utf8_string.resize(target_length);
int converted_length = ::WideCharToMultiByte( int converted_length = ::WideCharToMultiByte(
CP_UTF8, WC_ERR_INVALID_CHARS, utf16_string, CP_UTF8, WC_ERR_INVALID_CHARS, utf16_string,
-1, utf8_string.data(), input_length, utf8_string.data(), target_length, nullptr, nullptr);
target_length, nullptr, nullptr);
if (converted_length == 0) { if (converted_length == 0) {
return std::string(); return std::string();
} }
......
...@@ -51,16 +51,16 @@ std::string Utf8FromUtf16(const wchar_t* utf16_string) { ...@@ -51,16 +51,16 @@ std::string Utf8FromUtf16(const wchar_t* utf16_string) {
} }
int target_length = ::WideCharToMultiByte( int target_length = ::WideCharToMultiByte(
CP_UTF8, WC_ERR_INVALID_CHARS, utf16_string, CP_UTF8, WC_ERR_INVALID_CHARS, utf16_string,
-1, nullptr, 0, nullptr, nullptr); -1, nullptr, 0, nullptr, nullptr)
-1; // remove the trailing null character
std::string utf8_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; return utf8_string;
} }
utf8_string.resize(target_length); utf8_string.resize(target_length);
int converted_length = ::WideCharToMultiByte( int converted_length = ::WideCharToMultiByte(
CP_UTF8, WC_ERR_INVALID_CHARS, utf16_string, CP_UTF8, WC_ERR_INVALID_CHARS, utf16_string,
-1, utf8_string.data(), -1, utf8_string.data(), target_length, nullptr, nullptr);
target_length, nullptr, nullptr);
if (converted_length == 0) { if (converted_length == 0) {
return std::string(); return std::string();
} }
......
...@@ -51,16 +51,16 @@ std::string Utf8FromUtf16(const wchar_t* utf16_string) { ...@@ -51,16 +51,16 @@ std::string Utf8FromUtf16(const wchar_t* utf16_string) {
} }
int target_length = ::WideCharToMultiByte( int target_length = ::WideCharToMultiByte(
CP_UTF8, WC_ERR_INVALID_CHARS, utf16_string, CP_UTF8, WC_ERR_INVALID_CHARS, utf16_string,
-1, nullptr, 0, nullptr, nullptr); -1, nullptr, 0, nullptr, nullptr)
-1; // remove the trailing null character
std::string utf8_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; return utf8_string;
} }
utf8_string.resize(target_length); utf8_string.resize(target_length);
int converted_length = ::WideCharToMultiByte( int converted_length = ::WideCharToMultiByte(
CP_UTF8, WC_ERR_INVALID_CHARS, utf16_string, CP_UTF8, WC_ERR_INVALID_CHARS, utf16_string,
-1, utf8_string.data(), -1, utf8_string.data(), target_length, nullptr, nullptr);
target_length, nullptr, nullptr);
if (converted_length == 0) { if (converted_length == 0) {
return std::string(); return std::string();
} }
......
...@@ -51,16 +51,16 @@ std::string Utf8FromUtf16(const wchar_t* utf16_string) { ...@@ -51,16 +51,16 @@ std::string Utf8FromUtf16(const wchar_t* utf16_string) {
} }
int target_length = ::WideCharToMultiByte( int target_length = ::WideCharToMultiByte(
CP_UTF8, WC_ERR_INVALID_CHARS, utf16_string, CP_UTF8, WC_ERR_INVALID_CHARS, utf16_string,
-1, nullptr, 0, nullptr, nullptr); -1, nullptr, 0, nullptr, nullptr)
-1; // remove the trailing null character
std::string utf8_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; return utf8_string;
} }
utf8_string.resize(target_length); utf8_string.resize(target_length);
int converted_length = ::WideCharToMultiByte( int converted_length = ::WideCharToMultiByte(
CP_UTF8, WC_ERR_INVALID_CHARS, utf16_string, CP_UTF8, WC_ERR_INVALID_CHARS, utf16_string,
-1, utf8_string.data(), -1, utf8_string.data(), target_length, nullptr, nullptr);
target_length, nullptr, nullptr);
if (converted_length == 0) { if (converted_length == 0) {
return std::string(); return std::string();
} }
......
...@@ -51,16 +51,16 @@ std::string Utf8FromUtf16(const wchar_t* utf16_string) { ...@@ -51,16 +51,16 @@ std::string Utf8FromUtf16(const wchar_t* utf16_string) {
} }
int target_length = ::WideCharToMultiByte( int target_length = ::WideCharToMultiByte(
CP_UTF8, WC_ERR_INVALID_CHARS, utf16_string, CP_UTF8, WC_ERR_INVALID_CHARS, utf16_string,
-1, nullptr, 0, nullptr, nullptr); -1, nullptr, 0, nullptr, nullptr)
-1; // remove the trailing null character
std::string utf8_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; return utf8_string;
} }
utf8_string.resize(target_length); utf8_string.resize(target_length);
int converted_length = ::WideCharToMultiByte( int converted_length = ::WideCharToMultiByte(
CP_UTF8, WC_ERR_INVALID_CHARS, utf16_string, CP_UTF8, WC_ERR_INVALID_CHARS, utf16_string,
-1, utf8_string.data(), -1, utf8_string.data(), target_length, nullptr, nullptr);
target_length, nullptr, nullptr);
if (converted_length == 0) { if (converted_length == 0) {
return std::string(); return std::string();
} }
......
...@@ -49,17 +49,18 @@ std::string Utf8FromUtf16(const wchar_t* utf16_string) { ...@@ -49,17 +49,18 @@ std::string Utf8FromUtf16(const wchar_t* utf16_string) {
if (utf16_string == nullptr) { if (utf16_string == nullptr) {
return std::string(); return std::string();
} }
int target_length = int target_length = ::WideCharToMultiByte(
::WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, utf16_string, -1, CP_UTF8, WC_ERR_INVALID_CHARS, utf16_string,
nullptr, 0, nullptr, nullptr); -1, nullptr, 0, nullptr, nullptr)
-1; // remove the trailing null character
std::string utf8_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; return utf8_string;
} }
utf8_string.resize(target_length); utf8_string.resize(target_length);
int converted_length = ::WideCharToMultiByte( int converted_length = ::WideCharToMultiByte(
CP_UTF8, WC_ERR_INVALID_CHARS, utf16_string, -1, utf8_string.data(), CP_UTF8, WC_ERR_INVALID_CHARS, utf16_string,
target_length, nullptr, nullptr); -1, utf8_string.data(), target_length, nullptr, nullptr);
if (converted_length == 0) { if (converted_length == 0) {
return std::string(); return std::string();
} }
......
...@@ -51,16 +51,16 @@ std::string Utf8FromUtf16(const wchar_t* utf16_string) { ...@@ -51,16 +51,16 @@ std::string Utf8FromUtf16(const wchar_t* utf16_string) {
} }
int target_length = ::WideCharToMultiByte( int target_length = ::WideCharToMultiByte(
CP_UTF8, WC_ERR_INVALID_CHARS, utf16_string, CP_UTF8, WC_ERR_INVALID_CHARS, utf16_string,
-1, nullptr, 0, nullptr, nullptr); -1, nullptr, 0, nullptr, nullptr)
-1; // remove the trailing null character
std::string utf8_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; return utf8_string;
} }
utf8_string.resize(target_length); utf8_string.resize(target_length);
int converted_length = ::WideCharToMultiByte( int converted_length = ::WideCharToMultiByte(
CP_UTF8, WC_ERR_INVALID_CHARS, utf16_string, CP_UTF8, WC_ERR_INVALID_CHARS, utf16_string,
-1, utf8_string.data(), -1, utf8_string.data(), target_length, nullptr, nullptr);
target_length, nullptr, nullptr);
if (converted_length == 0) { if (converted_length == 0) {
return std::string(); return std::string();
} }
......
...@@ -47,16 +47,17 @@ std::string Utf8FromUtf16(const wchar_t* utf16_string) { ...@@ -47,16 +47,17 @@ std::string Utf8FromUtf16(const wchar_t* utf16_string) {
} }
int target_length = ::WideCharToMultiByte( int target_length = ::WideCharToMultiByte(
CP_UTF8, WC_ERR_INVALID_CHARS, utf16_string, CP_UTF8, WC_ERR_INVALID_CHARS, utf16_string,
-1, nullptr, 0, nullptr, nullptr); -1, nullptr, 0, nullptr, nullptr)
-1; // remove the trailing null character
int input_length = (int)wcslen(utf16_string);
std::string utf8_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; return utf8_string;
} }
utf8_string.resize(target_length); utf8_string.resize(target_length);
int converted_length = ::WideCharToMultiByte( int converted_length = ::WideCharToMultiByte(
CP_UTF8, WC_ERR_INVALID_CHARS, utf16_string, CP_UTF8, WC_ERR_INVALID_CHARS, utf16_string,
-1, utf8_string.data(), input_length, utf8_string.data(), target_length, nullptr, nullptr);
target_length, nullptr, nullptr);
if (converted_length == 0) { if (converted_length == 0) {
return std::string(); return std::string();
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment