From bcfac0fedb166f7f14a45df652412e75bdd564ed Mon Sep 17 00:00:00 2001 From: "Ryan C. Gordon" Date: Mon, 3 Nov 2025 10:10:34 -0500 Subject: [PATCH 1/2] wasapi: Minor style tweaks. --- src/audio/wasapi/SDL_wasapi.c | 3 +-- src/core/windows/SDL_immdevice.c | 6 +++--- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/src/audio/wasapi/SDL_wasapi.c b/src/audio/wasapi/SDL_wasapi.c index 8a9907eb65518..fd5c78bdd9d71 100644 --- a/src/audio/wasapi/SDL_wasapi.c +++ b/src/audio/wasapi/SDL_wasapi.c @@ -728,8 +728,7 @@ static bool mgmtthrtask_PrepDevice(void *userdata) newspec.freq = waveformat->nSamplesPerSec; - if (device->recording && device->hidden->isplayback) - { + if (device->recording && device->hidden->isplayback) { streamflags |= AUDCLNT_STREAMFLAGS_LOOPBACK; } diff --git a/src/core/windows/SDL_immdevice.c b/src/core/windows/SDL_immdevice.c index e6cf1cec198d8..93c4afa5d5b32 100644 --- a/src/core/windows/SDL_immdevice.c +++ b/src/core/windows/SDL_immdevice.c @@ -147,7 +147,7 @@ static SDL_AudioDevice *SDL_IMMDevice_Add(const bool recording, const char *devn if (!device) { // handle is freed by SDL_IMMDevice_FreeDeviceHandle! - SDL_IMMDevice_HandleData *handle = (SDL_IMMDevice_HandleData *)SDL_malloc(sizeof(SDL_IMMDevice_HandleData)); + SDL_IMMDevice_HandleData *handle = (SDL_IMMDevice_HandleData *)SDL_calloc(1, sizeof(*handle)); if (!handle) { return NULL; } @@ -156,7 +156,7 @@ static SDL_AudioDevice *SDL_IMMDevice_Add(const bool recording, const char *devn SDL_free(handle); return NULL; } - SDL_memcpy(&handle->directsound_guid, dsoundguid, sizeof(GUID)); + SDL_copyp(&handle->directsound_guid, &dsoundguid); SDL_AudioSpec spec; SDL_zero(spec); @@ -168,7 +168,7 @@ static SDL_AudioDevice *SDL_IMMDevice_Add(const bool recording, const char *devn if (!recording && supports_recording_playback_devices) { // handle is freed by SDL_IMMDevice_FreeDeviceHandle! - SDL_IMMDevice_HandleData *recording_handle = (SDL_IMMDevice_HandleData *)SDL_malloc(sizeof(SDL_IMMDevice_HandleData)); + SDL_IMMDevice_HandleData *recording_handle = (SDL_IMMDevice_HandleData *)SDL_malloc(sizeof(*recording_handle)); if (!recording_handle) { return NULL; } From 1ebeafd3f71e9a7dab371bef7eff4f4b9fdbd5f8 Mon Sep 17 00:00:00 2001 From: "Ryan C. Gordon" Date: Mon, 3 Nov 2025 10:10:52 -0500 Subject: [PATCH 2/2] wasapi: fix memory leak on unlikely failure case. --- src/core/windows/SDL_immdevice.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/core/windows/SDL_immdevice.c b/src/core/windows/SDL_immdevice.c index 93c4afa5d5b32..cb57ad0abeb67 100644 --- a/src/core/windows/SDL_immdevice.c +++ b/src/core/windows/SDL_immdevice.c @@ -156,7 +156,7 @@ static SDL_AudioDevice *SDL_IMMDevice_Add(const bool recording, const char *devn SDL_free(handle); return NULL; } - SDL_copyp(&handle->directsound_guid, &dsoundguid); + SDL_copyp(&handle->directsound_guid, dsoundguid); SDL_AudioSpec spec; SDL_zero(spec); @@ -173,10 +173,16 @@ static SDL_AudioDevice *SDL_IMMDevice_Add(const bool recording, const char *devn return NULL; } - SDL_memcpy(&recording_handle->directsound_guid, dsoundguid, sizeof(GUID)); recording_handle->immdevice_id = SDL_wcsdup(devid); + if (!recording_handle->immdevice_id) { + SDL_free(recording_handle); + return NULL; + } + + SDL_copyp(&recording_handle->directsound_guid, dsoundguid); - if (!recording_handle->immdevice_id || !SDL_AddAudioDevice(true, devname, &spec, recording_handle)) { + if (!SDL_AddAudioDevice(true, devname, &spec, recording_handle)) { + SDL_free(recording_handle->immdevice_id); SDL_free(recording_handle); } }