From a586f9daae17325334974043af97230f3ff45789 Mon Sep 17 00:00:00 2001 From: Ev1lbl0w Date: Wed, 10 Mar 2021 18:31:44 +0000 Subject: [PATCH] Replace malloc's with Godot's memalloc macro (cherry picked from commit 838e7d0a8d49ac5d2d149acf7afc6b909f640485) --- drivers/coreaudio/audio_driver_coreaudio.cpp | 30 +++++++++++-------- .../mono/mono_gd/support/android_support.cpp | 4 +-- modules/xatlas_unwrap/register_types.cpp | 3 ++ platform/android/java_godot_lib_jni.cpp | 10 ++++--- platform/windows/joypad_windows.cpp | 10 +++---- platform/windows/windows_terminal_logger.cpp | 5 ++-- 6 files changed, 37 insertions(+), 25 deletions(-) diff --git a/drivers/coreaudio/audio_driver_coreaudio.cpp b/drivers/coreaudio/audio_driver_coreaudio.cpp index eab4ea99e61..cd7b505516e 100644 --- a/drivers/coreaudio/audio_driver_coreaudio.cpp +++ b/drivers/coreaudio/audio_driver_coreaudio.cpp @@ -514,7 +514,8 @@ Array AudioDriverCoreAudio::_get_device_list(bool capture) { UInt32 size = 0; AudioObjectGetPropertyDataSize(kAudioObjectSystemObject, &prop, 0, NULL, &size); - AudioDeviceID *audioDevices = (AudioDeviceID *)malloc(size); + AudioDeviceID *audioDevices = (AudioDeviceID *)memalloc(size); + ERR_FAIL_NULL_V_MSG(audioDevices, list, "Out of memory."); AudioObjectGetPropertyData(kAudioObjectSystemObject, &prop, 0, NULL, &size, audioDevices); UInt32 deviceCount = size / sizeof(AudioDeviceID); @@ -523,14 +524,15 @@ Array AudioDriverCoreAudio::_get_device_list(bool capture) { prop.mSelector = kAudioDevicePropertyStreamConfiguration; AudioObjectGetPropertyDataSize(audioDevices[i], &prop, 0, NULL, &size); - AudioBufferList *bufferList = (AudioBufferList *)malloc(size); + AudioBufferList *bufferList = (AudioBufferList *)memalloc(size); + ERR_FAIL_NULL_V_MSG(bufferList, list, "Out of memory."); AudioObjectGetPropertyData(audioDevices[i], &prop, 0, NULL, &size, bufferList); UInt32 channelCount = 0; for (UInt32 j = 0; j < bufferList->mNumberBuffers; j++) channelCount += bufferList->mBuffers[j].mNumberChannels; - free(bufferList); + memfree(bufferList); if (channelCount >= 1) { CFStringRef cfname; @@ -542,17 +544,18 @@ Array AudioDriverCoreAudio::_get_device_list(bool capture) { CFIndex length = CFStringGetLength(cfname); CFIndex maxSize = CFStringGetMaximumSizeForEncoding(length, kCFStringEncodingUTF8) + 1; - char *buffer = (char *)malloc(maxSize); + char *buffer = (char *)memalloc(maxSize); + ERR_FAIL_NULL_V_MSG(buffer, list, "Out of memory."); if (CFStringGetCString(cfname, buffer, maxSize, kCFStringEncodingUTF8)) { // Append the ID to the name in case we have devices with duplicate name list.push_back(String(buffer) + " (" + itos(audioDevices[i]) + ")"); } - free(buffer); + memfree(buffer); } } - free(audioDevices); + memfree(audioDevices); return list; } @@ -570,7 +573,8 @@ void AudioDriverCoreAudio::_set_device(const String &device, bool capture) { UInt32 size = 0; AudioObjectGetPropertyDataSize(kAudioObjectSystemObject, &prop, 0, NULL, &size); - AudioDeviceID *audioDevices = (AudioDeviceID *)malloc(size); + AudioDeviceID *audioDevices = (AudioDeviceID *)memalloc(size); + ERR_FAIL_NULL_MSG(audioDevices, "Out of memory."); AudioObjectGetPropertyData(kAudioObjectSystemObject, &prop, 0, NULL, &size, audioDevices); UInt32 deviceCount = size / sizeof(AudioDeviceID); @@ -579,14 +583,15 @@ void AudioDriverCoreAudio::_set_device(const String &device, bool capture) { prop.mSelector = kAudioDevicePropertyStreamConfiguration; AudioObjectGetPropertyDataSize(audioDevices[i], &prop, 0, NULL, &size); - AudioBufferList *bufferList = (AudioBufferList *)malloc(size); + AudioBufferList *bufferList = (AudioBufferList *)memalloc(size); + ERR_FAIL_NULL_MSG(bufferList, "Out of memory."); AudioObjectGetPropertyData(audioDevices[i], &prop, 0, NULL, &size, bufferList); UInt32 channelCount = 0; for (UInt32 j = 0; j < bufferList->mNumberBuffers; j++) channelCount += bufferList->mBuffers[j].mNumberChannels; - free(bufferList); + memfree(bufferList); if (channelCount >= 1) { CFStringRef cfname; @@ -598,7 +603,8 @@ void AudioDriverCoreAudio::_set_device(const String &device, bool capture) { CFIndex length = CFStringGetLength(cfname); CFIndex maxSize = CFStringGetMaximumSizeForEncoding(length, kCFStringEncodingUTF8) + 1; - char *buffer = (char *)malloc(maxSize); + char *buffer = (char *)memalloc(maxSize); + ERR_FAIL_NULL_MSG(buffer, "Out of memory."); if (CFStringGetCString(cfname, buffer, maxSize, kCFStringEncodingUTF8)) { String name = String(buffer) + " (" + itos(audioDevices[i]) + ")"; if (name == device) { @@ -607,11 +613,11 @@ void AudioDriverCoreAudio::_set_device(const String &device, bool capture) { } } - free(buffer); + memfree(buffer); } } - free(audioDevices); + memfree(audioDevices); } if (!found) { diff --git a/modules/mono/mono_gd/support/android_support.cpp b/modules/mono/mono_gd/support/android_support.cpp index 5831dc80032..43885cd859e 100755 --- a/modules/mono/mono_gd/support/android_support.cpp +++ b/modules/mono/mono_gd/support/android_support.cpp @@ -416,8 +416,7 @@ GD_PINVOKE_EXPORT int32_t monodroid_get_system_property(const char *p_name, char if (r_value) { if (len >= 0) { *r_value = (char *)malloc(len + 1); - if (!*r_value) - return -1; + ERR_FAIL_NULL_V_MSG(*r_value, -1, "Out of memory."); memcpy(*r_value, prop_value_str, len); (*r_value)[len] = '\0'; } else { @@ -638,6 +637,7 @@ GD_PINVOKE_EXPORT int32_t _monodroid_get_dns_servers(void **r_dns_servers_array) if (dns_servers_count > 0) { size_t ret_size = sizeof(char *) * (size_t)dns_servers_count; *r_dns_servers_array = malloc(ret_size); // freed by the BCL + ERR_FAIL_NULL_MSG(*r_dns_servers_array, "Out of memory."); memcpy(*r_dns_servers_array, dns_servers, ret_size); } diff --git a/modules/xatlas_unwrap/register_types.cpp b/modules/xatlas_unwrap/register_types.cpp index d198acf92d7..0920696897f 100644 --- a/modules/xatlas_unwrap/register_types.cpp +++ b/modules/xatlas_unwrap/register_types.cpp @@ -85,8 +85,11 @@ bool xatlas_mesh_lightmap_unwrap_callback(float p_texel_size, const float *p_ver const xatlas::Mesh &output = atlas->meshes[0]; *r_vertex = (int *)malloc(sizeof(int) * output.vertexCount); + ERR_FAIL_NULL_V_MSG(*r_vertex, false, "Out of memory."); *r_uv = (float *)malloc(sizeof(float) * output.vertexCount * 2); + ERR_FAIL_NULL_V_MSG(*r_uv, false, "Out of memory."); *r_index = (int *)malloc(sizeof(int) * output.indexCount); + ERR_FAIL_NULL_V_MSG(*r_index, false, "Out of memory."); float max_x = 0; float max_y = 0; diff --git a/platform/android/java_godot_lib_jni.cpp b/platform/android/java_godot_lib_jni.cpp index cd3e2c1cbdd..aab6925cae8 100644 --- a/platform/android/java_godot_lib_jni.cpp +++ b/platform/android/java_godot_lib_jni.cpp @@ -172,9 +172,11 @@ JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_setup(JNIEnv *env, jc if (p_cmdline) { cmdlen = env->GetArrayLength(p_cmdline); if (cmdlen) { - cmdline = (const char **)malloc((cmdlen + 1) * sizeof(const char *)); + cmdline = (const char **)memalloc((cmdlen + 1) * sizeof(const char *)); + ERR_FAIL_NULL_MSG(cmdline, "Out of memory."); cmdline[cmdlen] = NULL; - j_cmdline = (jstring *)malloc(cmdlen * sizeof(jstring)); + j_cmdline = (jstring *)memalloc(cmdlen * sizeof(jstring)); + ERR_FAIL_NULL_MSG(j_cmdline, "Out of memory."); for (int i = 0; i < cmdlen; i++) { @@ -193,9 +195,9 @@ JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_setup(JNIEnv *env, jc for (int i = 0; i < cmdlen; ++i) { env->ReleaseStringUTFChars(j_cmdline[i], cmdline[i]); } - free(j_cmdline); + memfree(j_cmdline); } - free(cmdline); + memfree(cmdline); } if (err != OK) { diff --git a/platform/windows/joypad_windows.cpp b/platform/windows/joypad_windows.cpp index d75792882f7..b0ddad9ecb2 100644 --- a/platform/windows/joypad_windows.cpp +++ b/platform/windows/joypad_windows.cpp @@ -115,11 +115,11 @@ bool JoypadWindows::is_xinput_device(const GUID *p_guid) { if (GetRawInputDeviceList(NULL, &dev_list_count, sizeof(RAWINPUTDEVICELIST)) == (UINT)-1) { return false; } - dev_list = (PRAWINPUTDEVICELIST)malloc(sizeof(RAWINPUTDEVICELIST) * dev_list_count); - if (!dev_list) return false; + dev_list = (PRAWINPUTDEVICELIST)memalloc(sizeof(RAWINPUTDEVICELIST) * dev_list_count); + ERR_FAIL_NULL_V_MSG(dev_list, false, "Out of memory."); if (GetRawInputDeviceList(dev_list, &dev_list_count, sizeof(RAWINPUTDEVICELIST)) == (UINT)-1) { - free(dev_list); + memfree(dev_list); return false; } for (unsigned int i = 0; i < dev_list_count; i++) { @@ -136,11 +136,11 @@ bool JoypadWindows::is_xinput_device(const GUID *p_guid) { (GetRawInputDeviceInfoA(dev_list[i].hDevice, RIDI_DEVICENAME, &dev_name, &nameSize) != (UINT)-1) && (strstr(dev_name, "IG_") != NULL)) { - free(dev_list); + memfree(dev_list); return true; } } - free(dev_list); + memfree(dev_list); return false; } diff --git a/platform/windows/windows_terminal_logger.cpp b/platform/windows/windows_terminal_logger.cpp index f6319c2965d..0323f72854b 100644 --- a/platform/windows/windows_terminal_logger.cpp +++ b/platform/windows/windows_terminal_logger.cpp @@ -53,7 +53,8 @@ void WindowsTerminalLogger::logv(const char *p_format, va_list p_list, bool p_er if (wlen < 0) return; - wchar_t *wbuf = (wchar_t *)malloc((len + 1) * sizeof(wchar_t)); + wchar_t *wbuf = (wchar_t *)memalloc((len + 1) * sizeof(wchar_t)); + ERR_FAIL_NULL_MSG(wbuf, "Out of memory."); MultiByteToWideChar(CP_UTF8, 0, buf, len, wbuf, wlen); wbuf[wlen] = 0; @@ -62,7 +63,7 @@ void WindowsTerminalLogger::logv(const char *p_format, va_list p_list, bool p_er else wprintf(L"%ls", wbuf); - free(wbuf); + memfree(wbuf); #ifdef DEBUG_ENABLED fflush(stdout);