WIN32: Use malloc instead of new in string conversion functions

This commit is contained in:
Cameron Cawley 2020-07-31 11:26:19 +01:00 committed by Eugene Sandulenko
parent 27a2b94687
commit 30f2c2f3cd
5 changed files with 15 additions and 29 deletions

View file

@ -95,7 +95,7 @@ HRESULT getShellPath(IShellItem *item, Common::String &path) {
char *str = Win32::unicodeToAnsi(name);
path = Common::String(str);
CoTaskMemFree(name);
delete[] str;
free(str);
}
return hr;
}
@ -132,11 +132,11 @@ Common::DialogManager::DialogResult Win32DialogManager::showFileBrowser(const ch
LPWSTR str = Win32::ansiToUnicode(title, Win32::getCurrentCharset());
hr = dialog->SetTitle(str);
delete[] str;
free(str);
str = Win32::ansiToUnicode(_("Choose"), Win32::getCurrentCharset());
hr = dialog->SetOkButtonLabel(str);
delete[] str;
free(str);
if (ConfMan.hasKey("browser_lastpath")) {
str = Win32::ansiToUnicode(ConfMan.get("browser_lastpath").c_str());
@ -145,7 +145,7 @@ Common::DialogManager::DialogResult Win32DialogManager::showFileBrowser(const ch
if (SUCCEEDED(hr)) {
hr = dialog->SetDefaultFolder(item);
}
delete[] str;
free(str);
}
// Show dialog

View file

@ -474,18 +474,12 @@ char *OSystem_Win32::convertEncoding(const char* to, const char *from, const cha
}
memcpy(tmpStr, string, length);
} else {
// Win32::ansiToUnicode uses new to allocate the memory. We need to copy it into an array
// allocated with malloc as it is going to be freed using free.
WCHAR *tmpStr2 = Win32::ansiToUnicode(string, Win32::getCodePageId(from));
if (!tmpStr2) {
tmpStr = Win32::ansiToUnicode(string, Win32::getCodePageId(from));
if (!tmpStr) {
if (newString != nullptr)
free(newString);
return nullptr;
}
size_t size = wcslen(tmpStr2) + 1; // +1 for the terminating null wchar
tmpStr = (WCHAR *) malloc(sizeof(WCHAR) * size);
memcpy(tmpStr, tmpStr2, sizeof(WCHAR) * size);
delete[] tmpStr2;
}
if (newString != nullptr)
@ -501,15 +495,7 @@ char *OSystem_Win32::convertEncoding(const char* to, const char *from, const cha
} else {
result = Win32::unicodeToAnsi(tmpStr, Win32::getCodePageId(to));
free(tmpStr);
if (!result)
return nullptr;
// Win32::unicodeToAnsi uses new to allocate the memory. We need to copy it into an array
// allocated with malloc as it is going to be freed using free.
size_t size = strlen(result) + 1;
char *resultCopy = (char *) malloc(sizeof(char) * size);
memcpy(resultCopy, result, sizeof(char) * size);
delete[] result;
return resultCopy;
return result;
}
}

View file

@ -85,7 +85,7 @@ wchar_t *ansiToUnicode(const char *s, uint codePage) {
DWORD size = MultiByteToWideChar(codePage, 0, s, -1, NULL, 0);
if (size > 0) {
LPWSTR result = new WCHAR[size];
LPWSTR result = (LPWSTR)calloc(size, sizeof(WCHAR));
if (MultiByteToWideChar(codePage, 0, s, -1, result, size) != 0)
return result;
}
@ -97,7 +97,7 @@ char *unicodeToAnsi(const wchar_t *s, uint codePage) {
DWORD size = WideCharToMultiByte(codePage, 0, s, -1, NULL, 0, 0, 0);
if (size > 0) {
char *result = new char[size];
char *result = (char *)calloc(size, sizeof(char));
if (WideCharToMultiByte(codePage, 0, s, -1, result, size, 0, 0) != 0)
return result;
}

View file

@ -136,7 +136,7 @@ void Win32TaskbarManager::setOverlayIcon(const Common::String &name, const Commo
DestroyIcon(pIcon);
delete[] desc;
free(desc);
}
void Win32TaskbarManager::setProgressValue(int completed, int total) {
@ -267,7 +267,7 @@ void Win32TaskbarManager::setCount(int count) {
// Sets the overlay icon
LPWSTR desc = Win32::ansiToUnicode(Common::String::format("Found games: %d", count).c_str());
_taskbar->SetOverlayIcon(_window->getHwnd(), _icon, desc);
delete[] desc;
free(desc);
}
void Win32TaskbarManager::addRecent(const Common::String &name, const Common::String &description) {
@ -301,7 +301,7 @@ void Win32TaskbarManager::addRecent(const Common::String &name, const Common::St
link->SetIconLocation(icon, 0);
delete[] icon;
free(icon);
}
// The link's display name must be set via property store.
@ -321,8 +321,8 @@ void Win32TaskbarManager::addRecent(const Common::String &name, const Common::St
// SHAddToRecentDocs will cause the games to be added to the Recent list, allowing the user to pin them.
SHAddToRecentDocs(SHARD_LINK, link);
link->Release();
delete[] game;
delete[] desc;
free(game);
free(desc);
}
}

View file

@ -362,7 +362,7 @@ void WindowsTextToSpeechManager::createVoice(void *cpVoiceToken) {
if (SUCCEEDED(hr)) {
buffer = Win32::unicodeToAnsi(descW);
desc = buffer;
delete[] buffer;
free(buffer);
CoTaskMemFree(descW);
}