linux-omap.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] gpio/omap: fix possible memory leak in omap2_gpio_dev_init()
@ 2012-09-03 10:13 Wei Yongjun
  2012-09-03 11:11 ` Russell King - ARM Linux
  0 siblings, 1 reply; 3+ messages in thread
From: Wei Yongjun @ 2012-09-03 10:13 UTC (permalink / raw)
  To: tony, linux; +Cc: yongjun_wei, linux-omap, linux-arm-kernel

From: Wei Yongjun <yongjun_wei@trendmicro.com.cn>

pdata and pdata->regs have been allocated in this function and 
should be freed before leaving it, and in the other error handling
cases too.

spatch with a semantic match is used to found this problem.
(http://coccinelle.lip6.fr/)

Signed-off-by: Wei Yongjun <yongjun_wei@trendmicro.com.cn>
---
 arch/arm/mach-omap2/gpio.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/arch/arm/mach-omap2/gpio.c b/arch/arm/mach-omap2/gpio.c
index 9ad7d48..3bfa4bb 100644
--- a/arch/arm/mach-omap2/gpio.c
+++ b/arch/arm/mach-omap2/gpio.c
@@ -60,6 +60,7 @@ static int __init omap2_gpio_dev_init(struct omap_hwmod *oh, void *unused)
 	pdata->regs = kzalloc(sizeof(struct omap_gpio_reg_offs), GFP_KERNEL);
 	if (!pdata->regs) {
 		pr_err("gpio%d: Memory allocation failed\n", id);
+		kfree(pdata);
 		return -ENOMEM;
 	}
 
@@ -121,6 +122,7 @@ static int __init omap2_gpio_dev_init(struct omap_hwmod *oh, void *unused)
 		break;
 	default:
 		WARN(1, "Invalid gpio bank_type\n");
+		kfree(pdata->regs);
 		kfree(pdata);
 		return -EINVAL;
 	}
@@ -130,6 +132,7 @@ static int __init omap2_gpio_dev_init(struct omap_hwmod *oh, void *unused)
 
 	pdev = omap_device_build(name, id - 1, oh, pdata,
 				sizeof(*pdata),	NULL, 0, false);
+	kfree(pdata->regs);
 	kfree(pdata);
 
 	if (IS_ERR(pdev)) {



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

* Re: [PATCH] gpio/omap: fix possible memory leak in omap2_gpio_dev_init()
  2012-09-03 10:13 [PATCH] gpio/omap: fix possible memory leak in omap2_gpio_dev_init() Wei Yongjun
@ 2012-09-03 11:11 ` Russell King - ARM Linux
  0 siblings, 0 replies; 3+ messages in thread
From: Russell King - ARM Linux @ 2012-09-03 11:11 UTC (permalink / raw)
  To: Wei Yongjun
  Cc: tony, yongjun_wei, linux-omap, linux-arm-kernel, Paul Walmsley

On Mon, Sep 03, 2012 at 06:13:33PM +0800, Wei Yongjun wrote:
> @@ -130,6 +132,7 @@ static int __init omap2_gpio_dev_init(struct omap_hwmod *oh, void *unused)
>  
>  	pdev = omap_device_build(name, id - 1, oh, pdata,
>  				sizeof(*pdata),	NULL, 0, false);
> +	kfree(pdata->regs);
>  	kfree(pdata);

This one looks wrong.  pdata is copied by the internals of
omap_device_build(), which includes the 'regs' pointer, so it should not
be freed after a successful call to omap_device_build().  (It should,
however, be freed after an unsuccessful call.)

BTW, you want to copy Paul Walmsley with this patch (address in Cc).

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

* Re: [PATCH] gpio/omap: fix possible memory leak in omap2_gpio_dev_init()
@ 2012-09-03 12:36 Wei Yongjun
  0 siblings, 0 replies; 3+ messages in thread
From: Wei Yongjun @ 2012-09-03 12:36 UTC (permalink / raw)
  To: linux; +Cc: tony, yongjun_wei, linux-omap, linux-arm-kernel, paul

On 09/03/2012 07:11 PM, Russell King - ARM Linux wrote:
> On Mon, Sep 03, 2012 at 06:13:33PM +0800, Wei Yongjun wrote:
>> @@ -130,6 +132,7 @@ static int __init omap2_gpio_dev_init(struct omap_hwmod *oh, void *unused)
>>  
>>  	pdev = omap_device_build(name, id - 1, oh, pdata,
>>  				sizeof(*pdata),	NULL, 0, false);
>> +	kfree(pdata->regs);
>>  	kfree(pdata);
> This one looks wrong.  pdata is copied by the internals of
> omap_device_build(), which includes the 'regs' pointer, so it should not
> be freed after a successful call to omap_device_build().  (It should,
> however, be freed after an unsuccessful call.)
>
> BTW, you want to copy Paul Walmsley with this patch (address in Cc).
>
>

Thanks, I will fix this and send a update version.



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

end of thread, other threads:[~2012-09-03 12:36 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2012-09-03 10:13 [PATCH] gpio/omap: fix possible memory leak in omap2_gpio_dev_init() Wei Yongjun
2012-09-03 11:11 ` Russell King - ARM Linux
  -- strict thread matches above, loose matches on Subject: below --
2012-09-03 12:36 Wei Yongjun

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).