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

---------
Co-authored-by: schectman <schectman@google.com>
This commit is contained in:
Tomasz Gucio
2023-02-13 19:22:37 +01:00
committed by GitHub
parent 7bacc25eea
commit 98576cef58
13 changed files with 53 additions and 47 deletions

View File

@@ -51,16 +51,16 @@ 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);
-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);
int converted_length = ::WideCharToMultiByte(
CP_UTF8, WC_ERR_INVALID_CHARS, utf16_string,
-1, utf8_string.data(),
target_length, nullptr, nullptr);
-1, utf8_string.data(), target_length, nullptr, nullptr);
if (converted_length == 0) {
return std::string();
}