Skip to contentSkip to conversation

gpio/omap: fix possible memory leak in omap2_gpio_dev_init()

PATCH Superseded Wei Yongjun 2 participants 2 messages Sep 3, 2012

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 gpio/omap: fix possible memory leak in omap2_gpio_dev_init()

From: Wei Yongjun <[email protected]>

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/)

---
 arch/arm/mach-omap2/gpio.c | 3 +++
 1 file changed, 3 insertions(+)
arch/arm/mach-omap2/gpio.c +3 -0
index 9ad7d48..3bfa4bb 100644
@@ -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)) {
#1 lore raw

PATCH 1/1 gpio/omap: fix possible memory leak in omap2_gpio_dev_init()

arch/arm/mach-omap2/gpio.c static int __init omap2_gpio_dev_init(struct omap_hwmod *oh, void *unused) · line 136

Quoted 6 lines · Wei Yongjun
arch/arm/mach-omap2/gpio.c +1 -0
@@ -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).