public inbox for linux-mtd@lists.infradead.org
 help / color / mirror / Atom feed
* [PATCH] check kmalloc return value in mtd/sm_rtl.c
@ 2011-03-10  5:42 Xiaochen Wang
  2011-03-10 13:21 ` Artem Bityutskiy
  0 siblings, 1 reply; 4+ messages in thread
From: Xiaochen Wang @ 2011-03-10  5:42 UTC (permalink / raw)
  To: dwmw2, linux-mtd

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 <wangxiaochen0@gmail.com>
---
 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",
-- 
1.7.2.3

^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] check kmalloc return value in mtd/sm_rtl.c
  2011-03-10  5:42 [PATCH] check kmalloc return value in mtd/sm_rtl.c Xiaochen Wang
@ 2011-03-10 13:21 ` Artem Bityutskiy
  2011-03-10 14:31   ` Xiaochen Wang
  0 siblings, 1 reply; 4+ messages in thread
From: Artem Bityutskiy @ 2011-03-10 13:21 UTC (permalink / raw)
  To: Xiaochen Wang; +Cc: linux-mtd, dwmw2

Hi,

On Thu, 2011-03-10 at 13:42 +0800, Xiaochen Wang wrote: 
> @@ -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;

Please, compile-check patches before sending them out :-)


Best Regards,
Artem Bityutskiy (Артём Битюцкий)

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] check kmalloc return value in mtd/sm_rtl.c
  2011-03-10 13:21 ` Artem Bityutskiy
@ 2011-03-10 14:31   ` Xiaochen Wang
  2011-03-11  8:44     ` Artem Bityutskiy
  0 siblings, 1 reply; 4+ messages in thread
From: Xiaochen Wang @ 2011-03-10 14:31 UTC (permalink / raw)
  To: Artem Bityutskiy; +Cc: Xiaochen Wang, linux-mtd, dwmw2

Hi, Artem
> > +	if (!attributes)
> > +		got error3;
I'm so sorry for that:(
> Please, compile-check patches before sending them out :-)
Thanks for your advice, I'll do it.

The new patch , which has been compile-checked, is here:

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 <wangxiaochen0@gmail.com>
---
 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..2b0daae 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)
+		goto 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",
-- 
1.7.2.3

^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] check kmalloc return value in mtd/sm_rtl.c
  2011-03-10 14:31   ` Xiaochen Wang
@ 2011-03-11  8:44     ` Artem Bityutskiy
  0 siblings, 0 replies; 4+ messages in thread
From: Artem Bityutskiy @ 2011-03-11  8:44 UTC (permalink / raw)
  To: Xiaochen Wang; +Cc: linux-mtd, dwmw2

On Thu, 2011-03-10 at 22:31 +0800, Xiaochen Wang wrote:
> Hi, Artem
> > > +	if (!attributes)
> > > +		got error3;
> I'm so sorry for that:(
> > Please, compile-check patches before sending them out :-)
> Thanks for your advice, I'll do it.
> 
> The new patch , which has been compile-checked, is here:
> 
> 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 <wangxiaochen0@gmail.com>

Pushed to l2-mtd-2.6.git, thanks.

-- 
Best Regards,
Artem Bityutskiy (Артём Битюцкий)

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2011-03-11  8:45 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2011-03-10  5:42 [PATCH] check kmalloc return value in mtd/sm_rtl.c Xiaochen Wang
2011-03-10 13:21 ` Artem Bityutskiy
2011-03-10 14:31   ` Xiaochen Wang
2011-03-11  8:44     ` Artem Bityutskiy

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox