[01/15] power-supply: Don't over-allocate memory for "supplied-from" array

Message ID 2eacd908a8094887cc1796f75ae0513be8a3e36d.1409831636.git.viresh.kumar@linaro.org
State Accepted
Commit 8f5a37cb28fce189f3d6802ade98a116f59a47bf
Headers show

Commit Message

Viresh Kumar Sept. 4, 2014, 12:01 p.m.
In routine power_supply_check_supplies(), 'cnt' is counting the number of
supplies passed in "power-supplies" field of a node. The value of 'cnt' will
always be one more than the number of supplies after the do-while loop ends. And
so we need to allocate memory for 'cnt - 1' char pointers. But we are allocating
memory for 'cnt' instead.

Fix this by not over-allocating memory.

Signed-off-by: Viresh Kumar <viresh.kumar@linaro.org>
---
 drivers/power/power_supply_core.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Patch

diff --git a/drivers/power/power_supply_core.c b/drivers/power/power_supply_core.c
index 078afd6..10f0b57 100644
--- a/drivers/power/power_supply_core.c
+++ b/drivers/power/power_supply_core.c
@@ -234,7 +234,7 @@  static int power_supply_check_supplies(struct power_supply *psy)
 		return -ENOMEM;
 	}
 
-	*psy->supplied_from = devm_kzalloc(psy->dev, sizeof(char *) * cnt,
+	*psy->supplied_from = devm_kzalloc(psy->dev, sizeof(char *) * (cnt - 1),
 					   GFP_KERNEL);
 	if (!*psy->supplied_from) {
 		dev_err(psy->dev, "Couldn't allocate memory for supply list\n");