From 082ef41566362281d954c8f2d35f1cc171e83d58 Mon Sep 17 00:00:00 2001 From: Frank Praznik Date: Wed, 25 Oct 2023 19:26:40 -0400 Subject: [PATCH] alsa: Fix crash from invalid handle pointer ALSA expects handles to be of type ALSA_Device, and passing the handle for the default device as a plain string causes a crash as it attempts to deference the string contents itself as a pointer to a string. Create immutable static ALSA_Device structs for the default devices and pass those as the handles. They are not placed in the hotplug list, and the audio layer doesn't attempt to free ALSA handles, so there is no need to worry about them being erroneously freed. --- src/audio/alsa/SDL_alsa_audio.c | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/audio/alsa/SDL_alsa_audio.c b/src/audio/alsa/SDL_alsa_audio.c index 5d12e1751f..46ba1dc797 100644 --- a/src/audio/alsa/SDL_alsa_audio.c +++ b/src/audio/alsa/SDL_alsa_audio.c @@ -210,6 +210,18 @@ typedef struct ALSA_Device struct ALSA_Device *next; } ALSA_Device; +static const ALSA_Device default_output_handle = { + "default", + SDL_FALSE, + NULL +}; + +static const ALSA_Device default_capture_handle = { + "default", + SDL_TRUE, + NULL +}; + static const char *get_audio_device(void *handle, const int channels) { SDL_assert(handle != NULL); // SDL2 used NULL to mean "default" but that's not true in SDL3. @@ -911,10 +923,10 @@ static void ALSA_DetectDevices(SDL_AudioDevice **default_output, SDL_AudioDevice SDL_bool has_default_output = SDL_FALSE, has_default_capture = SDL_FALSE; ALSA_HotplugIteration(&has_default_output, &has_default_capture); // run once now before a thread continues to check. if (has_default_output) { - *default_output = SDL_AddAudioDevice(/*iscapture=*/SDL_FALSE, "ALSA default output device", NULL, SDL_strdup("default")); + *default_output = SDL_AddAudioDevice(/*iscapture=*/SDL_FALSE, "ALSA default output device", NULL, (void*)&default_output_handle); } if (has_default_capture) { - *default_capture = SDL_AddAudioDevice(/*iscapture=*/SDL_TRUE, "ALSA default capture device", NULL, SDL_strdup("default")); + *default_capture = SDL_AddAudioDevice(/*iscapture=*/SDL_TRUE, "ALSA default capture device", NULL, (void*)&default_capture_handle); } #if SDL_ALSA_HOTPLUG_THREAD