From 5ed70083ec49b03ec123e7e5d2eafd0bef4cbca8 Mon Sep 17 00:00:00 2001 From: Alex P Date: Fri, 21 Nov 2025 22:42:44 +0200 Subject: [PATCH] Add NULL checks before snd_pcm_close() calls Defensive programming to prevent undefined behavior when closing ALSA PCM handles. While the previous commit disabled assertions with -DNDEBUG, adding explicit NULL checks ensures graceful handling even if handles are unexpectedly NULL. All error paths that call snd_pcm_close() now verify the handle is non-NULL before closing, preventing potential crashes in edge cases. --- internal/audio/c/audio.c | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/internal/audio/c/audio.c b/internal/audio/c/audio.c index e4fc5ddd..58c190df 100644 --- a/internal/audio/c/audio.c +++ b/internal/audio/c/audio.c @@ -605,7 +605,9 @@ int jetkvm_audio_capture_init() { if (err < 0) { snd_pcm_t *handle = pcm_capture_handle; pcm_capture_handle = NULL; - snd_pcm_close(handle); + if (handle) { + snd_pcm_close(handle); + } atomic_store(&capture_stop_requested, 0); capture_initializing = 0; return -2; @@ -620,7 +622,9 @@ int jetkvm_audio_capture_init() { fflush(stderr); snd_pcm_t *handle = pcm_capture_handle; pcm_capture_handle = NULL; - snd_pcm_close(handle); + if (handle) { + snd_pcm_close(handle); + } atomic_store(&capture_stop_requested, 0); capture_initializing = 0; return -4; @@ -644,7 +648,9 @@ int jetkvm_audio_capture_init() { fflush(stderr); snd_pcm_t *handle = pcm_capture_handle; pcm_capture_handle = NULL; - snd_pcm_close(handle); + if (handle) { + snd_pcm_close(handle); + } atomic_store(&capture_stop_requested, 0); capture_initializing = 0; return -3; @@ -671,7 +677,9 @@ int jetkvm_audio_capture_init() { if (pcm_capture_handle) { snd_pcm_t *handle = pcm_capture_handle; pcm_capture_handle = NULL; - snd_pcm_close(handle); + if (handle) { + snd_pcm_close(handle); + } } atomic_store(&capture_stop_requested, 0); capture_initializing = 0; @@ -888,7 +896,9 @@ int jetkvm_audio_playback_init() { if (err < 0) { snd_pcm_t *handle = pcm_playback_handle; pcm_playback_handle = NULL; - snd_pcm_close(handle); + if (handle) { + snd_pcm_close(handle); + } atomic_store(&playback_stop_requested, 0); playback_initializing = 0; return -1; @@ -903,7 +913,9 @@ int jetkvm_audio_playback_init() { if (!decoder || opus_err != OPUS_OK) { snd_pcm_t *handle = pcm_playback_handle; pcm_playback_handle = NULL; - snd_pcm_close(handle); + if (handle) { + snd_pcm_close(handle); + } atomic_store(&playback_stop_requested, 0); playback_initializing = 0; return -2;