[PATCH 2/2] ALSA: gus: Fix memory leaks at error paths in memory allocations

Takashi Iwai tiwai at suse.de
Mon Dec 13 14:24:44 CET 2021


When snd_gf1_mem_xalloc() returns NULL, the current code still leaves
the formerly allocated block.name string but returns an error
immediately.  This patch covers the all callers to deal with the
release of leftover name strings in the error paths.

Signed-off-by: Takashi Iwai <tiwai at suse.de>
---
 sound/isa/gus/gus_mem.c | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)

diff --git a/sound/isa/gus/gus_mem.c b/sound/isa/gus/gus_mem.c
index 5e3ff3137dd7..1509e3e5d30e 100644
--- a/sound/isa/gus/gus_mem.c
+++ b/sound/isa/gus/gus_mem.c
@@ -204,6 +204,8 @@ struct snd_gf1_mem_block *snd_gf1_mem_alloc(struct snd_gf1_mem * alloc, int owne
 		return NULL;
 	}
 	nblock = snd_gf1_mem_xalloc(alloc, &block);
+	if (!nblock)
+		kfree(block.name);
 	snd_gf1_mem_lock(alloc, 1);
 	return nblock;
 }
@@ -241,14 +243,22 @@ int snd_gf1_mem_init(struct snd_gus_card * gus)
 		block.ptr = 0;
 		block.size = 1024;
 		block.name = kstrdup("InterWave LFOs", GFP_KERNEL);
-		if (block.name == NULL || snd_gf1_mem_xalloc(alloc, &block) == NULL)
+		if (block.name == NULL)
 			return -ENOMEM;
+		if (snd_gf1_mem_xalloc(alloc, &block) == NULL) {
+			kfree(block.name);
+			return -ENOMEM;
+		}
 	}
 	block.ptr = gus->gf1.default_voice_address;
 	block.size = 4;
 	block.name = kstrdup("Voice default (NULL's)", GFP_KERNEL);
-	if (block.name == NULL || snd_gf1_mem_xalloc(alloc, &block) == NULL)
+	if (block.name == NULL)
 		return -ENOMEM;
+	if (snd_gf1_mem_xalloc(alloc, &block) == NULL) {
+		kfree(block.name);
+		return -ENOMEM;
+	}
 #ifdef CONFIG_SND_DEBUG
 	snd_card_ro_proc_new(gus->card, "gusmem", gus, snd_gf1_mem_info_read);
 #endif
-- 
2.31.1



More information about the Alsa-devel mailing list