fix potential memleak under Windows

This commit is contained in:
Arisotura 2019-03-30 12:33:10 +01:00
parent 60a728283e
commit 2a2e6a0c76
1 changed files with 6 additions and 3 deletions

View File

@ -197,7 +197,10 @@ FILE* OpenLocalFile(const char* path, const char* mode)
PWSTR appDataPath = NULL; PWSTR appDataPath = NULL;
SHGetKnownFolderPath(FOLDERID_RoamingAppData, 0, NULL, &appDataPath); SHGetKnownFolderPath(FOLDERID_RoamingAppData, 0, NULL, &appDataPath);
if (!appDataPath) if (!appDataPath)
{
delete[] emudirpath;
return NULL; return NULL;
}
// this will be more than enough // this will be more than enough
WCHAR fatperm[4]; WCHAR fatperm[4];
@ -207,16 +210,16 @@ FILE* OpenLocalFile(const char* path, const char* mode)
fatperm[3] = 0; fatperm[3] = 0;
int fnlen = MultiByteToWideChar(CP_UTF8, 0, path, -1, NULL, 0); int fnlen = MultiByteToWideChar(CP_UTF8, 0, path, -1, NULL, 0);
if (fnlen < 1) return NULL; if (fnlen < 1) { delete[] emudirpath; return NULL; }
WCHAR* wfileName = new WCHAR[fnlen]; WCHAR* wfileName = new WCHAR[fnlen];
int res = MultiByteToWideChar(CP_UTF8, 0, path, -1, wfileName, fnlen); int res = MultiByteToWideChar(CP_UTF8, 0, path, -1, wfileName, fnlen);
if (res != fnlen) { delete[] wfileName; return NULL; } // checkme? if (res != fnlen) { delete[] wfileName; delete[] emudirpath; return NULL; } // checkme?
const WCHAR* appdir = L"\\melonDS\\"; const WCHAR* appdir = L"\\melonDS\\";
int pos = wcslen(appDataPath); int pos = wcslen(appDataPath);
void* ptr = CoTaskMemRealloc(appDataPath, (pos+wcslen(appdir)+fnlen+1)*sizeof(WCHAR)); void* ptr = CoTaskMemRealloc(appDataPath, (pos+wcslen(appdir)+fnlen+1)*sizeof(WCHAR));
if (!ptr) { delete[] wfileName; return NULL; } // oh well if (!ptr) { delete[] wfileName; delete[] emudirpath; return NULL; } // oh well
appDataPath = (PWSTR)ptr; appDataPath = (PWSTR)ptr;
wcscpy(&appDataPath[pos], appdir); pos += wcslen(appdir); wcscpy(&appDataPath[pos], appdir); pos += wcslen(appdir);