From a88339c21968ba48d553f97195d4457c26a7a60c Mon Sep 17 00:00:00 2001 From: Ben Payne Date: Tue, 20 Jan 2015 16:21:56 -0500 Subject: [PATCH 01/10] Fix buffer overflows due to incorrect use of sizeof A snippet of example code: UTF16 pszFilter[1024]; ... convertUTF8toUTF16((UTF8 *)mData.mFilters, pszFilter, sizeof(pszFilter)); Since the conversion function is expecting the third parameter to be the length in 16-bit characters, *not* bytes, this results in the function writing outside the bounds of the output array. To make this less likely to happen in the future (I hope), I've provided a template function that infers the correct size of a static array, so it's no longer necessary to pass the size in most cases. The sized function has been renamed with an "N" suffix to hopefully encourage this use. This bug was caught due to a warning from MSVC about stack corruption occurring in codeBlock::exec(), after opening a file open dialog twice in succession. After some hunting, I found that this was due to FileDialog::Execute() passing incorrect buffer sizes to the conversion function, which resulted in the function writing a null terminator into some memory that happened to be in the stack frame of codeBlock::exec()! --- Engine/source/core/stringBuffer.cpp | 2 +- Engine/source/core/strings/unicode.cpp | 4 +- Engine/source/core/strings/unicode.h | 8 +++- Engine/source/gfx/gFont.cpp | 4 +- Engine/source/gfx/gfxDrawUtil.cpp | 2 +- .../nativeDialogs/fileDialog.cpp | 8 ++-- Engine/source/platformWin32/winConsole.cpp | 2 +- Engine/source/platformWin32/winExec.cpp | 10 ++--- Engine/source/platformWin32/winFileio.cpp | 40 +++++++++---------- Engine/source/platformWin32/winRedbook.cpp | 2 +- Engine/source/platformWin32/winWindow.cpp | 22 +++++----- 11 files changed, 55 insertions(+), 49 deletions(-) diff --git a/Engine/source/core/stringBuffer.cpp b/Engine/source/core/stringBuffer.cpp index ae95fc060..2f05d1973 100644 --- a/Engine/source/core/stringBuffer.cpp +++ b/Engine/source/core/stringBuffer.cpp @@ -141,7 +141,7 @@ void StringBuffer::set(const UTF8 *in) incRequestCount8(); // Convert and store. Note that a UTF16 version of the string cannot be longer. FrameTemp tmpBuff(dStrlen(in)+1); - if(!in || in[0] == 0 || !convertUTF8toUTF16(in, tmpBuff, dStrlen(in)+1)) + if(!in || in[0] == 0 || !convertUTF8toUTF16N(in, tmpBuff, dStrlen(in)+1)) { // Easy out, it's a blank string, or a bad string. mBuffer.clear(); diff --git a/Engine/source/core/strings/unicode.cpp b/Engine/source/core/strings/unicode.cpp index 0dee6f3ef..26bad63d2 100644 --- a/Engine/source/core/strings/unicode.cpp +++ b/Engine/source/core/strings/unicode.cpp @@ -146,7 +146,7 @@ inline bool isAboveBMP(U32 codepoint) } //----------------------------------------------------------------------------- -U32 convertUTF8toUTF16(const UTF8 *unistring, UTF16 *outbuffer, U32 len) +U32 convertUTF8toUTF16N(const UTF8 *unistring, UTF16 *outbuffer, U32 len) { AssertFatal(len >= 1, "Buffer for unicode conversion must be large enough to hold at least the null terminator."); PROFILE_SCOPE(convertUTF8toUTF16); @@ -252,7 +252,7 @@ UTF16* convertUTF8toUTF16( const UTF8* unistring) FrameTemp buf(len); // perform conversion - nCodepoints = convertUTF8toUTF16( unistring, buf, len); + nCodepoints = convertUTF8toUTF16N( unistring, buf, len); // add 1 for the NULL terminator the converter promises it included. nCodepoints++; diff --git a/Engine/source/core/strings/unicode.h b/Engine/source/core/strings/unicode.h index 042415377..9a0c634eb 100644 --- a/Engine/source/core/strings/unicode.h +++ b/Engine/source/core/strings/unicode.h @@ -79,10 +79,16 @@ UTF8* convertUTF16toUTF8( const UTF16 *unistring); /// - Output is null terminated. Be sure to provide 1 extra byte, U16 or U32 for /// the null terminator, or you will see truncated output. /// - If the provided buffer is too small, the output will be truncated. -U32 convertUTF8toUTF16(const UTF8 *unistring, UTF16 *outbuffer, U32 len); +U32 convertUTF8toUTF16N(const UTF8 *unistring, UTF16 *outbuffer, U32 len); U32 convertUTF16toUTF8( const UTF16 *unistring, UTF8 *outbuffer, U32 len); +template +inline U32 convertUTF8toUTF16(const UTF8 *unistring, UTF16 (&outbuffer)[N]) +{ + return convertUTF8toUTF16N(unistring, outbuffer, (U32) N); +} + //----------------------------------------------------------------------------- /// Functions that converts one unicode codepoint at a time /// - Since these functions are designed to be used in tight loops, they do not diff --git a/Engine/source/gfx/gFont.cpp b/Engine/source/gfx/gFont.cpp index 7c838915d..8d555955b 100644 --- a/Engine/source/gfx/gFont.cpp +++ b/Engine/source/gfx/gFont.cpp @@ -423,7 +423,7 @@ U32 GFont::getStrNWidth(const UTF8 *str, U32 n) { // UTF8 conversion is expensive. Avoid converting in a tight loop. FrameTemp str16(n + 1); - convertUTF8toUTF16(str, str16, n + 1); + convertUTF8toUTF16N(str, str16, n + 1); return getStrNWidth(str16, dStrlen(str16)); } @@ -462,7 +462,7 @@ U32 GFont::getStrNWidth(const UTF16 *str, U32 n) U32 GFont::getStrNWidthPrecise(const UTF8 *str, U32 n) { FrameTemp str16(n + 1); - convertUTF8toUTF16(str, str16, n + 1); + convertUTF8toUTF16N(str, str16, n + 1); return getStrNWidthPrecise(str16, dStrlen(str16)); } diff --git a/Engine/source/gfx/gfxDrawUtil.cpp b/Engine/source/gfx/gfxDrawUtil.cpp index 68a336aab..b04791d1c 100644 --- a/Engine/source/gfx/gfxDrawUtil.cpp +++ b/Engine/source/gfx/gfxDrawUtil.cpp @@ -149,7 +149,7 @@ U32 GFXDrawUtil::drawTextN( GFont *font, const Point2I &ptDraw, const UTF8 *in_s // Convert to UTF16 temporarily. n++; // space for null terminator FrameTemp ubuf( n * sizeof(UTF16) ); - convertUTF8toUTF16(in_string, ubuf, n); + convertUTF8toUTF16N(in_string, ubuf, n); return drawTextN( font, ptDraw, ubuf, n, colorTable, maxColorIndex, rot ); } diff --git a/Engine/source/platformWin32/nativeDialogs/fileDialog.cpp b/Engine/source/platformWin32/nativeDialogs/fileDialog.cpp index 5db038332..55e7912fc 100644 --- a/Engine/source/platformWin32/nativeDialogs/fileDialog.cpp +++ b/Engine/source/platformWin32/nativeDialogs/fileDialog.cpp @@ -324,10 +324,10 @@ bool FileDialog::Execute() UTF16 pszFileTitle[MAX_PATH]; UTF16 pszDefaultExtension[MAX_PATH]; // Convert parameters to UTF16*'s - convertUTF8toUTF16((UTF8 *)mData.mDefaultFile, pszFile, sizeof(pszFile)); - convertUTF8toUTF16((UTF8 *)mData.mDefaultPath, pszInitialDir, sizeof(pszInitialDir)); - convertUTF8toUTF16((UTF8 *)mData.mTitle, pszTitle, sizeof(pszTitle)); - convertUTF8toUTF16((UTF8 *)mData.mFilters, pszFilter, sizeof(pszFilter) ); + convertUTF8toUTF16((UTF8 *)mData.mDefaultFile, pszFile); + convertUTF8toUTF16((UTF8 *)mData.mDefaultPath, pszInitialDir); + convertUTF8toUTF16((UTF8 *)mData.mTitle, pszTitle); + convertUTF8toUTF16((UTF8 *)mData.mFilters, pszFilter); #else // Not Unicode, All char*'s! char pszFile[MAX_PATH]; diff --git a/Engine/source/platformWin32/winConsole.cpp b/Engine/source/platformWin32/winConsole.cpp index 0040c5677..cc92c2c15 100644 --- a/Engine/source/platformWin32/winConsole.cpp +++ b/Engine/source/platformWin32/winConsole.cpp @@ -67,7 +67,7 @@ void WinConsole::enable(bool enabled) { #ifdef UNICODE UTF16 buf[512]; - convertUTF8toUTF16((UTF8 *)title, buf, sizeof(buf)); + convertUTF8toUTF16((UTF8 *)title, buf); SetConsoleTitle(buf); #else SetConsoleTitle(title); diff --git a/Engine/source/platformWin32/winExec.cpp b/Engine/source/platformWin32/winExec.cpp index 67ef9d397..eb7a1928c 100644 --- a/Engine/source/platformWin32/winExec.cpp +++ b/Engine/source/platformWin32/winExec.cpp @@ -88,15 +88,15 @@ ExecuteThread::ExecuteThread(const char *executable, const char *args /* = NULL #ifdef UNICODE WCHAR exe[ 1024 ]; - convertUTF8toUTF16( exeBuf, exe, sizeof( exe ) / sizeof( exe[ 0 ] ) ); + convertUTF8toUTF16( exeBuf, exe ); TempAlloc< WCHAR > argsBuf( ( args ? dStrlen( args ) : 0 ) + 1 ); argsBuf[ argsBuf.size - 1 ] = 0; if( args ) - convertUTF8toUTF16( args, argsBuf, argsBuf.size ); + convertUTF8toUTF16N( args, argsBuf, argsBuf.size ); if( directory ) - convertUTF8toUTF16( directory, dirBuf, dirBuf.size ); + convertUTF8toUTF16N( directory, dirBuf, dirBuf.size ); #else char* exe = exeBuf; char* argsBuf = args; @@ -162,7 +162,7 @@ void Platform::openFolder(const char* path ) #ifdef UNICODE WCHAR p[ 1024 ]; - convertUTF8toUTF16( filePath, p, sizeof( p ) / sizeof( p[ 0 ] ) ); + convertUTF8toUTF16( filePath, p ); #else char* p = filePath; #endif @@ -179,7 +179,7 @@ void Platform::openFile(const char* path ) #ifdef UNICODE WCHAR p[ 1024 ]; - convertUTF8toUTF16( filePath, p, sizeof( p ) / sizeof( p[ 0 ] ) ); + convertUTF8toUTF16( filePath, p ); #else char* p = filePath; #endif diff --git a/Engine/source/platformWin32/winFileio.cpp b/Engine/source/platformWin32/winFileio.cpp index 7296ed32e..dff87f3b7 100644 --- a/Engine/source/platformWin32/winFileio.cpp +++ b/Engine/source/platformWin32/winFileio.cpp @@ -54,7 +54,7 @@ bool dFileDelete(const char * name) TempAlloc< TCHAR > buf( dStrlen( name ) + 1 ); #ifdef UNICODE - convertUTF8toUTF16( name, buf, buf.size ); + convertUTF8toUTF16N( name, buf, buf.size ); #else dStrcpy( buf, name ); #endif @@ -74,8 +74,8 @@ bool dFileRename(const char *oldName, const char *newName) TempAlloc< TCHAR > newf( dStrlen( newName ) + 1 ); #ifdef UNICODE - convertUTF8toUTF16( oldName, oldf, oldf.size ); - convertUTF8toUTF16( newName, newf, newf.size ); + convertUTF8toUTF16N( oldName, oldf, oldf.size ); + convertUTF8toUTF16N( newName, newf, newf.size ); #else dStrcpy(oldf, oldName); dStrcpy(newf, newName); @@ -93,7 +93,7 @@ bool dFileTouch(const char * name) TempAlloc< TCHAR > buf( dStrlen( name ) + 1 ); #ifdef UNICODE - convertUTF8toUTF16( name, buf, buf.size ); + convertUTF8toUTF16N( name, buf, buf.size ); #else dStrcpy( buf, name ); #endif @@ -119,8 +119,8 @@ bool dPathCopy(const char *fromName, const char *toName, bool nooverwrite) TempAlloc< TCHAR > to( dStrlen( toName ) + 1 ); #ifdef UNICODE - convertUTF8toUTF16( fromName, from, from.size ); - convertUTF8toUTF16( toName, to, to.size ); + convertUTF8toUTF16N( fromName, from, from.size ); + convertUTF8toUTF16N( toName, to, to.size ); #else dStrcpy( from, fromName ); dStrcpy( to, toName ); @@ -187,8 +187,8 @@ bool dPathCopy(const char *fromName, const char *toName, bool nooverwrite) backslash(toFile); #ifdef UNICODE - convertUTF8toUTF16( tempBuf, wtempBuf, wtempBuf.size ); - convertUTF8toUTF16( tempBuf1, wtempBuf1, wtempBuf1.size ); + convertUTF8toUTF16N( tempBuf, wtempBuf, wtempBuf.size ); + convertUTF8toUTF16N( tempBuf1, wtempBuf1, wtempBuf1.size ); WCHAR* f = wtempBuf1; WCHAR* t = wtempBuf; #else @@ -256,7 +256,7 @@ File::FileStatus File::open(const char *filename, const AccessMode openMode) TempAlloc< TCHAR > fname( dStrlen( filename ) + 1 ); #ifdef UNICODE - convertUTF8toUTF16( filename, fname, fname.size ); + convertUTF8toUTF16N( filename, fname, fname.size ); #else dStrcpy(fname, filename); #endif @@ -586,7 +586,7 @@ static bool recurseDumpPath(const char *path, const char *pattern, Vector searchBuf( buf.size ); - convertUTF8toUTF16( buf, searchBuf, searchBuf.size ); + convertUTF8toUTF16N( buf, searchBuf, searchBuf.size ); WCHAR* search = searchBuf; #else char *search = buf; @@ -665,7 +665,7 @@ bool Platform::getFileTimes(const char *filePath, FileTime *createTime, FileTime TempAlloc< TCHAR > fp( dStrlen( filePath ) + 1 ); #ifdef UNICODE - convertUTF8toUTF16( filePath, fp, fp.size ); + convertUTF8toUTF16N( filePath, fp, fp.size ); #else dStrcpy( fp, filePath ); #endif @@ -697,7 +697,7 @@ bool Platform::createPath(const char *file) #ifdef UNICODE TempAlloc< WCHAR > fileBuf( pathbuf.size ); - convertUTF8toUTF16( file, fileBuf, fileBuf.size ); + convertUTF8toUTF16N( file, fileBuf, fileBuf.size ); const WCHAR* fileName = fileBuf; const WCHAR* dir; #else @@ -820,7 +820,7 @@ bool Platform::setCurrentDirectory(StringTableEntry newDir) TempAlloc< TCHAR > buf( dStrlen( newDir ) + 2 ); #ifdef UNICODE - convertUTF8toUTF16( newDir, buf, buf.size - 1 ); + convertUTF8toUTF16N( newDir, buf, buf.size - 1 ); #else dStrcpy( buf, newDir ); #endif @@ -935,7 +935,7 @@ bool Platform::isFile(const char *pFilePath) TempAlloc< TCHAR > buf( dStrlen( pFilePath ) + 1 ); #ifdef UNICODE - convertUTF8toUTF16( pFilePath, buf, buf.size ); + convertUTF8toUTF16N( pFilePath, buf, buf.size ); #else dStrcpy( buf, pFilePath ); #endif @@ -974,7 +974,7 @@ S32 Platform::getFileSize(const char *pFilePath) TempAlloc< TCHAR > buf( dStrlen( pFilePath ) + 1 ); #ifdef UNICODE - convertUTF8toUTF16( pFilePath, buf, buf.size ); + convertUTF8toUTF16N( pFilePath, buf, buf.size ); #else dStrcpy( buf, pFilePath ); #endif @@ -1011,7 +1011,7 @@ bool Platform::isDirectory(const char *pDirPath) TempAlloc< TCHAR > buf( dStrlen( pDirPath ) + 1 ); #ifdef UNICODE - convertUTF8toUTF16( pDirPath, buf, buf.size ); + convertUTF8toUTF16N( pDirPath, buf, buf.size ); #else dStrcpy( buf, pDirPath ); #endif @@ -1057,8 +1057,8 @@ bool Platform::isSubDirectory(const char *pParent, const char *pDir) TempAlloc< TCHAR > dir( dStrlen( pDir ) + 1 ); #ifdef UNICODE - convertUTF8toUTF16( fileName, file, file.size ); - convertUTF8toUTF16( pDir, dir, dir.size ); + convertUTF8toUTF16N( fileName, file, file.size ); + convertUTF8toUTF16N( pDir, dir, dir.size ); #else dStrcpy( file, fileName ); dStrcpy( dir, pDir ); @@ -1251,7 +1251,7 @@ bool Platform::hasSubDirectory(const char *pPath) #ifdef UNICODE WCHAR buf[ 1024 ]; - convertUTF8toUTF16( searchBuf, buf, sizeof( buf ) / sizeof( buf[ 0 ] ) ); + convertUTF8toUTF16( searchBuf, buf ); WCHAR* search = buf; #else char* search = searchBuf; @@ -1335,7 +1335,7 @@ static bool recurseDumpDirectories(const char *basePath, const char *subPath, Ve #ifdef UNICODE TempAlloc< WCHAR > searchStr( dStrlen( search ) + 1 ); - convertUTF8toUTF16( search, searchStr, searchStr.size ); + convertUTF8toUTF16N( search, searchStr, searchStr.size ); #else char* searchStr = search; #endif diff --git a/Engine/source/platformWin32/winRedbook.cpp b/Engine/source/platformWin32/winRedbook.cpp index 38c5b3933..309f7513a 100644 --- a/Engine/source/platformWin32/winRedbook.cpp +++ b/Engine/source/platformWin32/winRedbook.cpp @@ -144,7 +144,7 @@ bool Win32RedBookDevice::open() openParms.lpstrDeviceType = (LPCWSTR)MCI_DEVTYPE_CD_AUDIO; UTF16 buf[512]; - convertUTF8toUTF16((UTF8 *)mDeviceName, buf, sizeof(buf)); + convertUTF8toUTF16((UTF8 *)mDeviceName, buf); openParms.lpstrElementName = buf; #else openParms.lpstrDeviceType = (LPCSTR)MCI_DEVTYPE_CD_AUDIO; diff --git a/Engine/source/platformWin32/winWindow.cpp b/Engine/source/platformWin32/winWindow.cpp index 45119dbb4..f8417f2f1 100644 --- a/Engine/source/platformWin32/winWindow.cpp +++ b/Engine/source/platformWin32/winWindow.cpp @@ -104,7 +104,7 @@ bool Platform::excludeOtherInstances(const char *mutexName) { #ifdef UNICODE UTF16 b[512]; - convertUTF8toUTF16((UTF8 *)mutexName, b, sizeof(b)); + convertUTF8toUTF16((UTF8 *)mutexName, b); gMutexHandle = CreateMutex(NULL, true, b); #else gMutexHandle = CreateMutex(NULL, true, mutexName); @@ -164,7 +164,7 @@ bool Platform::checkOtherInstances(const char *mutexName) #ifdef UNICODE UTF16 b[512]; - convertUTF8toUTF16((UTF8 *)mutexName, b, sizeof(b)); + convertUTF8toUTF16((UTF8 *)mutexName, b); pMutex = CreateMutex(NULL, true, b); #else pMutex = CreateMutex(NULL, true, mutexName); @@ -197,8 +197,8 @@ void Platform::AlertOK(const char *windowTitle, const char *message) ShowCursor(true); #ifdef UNICODE UTF16 m[1024], t[512]; - convertUTF8toUTF16((UTF8 *)windowTitle, t, sizeof(t)); - convertUTF8toUTF16((UTF8 *)message, m, sizeof(m)); + convertUTF8toUTF16((UTF8 *)windowTitle, t); + convertUTF8toUTF16((UTF8 *)message, m); MessageBox(NULL, m, t, MB_ICONINFORMATION | MB_SETFOREGROUND | MB_TASKMODAL | MB_OK); #else MessageBox(NULL, message, windowTitle, MB_ICONINFORMATION | MB_SETFOREGROUND | MB_TASKMODAL | MB_OK); @@ -211,8 +211,8 @@ bool Platform::AlertOKCancel(const char *windowTitle, const char *message) ShowCursor(true); #ifdef UNICODE UTF16 m[1024], t[512]; - convertUTF8toUTF16((UTF8 *)windowTitle, t, sizeof(t)); - convertUTF8toUTF16((UTF8 *)message, m, sizeof(m)); + convertUTF8toUTF16((UTF8 *)windowTitle, t); + convertUTF8toUTF16((UTF8 *)message, m); return MessageBox(NULL, m, t, MB_ICONINFORMATION | MB_SETFOREGROUND | MB_TASKMODAL | MB_OKCANCEL) == IDOK; #else return MessageBox(NULL, message, windowTitle, MB_ICONINFORMATION | MB_SETFOREGROUND | MB_TASKMODAL | MB_OKCANCEL) == IDOK; @@ -225,8 +225,8 @@ bool Platform::AlertRetry(const char *windowTitle, const char *message) ShowCursor(true); #ifdef UNICODE UTF16 m[1024], t[512]; - convertUTF8toUTF16((UTF8 *)windowTitle, t, sizeof(t)); - convertUTF8toUTF16((UTF8 *)message, m, sizeof(m)); + convertUTF8toUTF16((UTF8 *)windowTitle, t); + convertUTF8toUTF16((UTF8 *)message, m); return (MessageBox(NULL, m, t, MB_ICONINFORMATION | MB_SETFOREGROUND | MB_TASKMODAL | MB_RETRYCANCEL) == IDRETRY); #else return (MessageBox(NULL, message, windowTitle, MB_ICONINFORMATION | MB_SETFOREGROUND | MB_TASKMODAL | MB_RETRYCANCEL) == IDRETRY); @@ -241,8 +241,8 @@ Platform::ALERT_ASSERT_RESULT Platform::AlertAssert(const char *windowTitle, con #ifdef UNICODE UTF16 messageUTF[1024], title[512]; - convertUTF8toUTF16((UTF8 *)windowTitle, title, sizeof(title)); - convertUTF8toUTF16((UTF8 *)message, messageUTF, sizeof(messageUTF)); + convertUTF8toUTF16((UTF8 *)windowTitle, title); + convertUTF8toUTF16((UTF8 *)message, messageUTF); #else const char* messageUTF = message; const char* title = windowTitle; @@ -560,7 +560,7 @@ bool Platform::openWebBrowser( const char* webAddress ) #ifdef UNICODE dSprintf( buf, sizeof( buf ), "%s %s", utf8WebKey, webAddress ); UTF16 b[1024]; - convertUTF8toUTF16((UTF8 *)buf, b, sizeof(b)); + convertUTF8toUTF16((UTF8 *)buf, b); #else dSprintf( buf, sizeof( buf ), "%s %s", sWebKey, webAddress ); #endif From e3bbc4292536276de398ef39d3f6a878f3523746 Mon Sep 17 00:00:00 2001 From: Ben Payne Date: Tue, 20 Jan 2015 17:45:31 -0500 Subject: [PATCH 02/10] Provide a safer version of convertUTF16toUTF8 --- Engine/source/core/stringBuffer.cpp | 4 ++-- Engine/source/core/strings/unicode.cpp | 4 ++-- Engine/source/core/strings/unicode.h | 8 +++++++- .../platformWin32/nativeDialogs/fileDialog.cpp | 8 ++++---- Engine/source/platformWin32/winDInputDevice.cpp | 4 ++-- Engine/source/platformWin32/winFileio.cpp | 12 ++++++------ Engine/source/platformWin32/winFont.cpp | 2 +- Engine/source/platformWin32/winUser.cpp | 4 ++-- Engine/source/platformWin32/winVolume.cpp | 4 ++-- Engine/source/platformWin32/winWindow.cpp | 6 +++--- 10 files changed, 31 insertions(+), 25 deletions(-) diff --git a/Engine/source/core/stringBuffer.cpp b/Engine/source/core/stringBuffer.cpp index 2f05d1973..1706de9dc 100644 --- a/Engine/source/core/stringBuffer.cpp +++ b/Engine/source/core/stringBuffer.cpp @@ -359,7 +359,7 @@ void StringBuffer::getCopy8(UTF8 *buff, const U32 buffSize) const { incRequestCount8(); AssertFatal(mBuffer.last() == 0, "StringBuffer::get UTF8 - not a null terminated string!"); - convertUTF16toUTF8(mBuffer.address(), buff, buffSize); + convertUTF16toUTF8N(mBuffer.address(), buff, buffSize); } void StringBuffer::getCopy(UTF16 *buff, const U32 buffSize) const @@ -408,7 +408,7 @@ void StringBuffer::updateBuffer8() { U32 slackLen = getUTF8BufferSizeEstimate(); mBuffer8.setSize(slackLen); - U32 len = convertUTF16toUTF8(mBuffer.address(), mBuffer8.address(), slackLen); + U32 len = convertUTF16toUTF8N(mBuffer.address(), mBuffer8.address(), slackLen); mBuffer8.setSize(len+1); mBuffer8.compact(); mDirty8 = false; diff --git a/Engine/source/core/strings/unicode.cpp b/Engine/source/core/strings/unicode.cpp index 26bad63d2..ffc650fae 100644 --- a/Engine/source/core/strings/unicode.cpp +++ b/Engine/source/core/strings/unicode.cpp @@ -191,7 +191,7 @@ U32 convertUTF8toUTF16N(const UTF8 *unistring, UTF16 *outbuffer, U32 len) } //----------------------------------------------------------------------------- -U32 convertUTF16toUTF8( const UTF16 *unistring, UTF8 *outbuffer, U32 len) +U32 convertUTF16toUTF8N( const UTF16 *unistring, UTF8 *outbuffer, U32 len) { AssertFatal(len >= 1, "Buffer for unicode conversion must be large enough to hold at least the null terminator."); PROFILE_START(convertUTF16toUTF8); @@ -274,7 +274,7 @@ UTF8* convertUTF16toUTF8( const UTF16* unistring) FrameTemp buf(len); // perform conversion - nCodeunits = convertUTF16toUTF8( unistring, buf, len); + nCodeunits = convertUTF16toUTF8N( unistring, buf, len); // add 1 for the NULL terminator the converter promises it included. nCodeunits++; diff --git a/Engine/source/core/strings/unicode.h b/Engine/source/core/strings/unicode.h index 9a0c634eb..b5d332b40 100644 --- a/Engine/source/core/strings/unicode.h +++ b/Engine/source/core/strings/unicode.h @@ -81,7 +81,7 @@ UTF8* convertUTF16toUTF8( const UTF16 *unistring); /// - If the provided buffer is too small, the output will be truncated. U32 convertUTF8toUTF16N(const UTF8 *unistring, UTF16 *outbuffer, U32 len); -U32 convertUTF16toUTF8( const UTF16 *unistring, UTF8 *outbuffer, U32 len); +U32 convertUTF16toUTF8N( const UTF16 *unistring, UTF8 *outbuffer, U32 len); template inline U32 convertUTF8toUTF16(const UTF8 *unistring, UTF16 (&outbuffer)[N]) @@ -89,6 +89,12 @@ inline U32 convertUTF8toUTF16(const UTF8 *unistring, UTF16 (&outbuffer)[N]) return convertUTF8toUTF16N(unistring, outbuffer, (U32) N); } +template +inline U32 convertUTF16toUTF8(const UTF16 *unistring, UTF8 (&outbuffer)[N]) +{ + return convertUTF16toUTF8N(unistring, outbuffer, (U32) N); +} + //----------------------------------------------------------------------------- /// Functions that converts one unicode codepoint at a time /// - Since these functions are designed to be used in tight loops, they do not diff --git a/Engine/source/platformWin32/nativeDialogs/fileDialog.cpp b/Engine/source/platformWin32/nativeDialogs/fileDialog.cpp index 55e7912fc..1d64139ee 100644 --- a/Engine/source/platformWin32/nativeDialogs/fileDialog.cpp +++ b/Engine/source/platformWin32/nativeDialogs/fileDialog.cpp @@ -79,7 +79,7 @@ static LRESULT PASCAL OKBtnFolderHackProc(HWND hWnd, UINT uMsg, WPARAM wParam, L char *filePath; #ifdef UNICODE char fileBuf[MAX_PATH]; - convertUTF16toUTF8(ofn->lpstrFile, fileBuf, sizeof(fileBuf)); + convertUTF16toUTF8(ofn->lpstrFile, fileBuf); filePath = fileBuf; #else filePath = ofn->lpstrFile; @@ -140,7 +140,7 @@ static UINT_PTR CALLBACK FolderHookProc(HWND hdlg, UINT uMsg, WPARAM wParam, LPA char filePath[MAX_PATH]; #ifdef UNICODE - convertUTF16toUTF8(buf, filePath, sizeof(filePath)); + convertUTF16toUTF8(buf, filePath); #else dStrcpy( filePath, buf ); #endif @@ -158,7 +158,7 @@ static UINT_PTR CALLBACK FolderHookProc(HWND hdlg, UINT uMsg, WPARAM wParam, LPA #ifdef UNICODE char buf2[MAX_PATH]; - convertUTF16toUTF8(buf, buf2, sizeof(buf2)); + convertUTF16toUTF8(buf, buf2); #else char *buf2 = buf; #endif @@ -442,7 +442,7 @@ bool FileDialog::Execute() // Handle Result Properly for Unicode as well as ANSI #ifdef UNICODE if(pszFileTitle[0] || ! ( mData.mStyle & FileDialogData::FDS_OPEN && mData.mStyle & FileDialogData::FDS_MULTIPLEFILES )) - convertUTF16toUTF8( (UTF16*)pszFile, (UTF8*)pszResult, sizeof(pszResult)); + convertUTF16toUTF8( (UTF16*)pszFile, pszResult); else convertUTF16toUTF8DoubleNULL( (UTF16*)pszFile, (UTF8*)pszResult, sizeof(pszResult)); #else diff --git a/Engine/source/platformWin32/winDInputDevice.cpp b/Engine/source/platformWin32/winDInputDevice.cpp index 8d036352f..a88f9ff1d 100644 --- a/Engine/source/platformWin32/winDInputDevice.cpp +++ b/Engine/source/platformWin32/winDInputDevice.cpp @@ -588,7 +588,7 @@ const char* DInputDevice::getName() { #ifdef UNICODE static UTF8 buf[512]; - convertUTF16toUTF8(mDeviceInstance.tszInstanceName, buf, sizeof(buf)); + convertUTF16toUTF8(mDeviceInstance.tszInstanceName, buf); return (const char *)buf; #else return mDeviceInstance.tszInstanceName; @@ -600,7 +600,7 @@ const char* DInputDevice::getProductName() { #ifdef UNICODE static UTF8 buf[512]; - convertUTF16toUTF8(mDeviceInstance.tszProductName, buf, sizeof(buf)); + convertUTF16toUTF8(mDeviceInstance.tszProductName, buf); return (const char *)buf; #else return mDeviceInstance.tszProductName; diff --git a/Engine/source/platformWin32/winFileio.cpp b/Engine/source/platformWin32/winFileio.cpp index dff87f3b7..764eabe20 100644 --- a/Engine/source/platformWin32/winFileio.cpp +++ b/Engine/source/platformWin32/winFileio.cpp @@ -601,7 +601,7 @@ static bool recurseDumpPath(const char *path, const char *pattern, Vector& out_rVolumeI #ifdef UNICODE char buf[ sizeof( lpszFileSystem ) / sizeof( lpszFileSystem[ 0 ] ) * 3 + 1 ]; - convertUTF16toUTF8( lpszFileSystem, buf, sizeof( buf ) / sizeof( buf[ 0 ] ) ); + convertUTF16toUTF8( lpszFileSystem, buf ); info.FileSystem = StringTable->insert( buf ); - convertUTF16toUTF8( lpszVolumeName, buf, sizeof( buf ) / sizeof( buf[ 0 ] ) ); + convertUTF16toUTF8( lpszVolumeName ); info.Name = StringTable->insert( buf ); #else info.FileSystem = StringTable->insert( lpszFileSystem ); @@ -1276,7 +1276,7 @@ bool Platform::hasSubDirectory(const char *pPath) #ifdef UNICODE char fileName[ 1024 ]; - convertUTF16toUTF8( findData.cFileName, fileName, sizeof( fileName ) / sizeof( fileName[ 0 ] ) ); + convertUTF16toUTF8( findData.cFileName, fileName ); #else char* fileName = findData.cFileName; #endif @@ -1397,7 +1397,7 @@ static bool recurseDumpDirectories(const char *basePath, const char *subPath, Ve continue; #ifdef UNICODE - convertUTF16toUTF8( findData.cFileName, fileName, fileName.size ); + convertUTF16toUTF8N( findData.cFileName, fileName, fileName.size ); #else char* fileName = findData.cFileName; #endif @@ -1472,7 +1472,7 @@ StringTableEntry osGetTemporaryDirectory() #ifdef UNICODE TempAlloc< char > dirBuffer( len * 3 + 1 ); char* dir = dirBuffer; - convertUTF16toUTF8( buffer, dir, dirBuffer.size ); + convertUTF16toUTF8N( buffer, dir, dirBuffer.size ); #else char* dir = buf; #endif diff --git a/Engine/source/platformWin32/winFont.cpp b/Engine/source/platformWin32/winFont.cpp index ff179be5f..a67b096b3 100644 --- a/Engine/source/platformWin32/winFont.cpp +++ b/Engine/source/platformWin32/winFont.cpp @@ -104,7 +104,7 @@ BOOL CALLBACK EnumFamCallBack(LPLOGFONT logFont, LPNEWTEXTMETRIC textMetric, DWO const U32 len = dStrlen( logFont->lfFaceName ) * 3 + 1; FrameTemp buffer( len ); - convertUTF16toUTF8( logFont->lfFaceName, buffer, len ); + convertUTF16toUTF8N( logFont->lfFaceName, buffer, len ); fonts->push_back( StringTable->insert( buffer ) ); diff --git a/Engine/source/platformWin32/winUser.cpp b/Engine/source/platformWin32/winUser.cpp index c82a273a3..171dfff87 100644 --- a/Engine/source/platformWin32/winUser.cpp +++ b/Engine/source/platformWin32/winUser.cpp @@ -52,7 +52,7 @@ const char *Platform::getUserDataDirectory() #ifdef UNICODE char path[ MAX_PATH * 3 + 1 ]; - convertUTF16toUTF8( szBuffer, path, sizeof( path ) ); + convertUTF16toUTF8( szBuffer, path ); #else char* path = szBuffer; #endif @@ -78,7 +78,7 @@ const char *Platform::getUserHomeDirectory() #ifdef UNICODE char path[ MAX_PATH * 3 + 1 ]; - convertUTF16toUTF8( szBuffer, path, sizeof( path ) ); + convertUTF16toUTF8( szBuffer, path ); #else char* path = szBuffer; #endif diff --git a/Engine/source/platformWin32/winVolume.cpp b/Engine/source/platformWin32/winVolume.cpp index 04422d9eb..9f5eb3459 100644 --- a/Engine/source/platformWin32/winVolume.cpp +++ b/Engine/source/platformWin32/winVolume.cpp @@ -720,13 +720,13 @@ String Platform::FS::getAssetDir() { TCHAR buf[ 2048 ]; ::GetModuleFileNameW( NULL, buf, sizeof( buf ) ); - convertUTF16toUTF8( buf, cen_buf, sizeof( cen_buf ) ); + convertUTF16toUTF8( buf, cen_buf ); } else { TCHAR buf[ 2048 ]; GetCurrentDirectoryW( sizeof( buf ) / sizeof( buf[ 0 ] ), buf ); - convertUTF16toUTF8( buf, cen_buf, sizeof( cen_buf ) ); + convertUTF16toUTF8( buf, cen_buf ); return Path::CleanSeparators(cen_buf); } #else diff --git a/Engine/source/platformWin32/winWindow.cpp b/Engine/source/platformWin32/winWindow.cpp index f8417f2f1..bede84739 100644 --- a/Engine/source/platformWin32/winWindow.cpp +++ b/Engine/source/platformWin32/winWindow.cpp @@ -362,7 +362,7 @@ S32 WINAPI WinMain( HINSTANCE hInstance, HINSTANCE, LPSTR lpszCmdLine, S32) { TCHAR buf[ moduleNameSize ]; GetModuleFileNameW( NULL, buf, moduleNameSize ); - convertUTF16toUTF8( buf, moduleName, moduleNameSize ); + convertUTF16toUTF8( buf, moduleName ); } #else GetModuleFileNameA(NULL, moduleName, moduleNameSize); @@ -440,7 +440,7 @@ S32 torque_winmain( HINSTANCE hInstance, HINSTANCE, LPSTR lpszCmdLine, S32) { TCHAR buf[ moduleNameSize ]; GetModuleFileNameW( NULL, buf, moduleNameSize ); - convertUTF16toUTF8( buf, moduleName, moduleNameSize ); + convertUTF16toUTF8( buf, moduleName ); } #else GetModuleFileNameA(NULL, moduleName, moduleNameSize); @@ -541,7 +541,7 @@ bool Platform::openWebBrowser( const char* webAddress ) RegCloseKey( regKey ); sHaveKey = true; - convertUTF16toUTF8(sWebKey,utf8WebKey,512); + convertUTF16toUTF8(sWebKey,utf8WebKey); #ifdef UNICODE char *p = dStrstr((const char *)utf8WebKey, "%1"); From 7613fa037523ea83a0add6135dee4cf7a30b6fde Mon Sep 17 00:00:00 2001 From: Ben Payne Date: Tue, 20 Jan 2015 17:55:00 -0500 Subject: [PATCH 03/10] Remove unnecessary null termination UTF16Cache::copyToBuffer() is already adding a null terminator --- Engine/source/core/strings/unicode.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/Engine/source/core/strings/unicode.cpp b/Engine/source/core/strings/unicode.cpp index ffc650fae..d43959485 100644 --- a/Engine/source/core/strings/unicode.cpp +++ b/Engine/source/core/strings/unicode.cpp @@ -159,7 +159,6 @@ U32 convertUTF8toUTF16N(const UTF8 *unistring, UTF16 *outbuffer, U32 len) { const UTF16Cache &cache = (*cacheItr).value; cache.copyToBuffer(outbuffer, len); - outbuffer[len-1] = '\0'; return getMin(cache.mLength,len - 1); } #endif From 2cc4801974f9135c3e19b41e3d22a67c4228a4f2 Mon Sep 17 00:00:00 2001 From: Ben Payne Date: Tue, 20 Jan 2015 17:56:11 -0500 Subject: [PATCH 04/10] Remove a dead function --- Engine/source/core/strings/unicode.cpp | 15 --------------- Engine/source/core/strings/unicode.h | 4 ---- 2 files changed, 19 deletions(-) diff --git a/Engine/source/core/strings/unicode.cpp b/Engine/source/core/strings/unicode.cpp index d43959485..3db038aa3 100644 --- a/Engine/source/core/strings/unicode.cpp +++ b/Engine/source/core/strings/unicode.cpp @@ -508,21 +508,6 @@ U32 dStrlen(const UTF32 *unistring) return i; } -//----------------------------------------------------------------------------- -U32 dStrncmp(const UTF16* unistring1, const UTF16* unistring2, U32 len) -{ - UTF16 c1, c2; - for(U32 i = 0; i c2) return 1; - if(!c1) return 0; - } - return 0; -} - //----------------------------------------------------------------------------- const UTF16* dStrrchr(const UTF16* unistring, U32 c) diff --git a/Engine/source/core/strings/unicode.h b/Engine/source/core/strings/unicode.h index b5d332b40..a1293b8ee 100644 --- a/Engine/source/core/strings/unicode.h +++ b/Engine/source/core/strings/unicode.h @@ -117,10 +117,6 @@ U32 oneUTF32toUTF8( const UTF32 codepoint, UTF8 *threeByteCodeunitBuf); U32 dStrlen(const UTF16 *unistring); U32 dStrlen(const UTF32 *unistring); -//----------------------------------------------------------------------------- -/// Comparing unicode strings -U32 dStrncmp(const UTF16* unistring1, const UTF16* unistring2, U32 len); - //----------------------------------------------------------------------------- /// Scanning for characters in unicode strings UTF16* dStrrchr(UTF16* unistring, U32 c); From 4694b0a8edf0312e78a4f12e1483e432b5140cc8 Mon Sep 17 00:00:00 2001 From: Ben Payne Date: Tue, 20 Jan 2015 17:56:46 -0500 Subject: [PATCH 05/10] Fix buffer size larger than necessary --- Engine/source/gfx/gfxDrawUtil.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Engine/source/gfx/gfxDrawUtil.cpp b/Engine/source/gfx/gfxDrawUtil.cpp index b04791d1c..1e677685e 100644 --- a/Engine/source/gfx/gfxDrawUtil.cpp +++ b/Engine/source/gfx/gfxDrawUtil.cpp @@ -148,7 +148,7 @@ U32 GFXDrawUtil::drawTextN( GFont *font, const Point2I &ptDraw, const UTF8 *in_s // Convert to UTF16 temporarily. n++; // space for null terminator - FrameTemp ubuf( n * sizeof(UTF16) ); + FrameTemp ubuf( n ); convertUTF8toUTF16N(in_string, ubuf, n); return drawTextN( font, ptDraw, ubuf, n, colorTable, maxColorIndex, rot ); From 6e45643b28e1270f9060f4c1740aa6aa0b0b0341 Mon Sep 17 00:00:00 2001 From: Ben Payne Date: Tue, 20 Jan 2015 21:29:44 -0500 Subject: [PATCH 06/10] Add descriptions --- Engine/source/core/strings/unicode.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Engine/source/core/strings/unicode.h b/Engine/source/core/strings/unicode.h index a1293b8ee..737a8c500 100644 --- a/Engine/source/core/strings/unicode.h +++ b/Engine/source/core/strings/unicode.h @@ -83,12 +83,14 @@ U32 convertUTF8toUTF16N(const UTF8 *unistring, UTF16 *outbuffer, U32 len); U32 convertUTF16toUTF8N( const UTF16 *unistring, UTF8 *outbuffer, U32 len); +/// Safe conversion function for statically sized buffers. template inline U32 convertUTF8toUTF16(const UTF8 *unistring, UTF16 (&outbuffer)[N]) { return convertUTF8toUTF16N(unistring, outbuffer, (U32) N); } +/// Safe conversion function for statically sized buffers. template inline U32 convertUTF16toUTF8(const UTF16 *unistring, UTF8 (&outbuffer)[N]) { From d669eb6ee7434874f7e01330671b3fdf69c57f9c Mon Sep 17 00:00:00 2001 From: Ben Payne Date: Fri, 23 Jan 2015 15:20:13 -0500 Subject: [PATCH 07/10] Fix unintentionally deleted param --- Engine/source/platformWin32/winFileio.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Engine/source/platformWin32/winFileio.cpp b/Engine/source/platformWin32/winFileio.cpp index 764eabe20..81b6142df 100644 --- a/Engine/source/platformWin32/winFileio.cpp +++ b/Engine/source/platformWin32/winFileio.cpp @@ -1216,7 +1216,7 @@ void Platform::getVolumeInformationList( Vector& out_rVolumeI convertUTF16toUTF8( lpszFileSystem, buf ); info.FileSystem = StringTable->insert( buf ); - convertUTF16toUTF8( lpszVolumeName ); + convertUTF16toUTF8( lpszVolumeName, buf ); info.Name = StringTable->insert( buf ); #else info.FileSystem = StringTable->insert( lpszFileSystem ); From fcf52fb5e0d6e00ab5998af68081c4a16d1a53e6 Mon Sep 17 00:00:00 2001 From: Ben Payne Date: Fri, 23 Jan 2015 16:09:01 -0500 Subject: [PATCH 08/10] Rename the memory allocating versions to make prev error less likely The behavior is different enough that these shouldn't be overloaded with the non-allocating verions. Also makes it more obvious what is going on to the caller. --- Engine/source/core/stringBuffer.cpp | 6 +++--- Engine/source/core/strings/unicode.cpp | 4 ++-- Engine/source/core/strings/unicode.h | 4 ++-- Engine/source/core/util/str.cpp | 4 ++-- Engine/source/core/util/test/strTest.cpp | 2 +- Engine/source/gui/controls/guiTextEditCtrl.cpp | 2 +- Engine/source/platformWin32/nativeDialogs/win32MsgBox.cpp | 4 ++-- Engine/source/platformWin32/winFileio.cpp | 6 +++--- Engine/source/windowManager/win32/win32CursorController.cpp | 2 +- 9 files changed, 17 insertions(+), 17 deletions(-) diff --git a/Engine/source/core/stringBuffer.cpp b/Engine/source/core/stringBuffer.cpp index 1706de9dc..3d15a6183 100644 --- a/Engine/source/core/stringBuffer.cpp +++ b/Engine/source/core/stringBuffer.cpp @@ -186,7 +186,7 @@ void StringBuffer::append(const UTF8* in) // convert to UTF16, because that's our internal format. // if the conversion fails, exit. - UTF16* tmp = convertUTF8toUTF16(in); + UTF16* tmp = createUTF16string(in); AssertFatal(tmp, "StringBuffer::append(UTF8) - could not convert UTF8 string!"); if(!tmp) return; @@ -231,7 +231,7 @@ void StringBuffer::insert(const U32 charOffset, const UTF8* in) // convert to UTF16, because that's our internal format. // if the conversion fails, exit. - UTF16* tmp = convertUTF8toUTF16(in); + UTF16* tmp = createUTF16string(in); AssertFatal(tmp, "StringBuffer::insert(UTF8) - could not convert UTF8 string!"); if(!tmp) return; @@ -377,7 +377,7 @@ UTF8* StringBuffer::createCopy8() const incRequestCount8(); // convert will create a buffer of the appropriate size for a null terminated // input string. - UTF8* out = convertUTF16toUTF8(mBuffer.address()); + UTF8* out = createUTF8string(mBuffer.address()); return out; } diff --git a/Engine/source/core/strings/unicode.cpp b/Engine/source/core/strings/unicode.cpp index 3db038aa3..984168653 100644 --- a/Engine/source/core/strings/unicode.cpp +++ b/Engine/source/core/strings/unicode.cpp @@ -242,7 +242,7 @@ U32 convertUTF16toUTF8DoubleNULL( const UTF16 *unistring, UTF8 *outbuffer, U32 //----------------------------------------------------------------------------- // Functions that convert buffers of unicode code points //----------------------------------------------------------------------------- -UTF16* convertUTF8toUTF16( const UTF8* unistring) +UTF16* createUTF16string( const UTF8* unistring) { PROFILE_SCOPE(convertUTF8toUTF16_create); @@ -264,7 +264,7 @@ UTF16* convertUTF8toUTF16( const UTF8* unistring) } //----------------------------------------------------------------------------- -UTF8* convertUTF16toUTF8( const UTF16* unistring) +UTF8* createUTF8string( const UTF16* unistring) { PROFILE_SCOPE(convertUTF16toUTF8_create); diff --git a/Engine/source/core/strings/unicode.h b/Engine/source/core/strings/unicode.h index 737a8c500..ec26d1c91 100644 --- a/Engine/source/core/strings/unicode.h +++ b/Engine/source/core/strings/unicode.h @@ -62,9 +62,9 @@ /// calling delete[] on these buffers. /// - Because they allocate memory, do not use these functions in a tight loop. /// - These are useful when you need a new long term copy of a string. -UTF16* convertUTF8toUTF16( const UTF8 *unistring); +UTF16* createUTF16string( const UTF8 *unistring); -UTF8* convertUTF16toUTF8( const UTF16 *unistring); +UTF8* createUTF8string( const UTF16 *unistring); //----------------------------------------------------------------------------- /// Functions that convert buffers of unicode code points, into a provided buffer. diff --git a/Engine/source/core/util/str.cpp b/Engine/source/core/util/str.cpp index 3f4f3d8f9..d9d3c3ec7 100644 --- a/Engine/source/core/util/str.cpp +++ b/Engine/source/core/util/str.cpp @@ -346,7 +346,7 @@ class String::StringData : protected StringDataImpl { // Do this atomically to protect interned strings. - UTF16* utf16 = convertUTF8toUTF16( mData ); + UTF16* utf16 = createUTF16string( mData ); if( !dCompareAndSwap( mUTF16,( UTF16* ) NULL, utf16 ) ) delete [] utf16; } @@ -580,7 +580,7 @@ String::String(const UTF16 *str) if( str && str[ 0 ] ) { - UTF8* utf8 = convertUTF16toUTF8( str ); + UTF8* utf8 = createUTF8string( str ); U32 len = dStrlen( utf8 ); _string = new ( len ) StringData( utf8 ); delete [] utf8; diff --git a/Engine/source/core/util/test/strTest.cpp b/Engine/source/core/util/test/strTest.cpp index 3bb381d56..5658f2aa0 100644 --- a/Engine/source/core/util/test/strTest.cpp +++ b/Engine/source/core/util/test/strTest.cpp @@ -44,7 +44,7 @@ protected: : mData( str ), mLength( str ? dStrlen( str ) : 0 ), mUTF16( NULL ) { if( str ) - mUTF16 = convertUTF8toUTF16( mData ); + mUTF16 = createUTF16string( mData ); } ~StrTest() { diff --git a/Engine/source/gui/controls/guiTextEditCtrl.cpp b/Engine/source/gui/controls/guiTextEditCtrl.cpp index c451400b7..450be5280 100644 --- a/Engine/source/gui/controls/guiTextEditCtrl.cpp +++ b/Engine/source/gui/controls/guiTextEditCtrl.cpp @@ -279,7 +279,7 @@ void GuiTextEditCtrl::setText( const UTF16* txt) { if(txt && txt[0] != 0) { - UTF8* txt8 = convertUTF16toUTF8( txt ); + UTF8* txt8 = createUTF8string( txt ); Parent::setText( txt8 ); delete[] txt8; mTextBuffer.set( txt ); diff --git a/Engine/source/platformWin32/nativeDialogs/win32MsgBox.cpp b/Engine/source/platformWin32/nativeDialogs/win32MsgBox.cpp index 9fb6948f8..422800415 100644 --- a/Engine/source/platformWin32/nativeDialogs/win32MsgBox.cpp +++ b/Engine/source/platformWin32/nativeDialogs/win32MsgBox.cpp @@ -101,8 +101,8 @@ S32 Platform::messageBox(const UTF8 *title, const UTF8 *message, MBButtons butto pWindow->setCursorVisible(true); #ifdef UNICODE - const UTF16 *msg = convertUTF8toUTF16(message); - const UTF16 *t = convertUTF8toUTF16(title); + const UTF16 *msg = createUTF16string(message); + const UTF16 *t = createUTF16string(title); #else const UTF8 *msg = message; const UTF8 *t = title; diff --git a/Engine/source/platformWin32/winFileio.cpp b/Engine/source/platformWin32/winFileio.cpp index 81b6142df..36900cd15 100644 --- a/Engine/source/platformWin32/winFileio.cpp +++ b/Engine/source/platformWin32/winFileio.cpp @@ -802,7 +802,7 @@ StringTableEntry Platform::getCurrentDirectory() forwardslash( buf ); #ifdef UNICODE - char* utf8 = convertUTF16toUTF8( buf ); + char* utf8 = createUTF8string( buf ); StringTableEntry result = StringTable->insert( utf8 ); SAFE_DELETE_ARRAY( utf8 ); return result; @@ -847,8 +847,8 @@ static void getExecutableInfo( StringTableEntry* path, StringTableEntry* exe ) if( delimiter ) *delimiter = '\0'; - char* pathBuf = convertUTF16toUTF8( cen_buf ); - char* exeBuf = convertUTF16toUTF8( delimiter + 1 ); + char* pathBuf = createUTF8string( cen_buf ); + char* exeBuf = createUTF8string( delimiter + 1 ); pathEntry = StringTable->insert( pathBuf ); exeEntry = StringTable->insert( exeBuf ); diff --git a/Engine/source/windowManager/win32/win32CursorController.cpp b/Engine/source/windowManager/win32/win32CursorController.cpp index 9b1b1eb3f..ca8a12563 100644 --- a/Engine/source/windowManager/win32/win32CursorController.cpp +++ b/Engine/source/windowManager/win32/win32CursorController.cpp @@ -150,7 +150,7 @@ static HCURSOR gCursorShape = NULL; void Win32CursorController::setCursorShape( const UTF8 *fileName, bool reload ) { #ifdef UNICODE - const UTF16 *lFileName = convertUTF8toUTF16( fileName ); + const UTF16 *lFileName = createUTF16string( fileName ); #else const UTF8 *lFileName = fileName; #endif From ae94fc4e46a1f078ff158575d06cb489d3fda6bd Mon Sep 17 00:00:00 2001 From: Ben Payne Date: Fri, 23 Jan 2015 16:10:20 -0500 Subject: [PATCH 09/10] Fix mem leak discovered during previous commit --- Engine/source/windowManager/win32/win32CursorController.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Engine/source/windowManager/win32/win32CursorController.cpp b/Engine/source/windowManager/win32/win32CursorController.cpp index ca8a12563..ba19b0979 100644 --- a/Engine/source/windowManager/win32/win32CursorController.cpp +++ b/Engine/source/windowManager/win32/win32CursorController.cpp @@ -160,6 +160,10 @@ void Win32CursorController::setCursorShape( const UTF8 *fileName, bool reload ) if ( gCursorShape ) SetCursor( gCursorShape ); + +#ifdef UNICODE + delete[] lFileName; +#endif } // Console function to set the current cursor shape given the cursor shape From 47950382f76cc8d7f0f4aafcc3454b7dafa53484 Mon Sep 17 00:00:00 2001 From: Ben Payne Date: Fri, 23 Jan 2015 17:25:17 -0500 Subject: [PATCH 10/10] ...and update the profiler strings --- Engine/source/core/strings/unicode.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Engine/source/core/strings/unicode.cpp b/Engine/source/core/strings/unicode.cpp index 984168653..4496da76b 100644 --- a/Engine/source/core/strings/unicode.cpp +++ b/Engine/source/core/strings/unicode.cpp @@ -244,7 +244,7 @@ U32 convertUTF16toUTF8DoubleNULL( const UTF16 *unistring, UTF8 *outbuffer, U32 //----------------------------------------------------------------------------- UTF16* createUTF16string( const UTF8* unistring) { - PROFILE_SCOPE(convertUTF8toUTF16_create); + PROFILE_SCOPE(createUTF16string); // allocate plenty of memory. U32 nCodepoints, len = dStrlen(unistring) + 1; @@ -266,7 +266,7 @@ UTF16* createUTF16string( const UTF8* unistring) //----------------------------------------------------------------------------- UTF8* createUTF8string( const UTF16* unistring) { - PROFILE_SCOPE(convertUTF16toUTF8_create); + PROFILE_SCOPE(createUTF8string); // allocate plenty of memory. U32 nCodeunits, len = dStrlen(unistring) * 3 + 1;