Fixes incorrect wide string conversion on win32 (#1516)

* fixes incorrect wide string conversion on win32

* removed redundant error checks
This commit is contained in:
Jessy De Lannoit 2022-12-06 13:51:41 +02:00 committed by GitHub
parent 2257fa4d6a
commit e67028c510
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -423,19 +423,12 @@ std::string GetSystemName() {
#ifndef UNICODE
str = std::string(hostname, DWCOUNT);
#else
std::vector<wchar_t> converted;
// Find the length first.
int len = ::MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, hostname,
DWCOUNT, converted.begin(), 0);
// TODO: Report error from GetLastError()?
if (len == 0) return std::string("");
converted.reserve(len + 1);
len = ::MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, hostname, DWCOUNT,
converted.begin(), converted.size());
// TODO: Report error from GetLastError()?
if (len == 0) return std::string("");
str = std::string(converted.data());
// `WideCharToMultiByte` returns `0` when conversion fails.
int len = WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, hostname,
DWCOUNT, NULL, 0, NULL, NULL);
str.resize(len);
WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, hostname, DWCOUNT, &str[0],
str.size(), NULL, NULL);
#endif
return str;
#elif defined(BENCHMARK_OS_QURT)