Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id BFFF3C433F5 for ; Mon, 13 Dec 2021 13:23:04 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237370AbhLMNXE (ORCPT ); Mon, 13 Dec 2021 08:23:04 -0500 Received: from smtp-out1.suse.de ([195.135.220.28]:36876 "EHLO smtp-out1.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229873AbhLMNXC (ORCPT ); Mon, 13 Dec 2021 08:23:02 -0500 Received: from relay2.suse.de (relay2.suse.de [149.44.160.134]) by smtp-out1.suse.de (Postfix) with ESMTP id AAFF92111A; Mon, 13 Dec 2021 13:23:01 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1639401781; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=Rv1XlQo6nj/92BtG35/KGmG6N9zBV3ZTKHuEaXrsTac=; b=mzrzkvw1pm9xPky4biekJZf29hG9hGJfB/JmS5N5yTZO1VH9xu6a3jvXdGZ6FVuvUjy/S0 F6YWqUSXT5t3npwWjF/T0Xhv5Si5ZG42Faf0xQPcNuJ0p1IvPwHm3K9gJBOK9hEWWZUNzY r7hQI+bOLYUT70WwUEpXzyMp+mgZa+c= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1639401781; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=Rv1XlQo6nj/92BtG35/KGmG6N9zBV3ZTKHuEaXrsTac=; b=EmXhpqx65hzsrIUq+Y6AKcl3Syeir/7CTtRpuXEadDzVyQVScaNPE+t/rH1mY2TQhlVS/G uGugPRsM3DbwQsBw== Received: from alsa1.suse.de (alsa1.suse.de [10.160.4.42]) by relay2.suse.de (Postfix) with ESMTP id 9A610A3B83; Mon, 13 Dec 2021 13:23:01 +0000 (UTC) Date: Mon, 13 Dec 2021 14:23:01 +0100 Message-ID: From: Takashi Iwai To: Xiaoke Wang Cc: perex@perex.cz, tiwai@suse.com, alsa-devel@alsa-project.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH v2] ALSA: sound/isa/gus: check the return value of kstrdup() In-Reply-To: References: User-Agent: Wanderlust/2.15.9 (Almost Unreal) SEMI/1.14.6 (Maruoka) FLIM/1.14.9 (=?UTF-8?B?R29qxY0=?=) APEL/10.8 Emacs/25.3 (x86_64-suse-linux-gnu) MULE/6.0 (HANACHIRUSATO) MIME-Version: 1.0 (generated by SEMI 1.14.6 - "Maruoka") Content-Type: text/plain; charset=US-ASCII Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, 13 Dec 2021 11:52:32 +0100, Xiaoke Wang wrote: > > kstrdup() returns NULL when some internal memory errors happen, it is > better to check the return value of it. Otherwise, we may not to be able > to catch some memory errors in time. > > Signed-off-by: Xiaoke Wang > --- > Changes in v2: > - add the proper error handling. > Thanks for the suggestions from Takashi Iwai. In the future, I'll pay > more attention to what he mentioned. Thanks, now I applied as is. BTW, when you look at the code carefully, you'll find that the memory leaks may occur at the second and the third chunk in your patch. It's not a fault of this change, but it's a bug in the original code. Actually there are two issues -- one is about the incorrect return value from snd_gf1_mem_xalloc() and another is the missing kfree of block.name. Will submit two fix patches as a follow up. Takashi > --- > sound/isa/gus/gus_mem.c | 8 ++++++-- > 1 file changed, 6 insertions(+), 2 deletions(-) > > diff --git a/sound/isa/gus/gus_mem.c b/sound/isa/gus/gus_mem.c > index ff9480f..4c691db 100644 > --- a/sound/isa/gus/gus_mem.c > +++ b/sound/isa/gus/gus_mem.c > @@ -199,6 +199,10 @@ struct snd_gf1_mem_block *snd_gf1_mem_alloc(struct snd_gf1_mem * alloc, int owne > memcpy(&block.share_id, share_id, sizeof(block.share_id)); > block.owner = owner; > block.name = kstrdup(name, GFP_KERNEL); > + if (block.name == NULL) { > + snd_gf1_mem_lock(alloc, 1); > + return NULL; > + } > nblock = snd_gf1_mem_xalloc(alloc, &block); > snd_gf1_mem_lock(alloc, 1); > return nblock; > @@ -237,13 +241,13 @@ int snd_gf1_mem_init(struct snd_gus_card * gus) > block.ptr = 0; > block.size = 1024; > block.name = kstrdup("InterWave LFOs", GFP_KERNEL); > - if (snd_gf1_mem_xalloc(alloc, &block) == NULL) > + if (block.name == NULL || snd_gf1_mem_xalloc(alloc, &block) == NULL) > return -ENOMEM; > } > block.ptr = gus->gf1.default_voice_address; > block.size = 4; > block.name = kstrdup("Voice default (NULL's)", GFP_KERNEL); > - if (snd_gf1_mem_xalloc(alloc, &block) == NULL) > + if (block.name == NULL || snd_gf1_mem_xalloc(alloc, &block) == NULL) > return -ENOMEM; > #ifdef CONFIG_SND_DEBUG > snd_card_ro_proc_new(gus->card, "gusmem", gus, snd_gf1_mem_info_read); > -- >