Skip to content

Commit 5ed7008

Browse files
committed
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.
1 parent fb6dbe5 commit 5ed7008

File tree

1 file changed

+18
-6
lines changed

1 file changed

+18
-6
lines changed

internal/audio/c/audio.c

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -605,7 +605,9 @@ int jetkvm_audio_capture_init() {
605605
if (err < 0) {
606606
snd_pcm_t *handle = pcm_capture_handle;
607607
pcm_capture_handle = NULL;
608-
snd_pcm_close(handle);
608+
if (handle) {
609+
snd_pcm_close(handle);
610+
}
609611
atomic_store(&capture_stop_requested, 0);
610612
capture_initializing = 0;
611613
return -2;
@@ -620,7 +622,9 @@ int jetkvm_audio_capture_init() {
620622
fflush(stderr);
621623
snd_pcm_t *handle = pcm_capture_handle;
622624
pcm_capture_handle = NULL;
623-
snd_pcm_close(handle);
625+
if (handle) {
626+
snd_pcm_close(handle);
627+
}
624628
atomic_store(&capture_stop_requested, 0);
625629
capture_initializing = 0;
626630
return -4;
@@ -644,7 +648,9 @@ int jetkvm_audio_capture_init() {
644648
fflush(stderr);
645649
snd_pcm_t *handle = pcm_capture_handle;
646650
pcm_capture_handle = NULL;
647-
snd_pcm_close(handle);
651+
if (handle) {
652+
snd_pcm_close(handle);
653+
}
648654
atomic_store(&capture_stop_requested, 0);
649655
capture_initializing = 0;
650656
return -3;
@@ -671,7 +677,9 @@ int jetkvm_audio_capture_init() {
671677
if (pcm_capture_handle) {
672678
snd_pcm_t *handle = pcm_capture_handle;
673679
pcm_capture_handle = NULL;
674-
snd_pcm_close(handle);
680+
if (handle) {
681+
snd_pcm_close(handle);
682+
}
675683
}
676684
atomic_store(&capture_stop_requested, 0);
677685
capture_initializing = 0;
@@ -888,7 +896,9 @@ int jetkvm_audio_playback_init() {
888896
if (err < 0) {
889897
snd_pcm_t *handle = pcm_playback_handle;
890898
pcm_playback_handle = NULL;
891-
snd_pcm_close(handle);
899+
if (handle) {
900+
snd_pcm_close(handle);
901+
}
892902
atomic_store(&playback_stop_requested, 0);
893903
playback_initializing = 0;
894904
return -1;
@@ -903,7 +913,9 @@ int jetkvm_audio_playback_init() {
903913
if (!decoder || opus_err != OPUS_OK) {
904914
snd_pcm_t *handle = pcm_playback_handle;
905915
pcm_playback_handle = NULL;
906-
snd_pcm_close(handle);
916+
if (handle) {
917+
snd_pcm_close(handle);
918+
}
907919
atomic_store(&playback_stop_requested, 0);
908920
playback_initializing = 0;
909921
return -2;

0 commit comments

Comments
 (0)