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.
This commit is contained in:
Alex P 2025-11-21 22:42:44 +02:00
parent fb6dbe53eb
commit 5ed70083ec
1 changed files with 18 additions and 6 deletions

View File

@ -605,7 +605,9 @@ int jetkvm_audio_capture_init() {
if (err < 0) {
snd_pcm_t *handle = pcm_capture_handle;
pcm_capture_handle = NULL;
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;
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;
if (handle) {
snd_pcm_close(handle);
}
atomic_store(&capture_stop_requested, 0);
capture_initializing = 0;
return -3;
@ -671,8 +677,10 @@ int jetkvm_audio_capture_init() {
if (pcm_capture_handle) {
snd_pcm_t *handle = pcm_capture_handle;
pcm_capture_handle = NULL;
if (handle) {
snd_pcm_close(handle);
}
}
atomic_store(&capture_stop_requested, 0);
capture_initializing = 0;
return -4;
@ -888,7 +896,9 @@ int jetkvm_audio_playback_init() {
if (err < 0) {
snd_pcm_t *handle = pcm_playback_handle;
pcm_playback_handle = NULL;
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;
if (handle) {
snd_pcm_close(handle);
}
atomic_store(&playback_stop_requested, 0);
playback_initializing = 0;
return -2;