From 30bcb0f54223cf915ac923cca73ebf4ba2b09e48 Mon Sep 17 00:00:00 2001 From: Eddy Jansson Date: Sun, 19 Oct 2025 11:07:48 +0200 Subject: [PATCH 1/2] Don't do NULL-checks before SDL_free() Replaces the pattern if (ptr) { SDL_free(ptr); } with SDL_free(ptr); --- src/audio/openslES/SDL_openslES.c | 8 ++----- src/audio/pipewire/SDL_pipewire.c | 8 ++----- src/core/linux/SDL_evdev_kbd.c | 4 +--- src/core/linux/SDL_ibus.c | 4 +--- src/dialog/unix/SDL_zenitymessagebox.c | 4 +--- src/dialog/windows/SDL_windowsdialog.c | 20 ++++------------ src/gpu/d3d12/SDL_gpu_d3d12.c | 16 ++++--------- src/gpu/vulkan/SDL_gpu_vulkan.c | 8 ++----- src/hidapi/SDL_hidapi.c | 4 +--- src/io/SDL_iostream.c | 4 +--- src/joystick/SDL_joystick.c | 4 +--- src/joystick/hidapi/SDL_hidapi_combined.c | 8 ++----- src/joystick/hidapi/SDL_hidapi_gip.c | 20 ++++------------ src/joystick/linux/SDL_sysjoystick.c | 8 ++----- src/joystick/windows/SDL_rawinputjoystick.c | 16 ++++--------- .../windows/SDL_windows_gaming_input.c | 4 +--- src/render/opengl/SDL_render_gl.c | 4 +--- src/render/opengles2/SDL_render_gles2.c | 4 +--- src/stdlib/SDL_getenv.c | 4 +--- src/test/SDL_test_common.c | 4 +--- src/thread/n3ds/SDL_sysmutex.c | 4 +--- src/tray/unix/SDL_tray.c | 4 +--- src/video/SDL_video.c | 4 +--- src/video/SDL_yuv.c | 8 ++----- src/video/kmsdrm/SDL_kmsdrmmouse.c | 16 ++++--------- src/video/kmsdrm/SDL_kmsdrmvideo.c | 8 ++----- src/video/kmsdrm/SDL_kmsdrmvulkan.c | 24 +++++-------------- src/video/openvr/SDL_openvrvideo.c | 4 +--- src/video/vita/SDL_vitavideo.c | 5 +--- src/video/wayland/SDL_waylanddatamanager.c | 12 +++------- src/video/windows/SDL_windowskeyboard.c | 4 +--- src/video/windows/SDL_windowswindow.c | 8 ++----- src/video/x11/SDL_x11clipboard.c | 4 +--- src/video/x11/SDL_x11keyboard.c | 8 ++----- src/video/x11/SDL_x11messagebox.c | 4 +--- src/video/x11/SDL_x11toolkit.c | 8 ++----- test/gamepadutils.c | 4 +--- test/testcontroller.c | 8 ++----- test/testffmpeg_vulkan.c | 8 ++----- test/testutils.c | 4 +--- 40 files changed, 76 insertions(+), 229 deletions(-) diff --git a/src/audio/openslES/SDL_openslES.c b/src/audio/openslES/SDL_openslES.c index e58ab8b048d42..62947fcc2622d 100644 --- a/src/audio/openslES/SDL_openslES.c +++ b/src/audio/openslES/SDL_openslES.c @@ -223,9 +223,7 @@ static void OPENSLES_DestroyPCMRecorder(SDL_AudioDevice *device) audiodata->playsem = NULL; } - if (audiodata->mixbuff) { - SDL_free(audiodata->mixbuff); - } + SDL_free(audiodata->mixbuff); } // !!! FIXME: make this non-blocking! @@ -419,9 +417,7 @@ static void OPENSLES_DestroyPCMPlayer(SDL_AudioDevice *device) audiodata->playsem = NULL; } - if (audiodata->mixbuff) { - SDL_free(audiodata->mixbuff); - } + SDL_free(audiodata->mixbuff); } static bool OPENSLES_CreatePCMPlayer(SDL_AudioDevice *device) diff --git a/src/audio/pipewire/SDL_pipewire.c b/src/audio/pipewire/SDL_pipewire.c index 2257263137f12..eb3432ac7b2d0 100644 --- a/src/audio/pipewire/SDL_pipewire.c +++ b/src/audio/pipewire/SDL_pipewire.c @@ -635,16 +635,12 @@ static int metadata_property(void *object, Uint32 subject, const char *key, cons if (subject == PW_ID_CORE && key && value) { if (!SDL_strcmp(key, "default.audio.sink")) { - if (pipewire_default_sink_id) { - SDL_free(pipewire_default_sink_id); - } + SDL_free(pipewire_default_sink_id); pipewire_default_sink_id = get_name_from_json(value); node->persist = true; change_default_device(pipewire_default_sink_id); } else if (!SDL_strcmp(key, "default.audio.source")) { - if (pipewire_default_source_id) { - SDL_free(pipewire_default_source_id); - } + SDL_free(pipewire_default_source_id); pipewire_default_source_id = get_name_from_json(value); node->persist = true; change_default_device(pipewire_default_source_id); diff --git a/src/core/linux/SDL_evdev_kbd.c b/src/core/linux/SDL_evdev_kbd.c index 32340bff20810..c4e1aa4ae97a6 100644 --- a/src/core/linux/SDL_evdev_kbd.c +++ b/src/core/linux/SDL_evdev_kbd.c @@ -534,9 +534,7 @@ void SDL_EVDEV_kbd_quit(SDL_EVDEV_keyboard_state *state) if (state->key_maps && state->key_maps != default_key_maps) { int i; for (i = 0; i < MAX_NR_KEYMAPS; ++i) { - if (state->key_maps[i]) { - SDL_free(state->key_maps[i]); - } + SDL_free(state->key_maps[i]); } SDL_free(state->key_maps); } diff --git a/src/core/linux/SDL_ibus.c b/src/core/linux/SDL_ibus.c index ea58ed577875b..bc9add4eab670 100644 --- a/src/core/linux/SDL_ibus.c +++ b/src/core/linux/SDL_ibus.c @@ -563,9 +563,7 @@ bool SDL_IBus_Init(void) return false; } - if (ibus_addr_file) { - SDL_free(ibus_addr_file); - } + SDL_free(ibus_addr_file); ibus_addr_file = SDL_strdup(addr_file); if (inotify_fd < 0) { diff --git a/src/dialog/unix/SDL_zenitymessagebox.c b/src/dialog/unix/SDL_zenitymessagebox.c index 39d44f8517cb6..3638416ea5500 100644 --- a/src/dialog/unix/SDL_zenitymessagebox.c +++ b/src/dialog/unix/SDL_zenitymessagebox.c @@ -161,9 +161,7 @@ bool SDL_Zenity_ShowMessageBox(const SDL_MessageBoxData *messageboxdata, int *bu if (buttonID) { char *output = SDL_ReadProcess(process, NULL, &exit_code); if (exit_code < 0 || exit_code == 255) { - if (output) { - SDL_free(output); - } + SDL_free(output); } else if (output) { // It likes to add a newline... char *tmp = SDL_strrchr(output, '\n'); diff --git a/src/dialog/windows/SDL_windowsdialog.c b/src/dialog/windows/SDL_windowsdialog.c index b10f82fabb684..b25282417526f 100644 --- a/src/dialog/windows/SDL_windowsdialog.c +++ b/src/dialog/windows/SDL_windowsdialog.c @@ -731,25 +731,15 @@ bool windows_ShowModernFileFolderDialog(SDL_FileDialogType dialog_type, const ch // default_file_w is a pointer into default_folder_w. if (default_folder_w) { SDL_free(default_folder_w); - } else if (default_file_w) { - SDL_free(default_file_w); - } + } else SDL_free(default_file_w); - if (title_w) { - SDL_free(title_w); - } + SDL_free(title_w); - if (accept_w) { - SDL_free(accept_w); - } + SDL_free(accept_w); - if (cancel_w) { - SDL_free(cancel_w); - } + SDL_free(cancel_w); - if (filter_data) { - SDL_free(filter_data); - } + SDL_free(filter_data); if (files) { for (char** files_ptr = files; *files_ptr; files_ptr++) { diff --git a/src/gpu/d3d12/SDL_gpu_d3d12.c b/src/gpu/d3d12/SDL_gpu_d3d12.c index b1b2de3605777..985a82eee1f6c 100644 --- a/src/gpu/d3d12/SDL_gpu_d3d12.c +++ b/src/gpu/d3d12/SDL_gpu_d3d12.c @@ -1349,9 +1349,7 @@ static void D3D12_INTERNAL_ReleaseBufferContainer( } // Containers are just client handles, so we can free immediately - if (container->debugName) { - SDL_free(container->debugName); - } + SDL_free(container->debugName); SDL_free(container->buffers); SDL_free(container); @@ -1426,9 +1424,7 @@ static void D3D12_INTERNAL_ReleaseTextureContainer( SDL_DestroyProperties(container->header.info.props); // Containers are just client handles, so we can destroy immediately - if (container->debugName) { - SDL_free(container->debugName); - } + SDL_free(container->debugName); SDL_free(container->textures); SDL_free(container); @@ -2072,9 +2068,7 @@ static void D3D12_SetBufferName( D3D12BufferContainer *container = (D3D12BufferContainer *)buffer; if (renderer->debug_mode && text != NULL) { - if (container->debugName != NULL) { - SDL_free(container->debugName); - } + SDL_free(container->debugName); container->debugName = SDL_strdup(text); @@ -2096,9 +2090,7 @@ static void D3D12_SetTextureName( D3D12TextureContainer *container = (D3D12TextureContainer *)texture; if (renderer->debug_mode && text != NULL) { - if (container->debugName != NULL) { - SDL_free(container->debugName); - } + SDL_free(container->debugName); container->debugName = SDL_strdup(text); diff --git a/src/gpu/vulkan/SDL_gpu_vulkan.c b/src/gpu/vulkan/SDL_gpu_vulkan.c index 8798d1ee59135..c7e7af50272d6 100644 --- a/src/gpu/vulkan/SDL_gpu_vulkan.c +++ b/src/gpu/vulkan/SDL_gpu_vulkan.c @@ -4958,9 +4958,7 @@ static void VULKAN_DestroyDevice( j); } - if (renderer->memoryAllocator->subAllocators[i].allocations != NULL) { - SDL_free(renderer->memoryAllocator->subAllocators[i].allocations); - } + SDL_free(renderer->memoryAllocator->subAllocators[i].allocations); SDL_free(renderer->memoryAllocator->subAllocators[i].sortedFreeRegions); } @@ -6987,9 +6985,7 @@ static void VULKAN_ReleaseTexture( SDL_DestroyProperties(vulkanTextureContainer->header.info.props); // Containers are just client handles, so we can destroy immediately - if (vulkanTextureContainer->debugName != NULL) { - SDL_free(vulkanTextureContainer->debugName); - } + SDL_free(vulkanTextureContainer->debugName); SDL_free(vulkanTextureContainer->textures); SDL_free(vulkanTextureContainer); diff --git a/src/hidapi/SDL_hidapi.c b/src/hidapi/SDL_hidapi.c index 275a12553a8cf..515446e93f942 100644 --- a/src/hidapi/SDL_hidapi.c +++ b/src/hidapi/SDL_hidapi.c @@ -1041,9 +1041,7 @@ static void SDLCALL OnlyControllersChanged(void *userdata, const char *name, con static void SDLCALL IgnoredDevicesChanged(void *userdata, const char *name, const char *oldValue, const char *hint) { - if (SDL_hidapi_ignored_devices) { - SDL_free(SDL_hidapi_ignored_devices); - } + SDL_free(SDL_hidapi_ignored_devices); if (hint && *hint) { SDL_hidapi_ignored_devices = SDL_strdup(hint); } else { diff --git a/src/io/SDL_iostream.c b/src/io/SDL_iostream.c index 04c6c94629a32..1c66e4b11e458 100644 --- a/src/io/SDL_iostream.c +++ b/src/io/SDL_iostream.c @@ -1177,9 +1177,7 @@ static bool SDLCALL dynamic_mem_close(void *userdata) { const IOStreamDynamicMemData *iodata = (IOStreamDynamicMemData *) userdata; void *mem = SDL_GetPointerProperty(SDL_GetIOProperties(iodata->stream), SDL_PROP_IOSTREAM_DYNAMIC_MEMORY_POINTER, NULL); - if (mem) { - SDL_free(mem); - } + SDL_free(mem); SDL_free(userdata); return true; } diff --git a/src/joystick/SDL_joystick.c b/src/joystick/SDL_joystick.c index 9bbbd848f9a13..b66c30303d5eb 100644 --- a/src/joystick/SDL_joystick.c +++ b/src/joystick/SDL_joystick.c @@ -3851,9 +3851,7 @@ static void SDL_LoadVIDPIDListFromHint(const char *hint, int *num_entries, int * (*entries)[(*num_entries)++] = entry; } - if (file) { - SDL_free(file); - } + SDL_free(file); } void SDL_LoadVIDPIDListFromHints(SDL_vidpid_list *list, const char *included_list, const char *excluded_list) diff --git a/src/joystick/hidapi/SDL_hidapi_combined.c b/src/joystick/hidapi/SDL_hidapi_combined.c index 5426edbd71e28..5bc8a97a2db30 100644 --- a/src/joystick/hidapi/SDL_hidapi_combined.c +++ b/src/joystick/hidapi/SDL_hidapi_combined.c @@ -76,9 +76,7 @@ static bool HIDAPI_DriverCombined_OpenJoystick(SDL_HIDAPI_Device *device, SDL_Jo child = device->children[i]; child->driver->CloseJoystick(child, joystick); } - if (serial) { - SDL_free(serial); - } + SDL_free(serial); return false; } @@ -102,9 +100,7 @@ static bool HIDAPI_DriverCombined_OpenJoystick(SDL_HIDAPI_Device *device, SDL_Jo } // Update the joystick with the combined serial numbers - if (joystick->serial) { - SDL_free(joystick->serial); - } + SDL_free(joystick->serial); joystick->serial = serial; return true; diff --git a/src/joystick/hidapi/SDL_hidapi_gip.c b/src/joystick/hidapi/SDL_hidapi_gip.c index 68161181c73af..e4d9d97f302df 100644 --- a/src/joystick/hidapi/SDL_hidapi_gip.c +++ b/src/joystick/hidapi/SDL_hidapi_gip.c @@ -797,28 +797,18 @@ static bool GIP_AttachmentIsController(GIP_Attachment *attachment) static void GIP_MetadataFree(GIP_Metadata *metadata) { - if (metadata->device.audio_formats) { - SDL_free(metadata->device.audio_formats); - } + SDL_free(metadata->device.audio_formats); if (metadata->device.preferred_types) { int i; for (i = 0; i < metadata->device.num_preferred_types; i++) { - if (metadata->device.preferred_types[i]) { - SDL_free(metadata->device.preferred_types[i]); - } + SDL_free(metadata->device.preferred_types[i]); } SDL_free(metadata->device.preferred_types); } - if (metadata->device.supported_interfaces) { - SDL_free(metadata->device.supported_interfaces); - } - if (metadata->device.hid_descriptor) { - SDL_free(metadata->device.hid_descriptor); - } + SDL_free(metadata->device.supported_interfaces); + SDL_free(metadata->device.hid_descriptor); - if (metadata->message_metadata) { - SDL_free(metadata->message_metadata); - } + SDL_free(metadata->message_metadata); SDL_memset(metadata, 0, sizeof(*metadata)); } diff --git a/src/joystick/linux/SDL_sysjoystick.c b/src/joystick/linux/SDL_sysjoystick.c index dba4d2258c837..d4bb97908114e 100644 --- a/src/joystick/linux/SDL_sysjoystick.c +++ b/src/joystick/linux/SDL_sysjoystick.c @@ -354,12 +354,8 @@ static bool IsJoystick(const char *path, int *fd, char **name_return, Uint16 *ve return true; error: - if (driver) { - SDL_free(driver); - } - if (name) { - SDL_free(name); - } + SDL_free(driver); + SDL_free(name); return false; } diff --git a/src/joystick/windows/SDL_rawinputjoystick.c b/src/joystick/windows/SDL_rawinputjoystick.c index 8590d9a83615f..bd13fa547c7dd 100644 --- a/src/joystick/windows/SDL_rawinputjoystick.c +++ b/src/joystick/windows/SDL_rawinputjoystick.c @@ -923,12 +923,8 @@ static void RAWINPUT_AddDevice(HANDLE hDevice) device->name = SDL_CreateJoystickName(device->vendor_id, device->product_id, manufacturer_string, product_string); device->guid = SDL_CreateJoystickGUID(SDL_HARDWARE_BUS_USB, device->vendor_id, device->product_id, device->version, manufacturer_string, product_string, 'r', 0); - if (manufacturer_string) { - SDL_free(manufacturer_string); - } - if (product_string) { - SDL_free(product_string); - } + SDL_free(manufacturer_string); + SDL_free(product_string); } device->path = SDL_strdup(dev_name); @@ -963,12 +959,8 @@ static void RAWINPUT_AddDevice(HANDLE hDevice) CloseHandle(hFile); } if (device) { - if (device->name) { - SDL_free(device->name); - } - if (device->path) { - SDL_free(device->path); - } + SDL_free(device->name); + SDL_free(device->path); SDL_free(device); } #undef CHECK diff --git a/src/joystick/windows/SDL_windows_gaming_input.c b/src/joystick/windows/SDL_windows_gaming_input.c index 5f9435e34a570..66641437af97f 100644 --- a/src/joystick/windows/SDL_windows_gaming_input.c +++ b/src/joystick/windows/SDL_windows_gaming_input.c @@ -973,9 +973,7 @@ static void WGI_JoystickQuit(void) while (wgi.controller_count > 0) { IEventHandler_CRawGameControllerVtbl_InvokeRemoved(&controller_removed.iface, NULL, wgi.controllers[wgi.controller_count - 1].controller); } - if (wgi.controllers) { - SDL_free(wgi.controllers); - } + SDL_free(wgi.controllers); if (wgi.arcade_stick_statics) { __x_ABI_CWindows_CGaming_CInput_CIArcadeStickStatics_Release(wgi.arcade_stick_statics); diff --git a/src/render/opengl/SDL_render_gl.c b/src/render/opengl/SDL_render_gl.c index 55aaca6a39558..e554edbcffad6 100644 --- a/src/render/opengl/SDL_render_gl.c +++ b/src/render/opengl/SDL_render_gl.c @@ -613,9 +613,7 @@ static bool GL_CreateTexture(SDL_Renderer *renderer, SDL_Texture *texture, SDL_P GL_CheckError("", renderer); renderdata->glGenTextures(1, &data->texture); if (!GL_CheckError("glGenTextures()", renderer)) { - if (data->pixels) { - SDL_free(data->pixels); - } + SDL_free(data->pixels); SDL_free(data); return false; } diff --git a/src/render/opengles2/SDL_render_gles2.c b/src/render/opengles2/SDL_render_gles2.c index 71e553661b13c..9d96efc5d482b 100644 --- a/src/render/opengles2/SDL_render_gles2.c +++ b/src/render/opengles2/SDL_render_gles2.c @@ -1962,9 +1962,7 @@ static bool GLES2_TexSubImage2D(GLES2_RenderData *data, GLenum target, GLint xof } data->glTexSubImage2D(target, 0, xoffset, yoffset, width, height, format, type, src); - if (blob) { - SDL_free(blob); - } + SDL_free(blob); return true; } diff --git a/src/stdlib/SDL_getenv.c b/src/stdlib/SDL_getenv.c index b47570587a510..a9ff1f398799a 100644 --- a/src/stdlib/SDL_getenv.c +++ b/src/stdlib/SDL_getenv.c @@ -564,9 +564,7 @@ const char *SDL_getenv_unsafe(const char *name) maxlen = length; } else { if (GetLastError() != ERROR_SUCCESS) { - if (string) { - SDL_free(string); - } + SDL_free(string); return NULL; } break; diff --git a/src/test/SDL_test_common.c b/src/test/SDL_test_common.c index a72e63faf6621..e3d8fcf57a1f1 100644 --- a/src/test/SDL_test_common.c +++ b/src/test/SDL_test_common.c @@ -2075,9 +2075,7 @@ static void SDLCALL SDLTest_ScreenShotClipboardCleanup(void *context) SDL_Log("Cleaning up screenshot image data"); - if (data->image) { - SDL_free(data->image); - } + SDL_free(data->image); SDL_free(data); } diff --git a/src/thread/n3ds/SDL_sysmutex.c b/src/thread/n3ds/SDL_sysmutex.c index 0abb9f55890ab..061f02fadc2ac 100644 --- a/src/thread/n3ds/SDL_sysmutex.c +++ b/src/thread/n3ds/SDL_sysmutex.c @@ -37,9 +37,7 @@ SDL_Mutex *SDL_CreateMutex(void) void SDL_DestroyMutex(SDL_Mutex *mutex) { - if (mutex) { - SDL_free(mutex); - } + SDL_free(mutex); } void SDL_LockMutex(SDL_Mutex *mutex) SDL_NO_THREAD_SAFETY_ANALYSIS // clang doesn't know about NULL mutexes diff --git a/src/tray/unix/SDL_tray.c b/src/tray/unix/SDL_tray.c index d45a3a4acb4bc..a281396b7f5b5 100644 --- a/src/tray/unix/SDL_tray.c +++ b/src/tray/unix/SDL_tray.c @@ -317,9 +317,7 @@ SDL_Tray *SDL_CreateTray(SDL_Surface *icon, const char *tooltip) SDL_free(sdl_dir); tray_error: - if (tray) { - SDL_free(tray); - } + SDL_free(tray); if (gtk) { SDL_Gtk_ExitContext(gtk); diff --git a/src/video/SDL_video.c b/src/video/SDL_video.c index b00efd9b84090..a622fa21ec919 100644 --- a/src/video/SDL_video.c +++ b/src/video/SDL_video.c @@ -1453,9 +1453,7 @@ void SDL_SetDesktopDisplayMode(SDL_VideoDisplay *display, const SDL_DisplayMode SDL_copyp(&last_mode, &display->desktop_mode); - if (display->desktop_mode.internal) { - SDL_free(display->desktop_mode.internal); - } + SDL_free(display->desktop_mode.internal); SDL_copyp(&display->desktop_mode, mode); display->desktop_mode.displayID = display->id; SDL_FinalizeDisplayMode(&display->desktop_mode); diff --git a/src/video/SDL_yuv.c b/src/video/SDL_yuv.c index 338b15630ba0a..a1642b4f2db47 100644 --- a/src/video/SDL_yuv.c +++ b/src/video/SDL_yuv.c @@ -1557,9 +1557,7 @@ static bool SDL_ConvertPixels_PackUVPlanes_to_NV_std(int width, int height, cons dstUV += dstUVPitchLeft; } - if (tmp) { - SDL_free(tmp); - } + SDL_free(tmp); return true; } @@ -1611,9 +1609,7 @@ static bool SDL_ConvertPixels_SplitNV_to_UVPlanes_std(int width, int height, con dst2 += dstUVPitchLeft; } - if (tmp) { - SDL_free(tmp); - } + SDL_free(tmp); return true; } diff --git a/src/video/kmsdrm/SDL_kmsdrmmouse.c b/src/video/kmsdrm/SDL_kmsdrmmouse.c index f72bd3ae6a2c7..b51f58a3424d7 100644 --- a/src/video/kmsdrm/SDL_kmsdrmmouse.c +++ b/src/video/kmsdrm/SDL_kmsdrmmouse.c @@ -243,9 +243,7 @@ static bool KMSDRM_DumpCursorToBO(SDL_VideoDisplay *display, SDL_Mouse *mouse, S } cleanup: - if (ready_buffer) { - SDL_free(ready_buffer); - } + SDL_free(ready_buffer); return result; } @@ -263,9 +261,7 @@ static void KMSDRM_FreeCursor(SDL_Cursor *cursor) curdata->buffer = NULL; } // Free cursor itself - if (cursor->internal) { - SDL_free(cursor->internal); - } + SDL_free(cursor->internal); SDL_free(cursor); } } @@ -322,14 +318,10 @@ static SDL_Cursor *KMSDRM_CreateCursor(SDL_Surface *surface, int hot_x, int hot_ cleanup: if (!result) { if (curdata) { - if (curdata->buffer) { - SDL_free(curdata->buffer); - } + SDL_free(curdata->buffer); SDL_free(curdata); } - if (cursor) { - SDL_free(cursor); - } + SDL_free(cursor); } return result; diff --git a/src/video/kmsdrm/SDL_kmsdrmvideo.c b/src/video/kmsdrm/SDL_kmsdrmvideo.c index 1d9ac5aee8a93..29c3cf5ce9013 100644 --- a/src/video/kmsdrm/SDL_kmsdrmvideo.c +++ b/src/video/kmsdrm/SDL_kmsdrmvideo.c @@ -711,13 +711,9 @@ static SDL_VideoDevice *KMSDRM_CreateDevice(void) return device; cleanup: - if (device) { - SDL_free(device); - } + SDL_free(device); - if (viddata) { - SDL_free(viddata); - } + SDL_free(viddata); return NULL; } diff --git a/src/video/kmsdrm/SDL_kmsdrmvulkan.c b/src/video/kmsdrm/SDL_kmsdrmvulkan.c index e150ee1c9fa65..493e62969910d 100644 --- a/src/video/kmsdrm/SDL_kmsdrmvulkan.c +++ b/src/video/kmsdrm/SDL_kmsdrmvulkan.c @@ -443,9 +443,7 @@ bool KMSDRM_Vulkan_CreateSurface(SDL_VideoDevice *_this, } // Free the list of displays supported by this plane. - if (supported_displays) { - SDL_free(supported_displays); - } + SDL_free(supported_displays); // If the display is not supported by this plane, iterate to the next plane. if (!plane_supports_display) { @@ -494,21 +492,11 @@ bool KMSDRM_Vulkan_CreateSurface(SDL_VideoDevice *_this, ret = true; // success! clean: - if (physical_devices) { - SDL_free(physical_devices); - } - if (display_props) { - SDL_free(display_props); - } - if (device_props) { - SDL_free(device_props); - } - if (plane_props) { - SDL_free(plane_props); - } - if (mode_props) { - SDL_free(mode_props); - } + SDL_free(physical_devices); + SDL_free(display_props); + SDL_free(device_props); + SDL_free(plane_props); + SDL_free(mode_props); return ret; } diff --git a/src/video/openvr/SDL_openvrvideo.c b/src/video/openvr/SDL_openvrvideo.c index cd9c1bfdb802c..c492d4b7fde28 100644 --- a/src/video/openvr/SDL_openvrvideo.c +++ b/src/video/openvr/SDL_openvrvideo.c @@ -1141,9 +1141,7 @@ static void OPENVR_DestroyWindow(SDL_VideoDevice *_this, SDL_Window *window) SDL_WindowData *data; data = window->internal; - if (data) { - SDL_free(data); - } + SDL_free(data); window->internal = NULL; } diff --git a/src/video/vita/SDL_vitavideo.c b/src/video/vita/SDL_vitavideo.c index 4643e1d71a6c3..72ce659a88789 100644 --- a/src/video/vita/SDL_vitavideo.c +++ b/src/video/vita/SDL_vitavideo.c @@ -326,10 +326,7 @@ void VITA_DestroyWindow(SDL_VideoDevice *_this, SDL_Window *window) SDL_WindowData *data; data = window->internal; - if (data) { - // TODO: should we destroy egl context? No one sane should recreate ogl window as non-ogl - SDL_free(data); - } + SDL_free(data); window->internal = NULL; Vita_Window = NULL; diff --git a/src/video/wayland/SDL_waylanddatamanager.c b/src/video/wayland/SDL_waylanddatamanager.c index 53375a09b5ef8..2960d0cbfede3 100644 --- a/src/video/wayland/SDL_waylanddatamanager.c +++ b/src/video/wayland/SDL_waylanddatamanager.c @@ -226,9 +226,7 @@ static bool mime_data_list_add(struct wl_list *list, } if (mime_data && buffer && length > 0) { - if (mime_data->data) { - SDL_free(mime_data->data); - } + SDL_free(mime_data->data); mime_data->data = internal_buffer; mime_data->length = length; } else { @@ -244,12 +242,8 @@ static void mime_data_list_free(struct wl_list *list) SDL_MimeDataList *next = NULL; wl_list_for_each_safe (mime_data, next, list, link) { - if (mime_data->data) { - SDL_free(mime_data->data); - } - if (mime_data->mime_type) { - SDL_free(mime_data->mime_type); - } + SDL_free(mime_data->data); + SDL_free(mime_data->mime_type); SDL_free(mime_data); } } diff --git a/src/video/windows/SDL_windowskeyboard.c b/src/video/windows/SDL_windowskeyboard.c index 081bcaebe2d2b..33aeee5d2d447 100644 --- a/src/video/windows/SDL_windowskeyboard.c +++ b/src/video/windows/SDL_windowskeyboard.c @@ -815,9 +815,7 @@ static void IME_GetCompositionString(SDL_VideoData *videodata, HIMC himc, DWORD length = ImmGetCompositionStringW(himc, string, NULL, 0); if (length > 0 && videodata->ime_composition_length < length) { - if (videodata->ime_composition) { - SDL_free(videodata->ime_composition); - } + SDL_free(videodata->ime_composition); videodata->ime_composition = (WCHAR *)SDL_malloc(length + sizeof(WCHAR)); videodata->ime_composition_length = length; diff --git a/src/video/windows/SDL_windowswindow.c b/src/video/windows/SDL_windowswindow.c index a7c7059fd2f95..02ed14018ce3f 100644 --- a/src/video/windows/SDL_windowswindow.c +++ b/src/video/windows/SDL_windowswindow.c @@ -589,9 +589,7 @@ static void CleanupWindowData(SDL_VideoDevice *_this, SDL_Window *window) if (data->drop_target) { WIN_AcceptDragAndDrop(window, false); } - if (data->ICMFileName) { - SDL_free(data->ICMFileName); - } + SDL_free(data->ICMFileName); if (data->keyboard_hook) { UnhookWindowsHookEx(data->keyboard_hook); } @@ -1391,9 +1389,7 @@ void WIN_UpdateWindowICCProfile(SDL_Window *window, bool send_event) // fileNameSize includes '\0' on return if (!data->ICMFileName || SDL_wcscmp(data->ICMFileName, fileName) != 0) { - if (data->ICMFileName) { - SDL_free(data->ICMFileName); - } + SDL_free(data->ICMFileName); data->ICMFileName = SDL_wcsdup(fileName); if (send_event) { SDL_SendWindowEvent(window, SDL_EVENT_WINDOW_ICCPROF_CHANGED, 0, 0); diff --git a/src/video/x11/SDL_x11clipboard.c b/src/video/x11/SDL_x11clipboard.c index 6a4a1cec5b0e8..f3e9f372ca47c 100644 --- a/src/video/x11/SDL_x11clipboard.c +++ b/src/video/x11/SDL_x11clipboard.c @@ -286,9 +286,7 @@ bool X11_HasClipboardData(SDL_VideoDevice *_this, const char *mime_type) size_t length; void *data; data = X11_GetClipboardData(_this, mime_type, &length); - if (data) { - SDL_free(data); - } + SDL_free(data); return length > 0; } diff --git a/src/video/x11/SDL_x11keyboard.c b/src/video/x11/SDL_x11keyboard.c index c7c5de99f7a74..fa01da629bc36 100644 --- a/src/video/x11/SDL_x11keyboard.c +++ b/src/video/x11/SDL_x11keyboard.c @@ -183,13 +183,9 @@ bool X11_InitKeyboard(SDL_VideoDevice *_this) (void)setlocale(LC_ALL, prev_locale); X11_XSetLocaleModifiers(prev_xmods); - if (prev_locale) { - SDL_free(prev_locale); - } + SDL_free(prev_locale); - if (prev_xmods) { - SDL_free(prev_xmods); - } + SDL_free(prev_xmods); } #endif // Try to determine which scancodes are being used based on fingerprint diff --git a/src/video/x11/SDL_x11messagebox.c b/src/video/x11/SDL_x11messagebox.c index 041f41d36dae3..158a65b45e268 100644 --- a/src/video/x11/SDL_x11messagebox.c +++ b/src/video/x11/SDL_x11messagebox.c @@ -344,9 +344,7 @@ static bool X11_ShowMessageBoxImpl(const SDL_MessageBoxData *messageboxdata, int X11Toolkit_CreateWindowRes(controls.window, w, h, 0, 0, (char *)messageboxdata->title); X11Toolkit_DoWindowEventLoop(controls.window); X11Toolkit_DestroyWindow(controls.window); - if (controls.buttons) { - SDL_free(controls.buttons); - } + SDL_free(controls.buttons); return true; } diff --git a/src/video/x11/SDL_x11toolkit.c b/src/video/x11/SDL_x11toolkit.c index 6ffba8e4651d4..027ea45608fc9 100644 --- a/src/video/x11/SDL_x11toolkit.c +++ b/src/video/x11/SDL_x11toolkit.c @@ -1864,12 +1864,8 @@ void X11Toolkit_DestroyWindow(SDL_ToolkitWindowX11 *data) { data->controls[i]->func_free(data->controls[i]); } } - if (data->controls) { - SDL_free(data->controls); - } - if (data->dyn_controls) { - SDL_free(data->dyn_controls); - } + SDL_free(data->controls); + SDL_free(data->dyn_controls); if (data->popup_windows) { SDL_ListClear(&data->popup_windows); diff --git a/test/gamepadutils.c b/test/gamepadutils.c index 61104df468ce8..dbfa98c1fa506 100644 --- a/test/gamepadutils.c +++ b/test/gamepadutils.c @@ -2642,9 +2642,7 @@ void SetGamepadButtonLabel(GamepadButton *ctx, const char *label) return; } - if (ctx->label) { - SDL_free(ctx->label); - } + SDL_free(ctx->label); ctx->label = SDL_strdup(label); ctx->label_width = (float)(FONT_CHARACTER_SIZE * SDL_strlen(label)); diff --git a/test/testcontroller.c b/test/testcontroller.c index a56661af5de10..a68ef107d0238 100644 --- a/test/testcontroller.c +++ b/test/testcontroller.c @@ -1237,12 +1237,8 @@ static void DelController(SDL_JoystickID id) CyclePS5TriggerEffect(&controllers[i]); } SDL_assert(controllers[i].gamepad == NULL); - if (controllers[i].axis_state) { - SDL_free(controllers[i].axis_state); - } - if (controllers[i].imu_state) { - SDL_free(controllers[i].imu_state); - } + SDL_free(controllers[i].axis_state); + SDL_free(controllers[i].imu_state); if (controllers[i].joystick) { SDL_CloseJoystick(controllers[i].joystick); } diff --git a/test/testffmpeg_vulkan.c b/test/testffmpeg_vulkan.c index 5a392a905a1f4..0a933ff0595e0 100644 --- a/test/testffmpeg_vulkan.c +++ b/test/testffmpeg_vulkan.c @@ -938,12 +938,8 @@ void DestroyVulkanVideoContext(VulkanVideoContext *context) if (context->device) { context->vkDeviceWaitIdle(context->device); } - if (context->instanceExtensions) { - SDL_free(context->instanceExtensions); - } - if (context->deviceExtensions) { - SDL_free(context->deviceExtensions); - } + SDL_free(context->instanceExtensions); + SDL_free(context->deviceExtensions); if (context->waitSemaphores) { for (uint32_t i = 0; i < context->waitSemaphoreCount; ++i) { context->vkDestroySemaphore(context->device, context->waitSemaphores[i], NULL); diff --git a/test/testutils.c b/test/testutils.c index a8dce587eb22c..4acf985fac447 100644 --- a/test/testutils.c +++ b/test/testutils.c @@ -123,8 +123,6 @@ SDL_Texture *LoadTexture(SDL_Renderer *renderer, const char *file, bool transpar } } SDL_DestroySurface(temp); - if (path) { - SDL_free(path); - } + SDL_free(path); return texture; } From cb100be6b7892279857e594fce1f2915caa1999c Mon Sep 17 00:00:00 2001 From: Eddy Jansson Date: Sun, 19 Oct 2025 11:17:11 +0200 Subject: [PATCH 2/2] Don't do NULL-checks before free() --- src/dialog/windows/SDL_windowsdialog.c | 4 +++- src/hidapi/windows/hid.c | 4 +--- src/video/x11/xsettings-client.c | 11 ++++------- 3 files changed, 8 insertions(+), 11 deletions(-) diff --git a/src/dialog/windows/SDL_windowsdialog.c b/src/dialog/windows/SDL_windowsdialog.c index b25282417526f..92ba44ed83ab5 100644 --- a/src/dialog/windows/SDL_windowsdialog.c +++ b/src/dialog/windows/SDL_windowsdialog.c @@ -731,7 +731,9 @@ bool windows_ShowModernFileFolderDialog(SDL_FileDialogType dialog_type, const ch // default_file_w is a pointer into default_folder_w. if (default_folder_w) { SDL_free(default_folder_w); - } else SDL_free(default_file_w); + } else { + SDL_free(default_file_w); + } SDL_free(title_w); diff --git a/src/hidapi/windows/hid.c b/src/hidapi/windows/hid.c index 8a8224302e75e..e8bfb026b5e40 100644 --- a/src/hidapi/windows/hid.c +++ b/src/hidapi/windows/hid.c @@ -991,9 +991,7 @@ struct hid_device_info HID_API_EXPORT * HID_API_CALL hid_enumerate(unsigned shor break; } - if (device_interface_list != NULL) { - free(device_interface_list); - } + free(device_interface_list); // This should NOT be SDL_free() device_interface_list = (wchar_t*)calloc(len, sizeof(wchar_t)); if (device_interface_list == NULL) { diff --git a/src/video/x11/xsettings-client.c b/src/video/x11/xsettings-client.c index 8fb1cd3c6c821..88a7b7eb5a80e 100644 --- a/src/video/x11/xsettings-client.c +++ b/src/video/x11/xsettings-client.c @@ -659,9 +659,8 @@ xsettings_setting_copy (XSettingsSetting *setting) return result; err: - if (result->name) - free (result->name); - free (result); + free(result->name); // This should NOT be SDL_free() + free(result); // This should NOT be SDL_free() return NULL; } @@ -741,10 +740,8 @@ xsettings_setting_free (XSettingsSetting *setting) if (setting->type == XSETTINGS_TYPE_STRING) free (setting->data.v_string); - if (setting->name) - free (setting->name); - - free (setting); + free(setting->name); // This should NOT be SDL_free() + free(setting); // This should NOT be SDL_free() } void