From patchwork Thu Mar 10 05:42:30 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Xiaochen Wang X-Patchwork-Id: 86186 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from bombadil.infradead.org (bombadil.infradead.org [18.85.46.34]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 0DD061007D2 for ; Thu, 10 Mar 2011 16:40:48 +1100 (EST) Received: from canuck.infradead.org ([2001:4978:20e::1]) by bombadil.infradead.org with esmtps (Exim 4.72 #1 (Red Hat Linux)) id 1PxYaA-0006Y3-Hi; Thu, 10 Mar 2011 05:38:42 +0000 Received: from localhost ([127.0.0.1] helo=canuck.infradead.org) by canuck.infradead.org with esmtp (Exim 4.72 #1 (Red Hat Linux)) id 1PxYa6-0002K2-S0; Thu, 10 Mar 2011 05:38:38 +0000 Received: from mail-iy0-f177.google.com ([209.85.210.177]) by canuck.infradead.org with esmtps (Exim 4.72 #1 (Red Hat Linux)) id 1PxYa2-0002Ji-Jy for linux-mtd@lists.infradead.org; Thu, 10 Mar 2011 05:38:35 +0000 Received: by iyf40 with SMTP id 40so1361353iyf.36 for ; Wed, 09 Mar 2011 21:38:33 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:date:from:to:subject:message-id:reply-to :mime-version:content-type:content-disposition:user-agent; bh=JUP/CP4Lw4Voh3g8KFXtjJsWiGVEIf3qhYLFl6NYJ7M=; b=pwNz0DyA2/BKth08ThHnUq9oOM0jGGxlIPFAmWMRmuObMzwGgeGQTViydMRJt0mxkR jGnnawOp42/pIk1VfmAuXYUQmpwtBC5u3/UPoccwTuhQb7kIIvp429AYWP1/w8CHyUHO APoO5X0U9EazvbbhkGP8X+M72ie/rNcVQJPCc= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=date:from:to:subject:message-id:reply-to:mime-version:content-type :content-disposition:user-agent; b=tRxSXlSuAaV+W7kUWyGD6GNuJAxID15C+tlWjkfcsTGZmsc/HhCDbhBlvgIwpACsbQ RUDCtzswlzAB5f/K43yNwph/0xTJTiRan4Lq4iFLIeC7B2fwG0WcIml5uoWAkpb7LRll P3LCO1nznAD+Jpb+MlFXpVBWyf4VeXFfvo9nU= Received: by 10.43.51.65 with SMTP id vh1mr9390890icb.435.1299735513222; Wed, 09 Mar 2011 21:38:33 -0800 (PST) Received: from localhost ([218.2.216.181]) by mx.google.com with ESMTPS id c4sm1931779ict.19.2011.03.09.21.38.27 (version=TLSv1/SSLv3 cipher=OTHER); Wed, 09 Mar 2011 21:38:30 -0800 (PST) Date: Thu, 10 Mar 2011 13:42:30 +0800 From: Xiaochen Wang To: dwmw2@infradead.org, linux-mtd@lists.infradead.org Subject: [PATCH] check kmalloc return value in mtd/sm_rtl.c Message-ID: <20110310054230.GA6022@chii> MIME-Version: 1.0 Content-Disposition: inline User-Agent: Mutt/1.5.20 (2009-08-17) X-CRM114-Version: 20090807-BlameThorstenAndJenny ( TRE 0.7.6 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20110310_003834_753238_A05F5FAA X-CRM114-Status: GOOD ( 12.88 ) X-Spam-Score: 1.4 (+) X-Spam-Report: SpamAssassin version 3.3.1 on canuck.infradead.org summary: Content analysis details: (1.4 points) pts rule name description ---- ---------------------- -------------------------------------------------- -0.7 RCVD_IN_DNSWL_LOW RBL: Sender listed at http://www.dnswl.org/, low trust [209.85.210.177 listed in list.dnswl.org] 0.0 FREEMAIL_FROM Sender email is freemail (wangxiaochen0[at]gmail.com) 2.2 FREEMAIL_ENVFROM_END_DIGIT Envelope-from freemail username ends in digit (wangxiaochen0[at]gmail.com) -0.1 DKIM_VALID_AU Message has a valid DKIM or DK signature from author's domain 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature 0.0 T_TO_NO_BRKTS_FREEMAIL T_TO_NO_BRKTS_FREEMAIL X-BeenThere: linux-mtd@lists.infradead.org X-Mailman-Version: 2.1.12 Precedence: list Reply-To: Xiaochen Wang List-Id: Linux MTD discussion mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: linux-mtd-bounces@lists.infradead.org Errors-To: linux-mtd-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org Description: Because malloc/kzalloc may fail, we should check kmalloc/kzalloc return value in sm_create_sysfs_attributes(), mtd/sm_rtl.c and do error handling. Meanwhile, we should check sm_create_sysfs_attributes return value. Signed-off-by: Xiaochen Wang --- drivers/mtd/sm_ftl.c | 18 ++++++++++++++++++ 1 files changed, 18 insertions(+), 0 deletions(-) diff --git a/drivers/mtd/sm_ftl.c b/drivers/mtd/sm_ftl.c index ac0d6a8..02bdfe4 100644 --- a/drivers/mtd/sm_ftl.c +++ b/drivers/mtd/sm_ftl.c @@ -64,12 +64,16 @@ struct attribute_group *sm_create_sysfs_attributes(struct sm_ftl *ftl) SM_SMALL_PAGE - SM_CIS_VENDOR_OFFSET); char *vendor = kmalloc(vendor_len, GFP_KERNEL); + if (!vendor) + goto error1; memcpy(vendor, ftl->cis_buffer + SM_CIS_VENDOR_OFFSET, vendor_len); vendor[vendor_len] = 0; /* Initialize sysfs attributes */ vendor_attribute = kzalloc(sizeof(struct sm_sysfs_attribute), GFP_KERNEL); + if (!vendor_attribute) + goto error2; sysfs_attr_init(&vendor_attribute->dev_attr.attr); @@ -83,12 +87,24 @@ struct attribute_group *sm_create_sysfs_attributes(struct sm_ftl *ftl) /* Create array of pointers to the attributes */ attributes = kzalloc(sizeof(struct attribute *) * (NUM_ATTRIBUTES + 1), GFP_KERNEL); + if (!attributes) + got error3; attributes[0] = &vendor_attribute->dev_attr.attr; /* Finally create the attribute group */ attr_group = kzalloc(sizeof(struct attribute_group), GFP_KERNEL); + if (!attr_group) + goto error4; attr_group->attrs = attributes; return attr_group; +error4: + kfree(attributes); +error3: + kfree(vendor_attribute); +error2: + kfree(vendor); +error1: + return NULL; } void sm_delete_sysfs_attributes(struct sm_ftl *ftl) @@ -1178,6 +1194,8 @@ static void sm_add_mtd(struct mtd_blktrans_ops *tr, struct mtd_info *mtd) } ftl->disk_attributes = sm_create_sysfs_attributes(ftl); + if (!ftl->disk_attributes) + goto error6; trans->disk_attributes = ftl->disk_attributes; sm_printk("Found %d MiB xD/SmartMedia FTL on mtd%d",