From e67028c510196783b4cb8143d62f81f570fd828b Mon Sep 17 00:00:00 2001 From: Jessy De Lannoit Date: Tue, 6 Dec 2022 13:51:41 +0200 Subject: [PATCH] Fixes incorrect wide string conversion on win32 (#1516) * fixes incorrect wide string conversion on win32 * removed redundant error checks --- src/sysinfo.cc | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-) diff --git a/src/sysinfo.cc b/src/sysinfo.cc index 917429ad..41c0f9f9 100644 --- a/src/sysinfo.cc +++ b/src/sysinfo.cc @@ -423,19 +423,12 @@ std::string GetSystemName() { #ifndef UNICODE str = std::string(hostname, DWCOUNT); #else - std::vector 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)