Skip to contentSkip to conversation

drivers/power/wm97xx_battery.c: Return -ENOMEM on memory allocation failure

PATCH In review Julia Lawall 1 participant 1 message Oct 15, 2010 Kernel janitors

lore
Share & tools
lore
#0 lore raw

PATCH 4/7 drivers/power/wm97xx_battery.c: Return -ENOMEM on memory allocation failure

From: Julia Lawall <[email protected]>

In this code, 0 is returned on memory allocation failure, even though other failures return -ENOMEM or other similar values.

A simplified version of the semantic match that finds this problem is as
follows: (http://coccinelle.lip6.fr/)

// <smpl>
@@
expression ret;
expression x,e1,e2,e3;
@@

ret = 0
... when != ret = e1
*x = \(kmalloc\|kcalloc\|kzalloc\)(...)
... when != ret = e2
if (x == NULL) { ... when != ret = e3
return ret;
}
// </smpl>

---
 drivers/power/wm97xx_battery.c |    4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/power/wm97xx_battery.c +3 -1
diff -u -p a/drivers/power/wm97xx_battery.c b/drivers/power/wm97xx_battery.c
@@ -215,8 +215,10 @@ static int __devinit wm97xx_bat_probe(st
 		props++;	/* POWER_SUPPLY_PROP_VOLTAGE_MIN */
 
 	prop = kzalloc(props * sizeof(*prop), GFP_KERNEL);
-	if (!prop)
+	if (!prop) {
+		ret = -ENOMEM;
 		goto err3;
+	}
 
 	prop[i++] = POWER_SUPPLY_PROP_PRESENT;
 	if (pdata->charge_gpio >= 0)