From c033591e6a3cb0b4fcdaeb950ad79840b53df36a Mon Sep 17 00:00:00 2001 From: James Chen Date: Sat, 15 Mar 2014 01:05:04 +0800 Subject: [PATCH] [win32] Overflow fix when using MultiByteToWideChar and WideCharToMultiByte. --- cocos/2d/platform/win32/CCFileUtilsWin32.cpp | 6 +++--- cocos/base/CCConsole.cpp | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/cocos/2d/platform/win32/CCFileUtilsWin32.cpp b/cocos/2d/platform/win32/CCFileUtilsWin32.cpp index 87c44d5d8c..8b93969262 100644 --- a/cocos/2d/platform/win32/CCFileUtilsWin32.cpp +++ b/cocos/2d/platform/win32/CCFileUtilsWin32.cpp @@ -110,7 +110,7 @@ bool FileUtilsWin32::isFileExist(const std::string& strFilePath) const } WCHAR utf16Buf[CC_MAX_PATH] = {0}; - MultiByteToWideChar(CP_UTF8, 0, strPath.c_str(), -1, utf16Buf, sizeof(utf16Buf)); + MultiByteToWideChar(CP_UTF8, 0, strPath.c_str(), -1, utf16Buf, sizeof(utf16Buf)/sizeof(utf16Buf[0])); return GetFileAttributesW(utf16Buf) != -1 ? true : false; } @@ -137,7 +137,7 @@ static Data getData(const std::string& filename, bool forString) std::string fullPath = FileUtils::getInstance()->fullPathForFilename(filename); WCHAR wszBuf[CC_MAX_PATH] = {0}; - MultiByteToWideChar(CP_UTF8, 0, fullPath.c_str(), -1, wszBuf, sizeof(wszBuf)); + MultiByteToWideChar(CP_UTF8, 0, fullPath.c_str(), -1, wszBuf, sizeof(wszBuf)/sizeof(wszBuf[0])); HANDLE fileHandle = ::CreateFileW(wszBuf, GENERIC_READ, 0, NULL, OPEN_EXISTING, NULL, NULL); CC_BREAK_IF(fileHandle == INVALID_HANDLE_VALUE); @@ -211,7 +211,7 @@ unsigned char* FileUtilsWin32::getFileData(const std::string& filename, const ch std::string fullPath = fullPathForFilename(filename); WCHAR wszBuf[CC_MAX_PATH] = {0}; - MultiByteToWideChar(CP_UTF8, 0, fullPath.c_str(), -1, wszBuf, sizeof(wszBuf)); + MultiByteToWideChar(CP_UTF8, 0, fullPath.c_str(), -1, wszBuf, sizeof(wszBuf)/sizeof(wszBuf[0])); HANDLE fileHandle = ::CreateFileW(wszBuf, GENERIC_READ, 0, NULL, OPEN_EXISTING, NULL, NULL); CC_BREAK_IF(fileHandle == INVALID_HANDLE_VALUE); diff --git a/cocos/base/CCConsole.cpp b/cocos/base/CCConsole.cpp index 40fc8970f6..99b37ae047 100644 --- a/cocos/base/CCConsole.cpp +++ b/cocos/base/CCConsole.cpp @@ -214,7 +214,7 @@ static void _log(const char *format, va_list args) WCHAR wszBuf[MAX_LOG_LENGTH] = {0}; MultiByteToWideChar(CP_UTF8, 0, buf, -1, wszBuf, sizeof(wszBuf)); OutputDebugStringW(wszBuf); - WideCharToMultiByte(CP_ACP, 0, wszBuf, sizeof(wszBuf), buf, sizeof(buf), NULL, FALSE); + WideCharToMultiByte(CP_ACP, 0, wszBuf, -1, buf, sizeof(buf), NULL, FALSE); printf("%s", buf); #else