From 6479f972dccaabb3afadb570583a4269e8a785e4 Mon Sep 17 00:00:00 2001 From: Tannin Date: Wed, 25 Feb 2015 18:38:01 +0100 Subject: tons of code cleanup and minor fixes to harden the code (mostly suggestions from static code analysis) --- src/shared/util.cpp | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) (limited to 'src/shared/util.cpp') diff --git a/src/shared/util.cpp b/src/shared/util.cpp index ceed3a34..2ce88eca 100644 --- a/src/shared/util.cpp +++ b/src/shared/util.cpp @@ -55,7 +55,10 @@ std::string ToString(const std::wstring &source, bool utf8) { std::string result; if (source.length() > 0) { - UINT codepage = utf8 ? CP_UTF8 : GetACP(); + UINT codepage = CP_UTF8; + if (!utf8) { + codepage = AreFileApisANSI() ? GetACP() : GetOEMCP(); + } int sizeRequired = ::WideCharToMultiByte(codepage, 0, &source[0], -1, nullptr, 0, nullptr, nullptr); if (sizeRequired == 0) { throw windows_error("failed to convert string to multibyte"); @@ -73,7 +76,10 @@ std::wstring ToWString(const std::string &source, bool utf8) { std::wstring result; if (source.length() > 0) { - UINT codepage = utf8 ? CP_UTF8 : GetACP(); + UINT codepage = CP_UTF8; + if (!utf8) { + codepage = AreFileApisANSI() ? GetACP() : GetOEMCP(); + } int sizeRequired = ::MultiByteToWideChar(codepage, 0, source.c_str(), source.length(), nullptr, 0); if (sizeRequired == 0) { throw windows_error("failed to convert string to wide character"); @@ -113,7 +119,7 @@ std::wstring ToLower(const std::wstring &text) VS_FIXEDFILEINFO GetFileVersion(const std::wstring &fileName) { - DWORD handle; + DWORD handle = 0UL; DWORD size = ::GetFileVersionInfoSizeW(fileName.c_str(), &handle); if (size == 0) { throw windows_error("failed to determine file version info size"); @@ -121,6 +127,7 @@ VS_FIXEDFILEINFO GetFileVersion(const std::wstring &fileName) void *buffer = new char[size]; try { + handle = 0UL; if (!::GetFileVersionInfoW(fileName.c_str(), handle, size, buffer)) { throw windows_error("failed to determine file version info"); } -- cgit v1.3.1