From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932196AbZHCUqg (ORCPT ); Mon, 3 Aug 2009 16:46:36 -0400 Received: (majordomo@vger.kernel.org) by vger.kernel.org id S1753573AbZHCUqf (ORCPT ); Mon, 3 Aug 2009 16:46:35 -0400 Received: from smtp1.linux-foundation.org ([140.211.169.13]:45913 "EHLO smtp1.linux-foundation.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753368AbZHCUqe (ORCPT ); Mon, 3 Aug 2009 16:46:34 -0400 Date: Mon, 3 Aug 2009 13:46:32 -0700 From: Andrew Morton To: Julia Lawall Cc: linux-kernel@vger.kernel.org, kernel-janitors@vger.kernel.org, Ming Lei Subject: Re: [PATCH 1/5] drivers/video: Add kmalloc NULL tests Message-Id: <20090803134632.7cff1658.akpm@linux-foundation.org> In-Reply-To: References: X-Mailer: Sylpheed version 2.2.4 (GTK+ 2.8.20; i486-pc-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, 30 Jul 2009 16:00:33 +0200 (CEST) Julia Lawall wrote: > From: Julia Lawall > > Check that the result of kmalloc is not NULL before passing it to other > functions. > > The semantic match that finds this problem is as follows: > (http://www.emn.fr/x-info/coccinelle/) > > // > @@ > expression *x; > identifier f; > constant char *C; > @@ > > x = \(kmalloc\|kcalloc\|kzalloc\)(...); > ... when != x == NULL > when != x != NULL > when != (x || ...) > ( > kfree(x) > | > f(...,C,...,x,...) > | > *f(...,x,...) > | > *x->f > ) > // > > Signed-off-by: Julia Lawall > > --- > drivers/video/au1100fb.c | 4 ++++ > 1 files changed, 4 insertions(+), 0 deletions(-) > > diff --git a/drivers/video/au1100fb.c b/drivers/video/au1100fb.c > index 378f277..1766846 100644 > --- a/drivers/video/au1100fb.c > +++ b/drivers/video/au1100fb.c > @@ -716,6 +716,10 @@ int au1100fb_setup(char *options) > /* Mode option (only option that start with digit) */ > else if (isdigit(this_opt[0])) { > mode = kmalloc(strlen(this_opt) + 1, GFP_KERNEL); > + if (!mode) { > + print_err("memory allocation failed"); > + return -ENOMEM; > + } > strncpy(mode, this_opt, strlen(this_opt) + 1); > } > /* Unsupported option */ We may as well clean up that open-coded kstrdup() too? --- a/drivers/video/au1100fb.c~drivers-video-add-kmalloc-null-tests-fix +++ a/drivers/video/au1100fb.c @@ -715,12 +715,11 @@ int au1100fb_setup(char *options) } /* Mode option (only option that start with digit) */ else if (isdigit(this_opt[0])) { - mode = kmalloc(strlen(this_opt) + 1, GFP_KERNEL); + mode = kstrdup(this_opt, GFP_KERNEL); if (!mode) { print_err("memory allocation failed"); return -ENOMEM; } - strncpy(mode, this_opt, strlen(this_opt) + 1); } /* Unsupported option */ else { _