From afc165146d3e03382f3ed8f7891957c6b7bb7b94 Mon Sep 17 00:00:00 2001 From: Juha Sointusalo Date: Tue, 10 Jul 2018 23:36:38 +0300 Subject: [PATCH] lib: fix wide char conversion functions When MultiByteToWideChar and WideCharToMultiByte are told to process entire input strings the functions include null terminators in required buffer sizes. std::string on the other hand stores embedded null characters in the string it manages if it's requested to do so. The way the code constructs std::string's from wide char function outputs makes null terminators part of the strings. This breaks concatenating strings together. Fix the code by not copying the last character, i.e. the null terminator, to std::string. --- lib/win_util.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/win_util.cpp b/lib/win_util.cpp index 28253c55df..33d02a1193 100644 --- a/lib/win_util.cpp +++ b/lib/win_util.cpp @@ -125,7 +125,7 @@ std::wstring boinc_ascii_to_wide(const std::string& str) { int length_wide = MultiByteToWideChar(CP_ACP, 0, str.data(), -1, NULL, 0); wchar_t *string_wide = static_cast(_alloca((length_wide * sizeof(wchar_t)) + sizeof(wchar_t))); MultiByteToWideChar(CP_ACP, 0, str.data(), -1, string_wide, length_wide); - std::wstring result(string_wide, length_wide); + std::wstring result(string_wide, length_wide - 1); return result; } @@ -133,7 +133,7 @@ std::string boinc_wide_to_ascii(const std::wstring& str) { int length_ansi = WideCharToMultiByte(CP_UTF8, 0, str.data(), -1, NULL, 0, NULL, NULL); char* string_ansi = static_cast(_alloca(length_ansi + sizeof(char))); WideCharToMultiByte(CP_UTF8, 0, str.data(), -1, string_ansi, length_ansi, NULL, NULL); - std::string result(string_ansi, length_ansi); + std::string result(string_ansi, length_ansi - 1); return result; }