Skip to contentSkip to conversation

selftests/alsa: Fix memory leak in find_controls error path

PATCH In review Malaya Kumar Rout 2 participants 2 messages Jul 4 – Jul 5

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 selftests/alsa: Fix memory leak in find_controls error path

In find_controls(), card_data is allocated with malloc() but when snd_ctl_open_lconf() fails, the code jumps to next_card without freeing the allocated memory. This results in a memory leak for each card where snd_ctl_open_lconf() fails.

Add free(card_data) before goto next_card to ensure proper cleanup of the allocated memory in the error path.

Fixes 5aaf9efffc57 ("kselftest: alsa: Add simplistic test for ALSA mixer controls kselftest")
---
 tools/testing/selftests/alsa/mixer-test.c | 1 +
 1 file changed, 1 insertion(+)
tools/testing/selftests/alsa/mixer-test.c +1 -0
index d4f845c32804..c5ae2a026ce9 100644
@@ -84,6 +84,7 @@ static void find_controls(void)
 		if (err < 0) {
 			ksft_print_msg("Failed to get hctl for card %d: %s\n",
 				       card, snd_strerror(err));
+			free(card_data);
 			goto next_card;
 		}
 
Signature · 1 line
2.54.0
#1 lore raw
Quoted 11 lines · Malaya Kumar Rout

In find_controls(), card_data is allocated with malloc() but when snd_ctl_open_lconf() fails, the code jumps to next_card without freeing the allocated memory. This results in a memory leak for each card where snd_ctl_open_lconf() fails.

Add free(card_data) before goto next_card to ensure proper cleanup of the allocated memory in the error path.

Fixes 5aaf9efffc57 ("kselftest: alsa: Add simplistic test for ALSA mixer controls kselftest")

Applied now. Thanks.

Takashi