From 30f2c2f3cdecc8536aed3f7d570876bc1f803d51 Mon Sep 17 00:00:00 2001 From: Cameron Cawley Date: Fri, 31 Jul 2020 11:26:19 +0100 Subject: [PATCH] WIN32: Use malloc instead of new in string conversion functions --- backends/dialogs/win32/win32-dialogs.cpp | 8 ++++---- backends/platform/sdl/win32/win32.cpp | 20 +++---------------- backends/platform/sdl/win32/win32_wrapper.cpp | 4 ++-- backends/taskbar/win32/win32-taskbar.cpp | 10 +++++----- .../windows/windows-text-to-speech.cpp | 2 +- 5 files changed, 15 insertions(+), 29 deletions(-) diff --git a/backends/dialogs/win32/win32-dialogs.cpp b/backends/dialogs/win32/win32-dialogs.cpp index bf4a79614a8..a8217ba7e0d 100644 --- a/backends/dialogs/win32/win32-dialogs.cpp +++ b/backends/dialogs/win32/win32-dialogs.cpp @@ -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 diff --git a/backends/platform/sdl/win32/win32.cpp b/backends/platform/sdl/win32/win32.cpp index 638174df557..1c67d0d601a 100644 --- a/backends/platform/sdl/win32/win32.cpp +++ b/backends/platform/sdl/win32/win32.cpp @@ -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; } } diff --git a/backends/platform/sdl/win32/win32_wrapper.cpp b/backends/platform/sdl/win32/win32_wrapper.cpp index 7efcb00aa17..31663f1b577 100644 --- a/backends/platform/sdl/win32/win32_wrapper.cpp +++ b/backends/platform/sdl/win32/win32_wrapper.cpp @@ -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; } diff --git a/backends/taskbar/win32/win32-taskbar.cpp b/backends/taskbar/win32/win32-taskbar.cpp index 1a03bbd5169..65429737160 100644 --- a/backends/taskbar/win32/win32-taskbar.cpp +++ b/backends/taskbar/win32/win32-taskbar.cpp @@ -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); } } diff --git a/backends/text-to-speech/windows/windows-text-to-speech.cpp b/backends/text-to-speech/windows/windows-text-to-speech.cpp index b2e8851d04a..59eb599a55e 100644 --- a/backends/text-to-speech/windows/windows-text-to-speech.cpp +++ b/backends/text-to-speech/windows/windows-text-to-speech.cpp @@ -362,7 +362,7 @@ void WindowsTextToSpeechManager::createVoice(void *cpVoiceToken) { if (SUCCEEDED(hr)) { buffer = Win32::unicodeToAnsi(descW); desc = buffer; - delete[] buffer; + free(buffer); CoTaskMemFree(descW); }