Don't do NULL-checks before SDL_free()

Replaces the pattern

  if (ptr) {
    SDL_free(ptr);
  }

with

  SDL_free(ptr);
This commit is contained in:
Eddy Jansson
2025-10-19 11:07:48 +02:00
committed by Sam Lantinga
parent 2f810e0a5f
commit aaee09d6ed
40 changed files with 76 additions and 229 deletions

View File

@@ -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)

View File

@@ -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);

View File

@@ -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);
}

View File

@@ -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) {

View File

@@ -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');

View File

@@ -731,25 +731,15 @@ quit:
// 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++) {

View File

@@ -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);

View File

@@ -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);

View File

@@ -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 {

View File

@@ -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;
}

View File

@@ -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)

View File

@@ -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;

View File

@@ -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));
}

View File

@@ -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;
}

View File

@@ -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 @@ err:
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

View File

@@ -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);

View File

@@ -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;
}

View File

@@ -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;
}

View File

@@ -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;

View File

@@ -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);
}

View File

@@ -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

View File

@@ -317,9 +317,7 @@ sdl_dir_error:
SDL_free(sdl_dir);
tray_error:
if (tray) {
SDL_free(tray);
}
SDL_free(tray);
if (gtk) {
SDL_Gtk_ExitContext(gtk);

View File

@@ -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);

View File

@@ -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;
}

View File

@@ -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;

View File

@@ -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;
}

View File

@@ -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;
}

View File

@@ -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;
}

View File

@@ -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;

View File

@@ -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);
}
}

View File

@@ -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;

View File

@@ -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);

View File

@@ -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;
}

View File

@@ -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

View File

@@ -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;
}

View File

@@ -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);

View File

@@ -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));

View File

@@ -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);
}

View File

@@ -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);

View File

@@ -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;
}