[PATCH 1/3] power_supply: scrub device pointer if registration fails

From: Stefan Hajnoczi
Date: Tue Jul 12 2011 - 04:04:14 EST


This patch makes power_supply_register() safer for callers that are not
being careful. When the function fails it leaves the caller's psy.dev
pointer set to the stale power supply device. A correct caller would
handle the error return and never use psy.dev but the example of
drivers/acpi/battery.c shows otherwise.

Clear the psy.dev pointer when power_supply_register() fails so the
caller either sees a valid pointer on success or NULL on failure.

Signed-off-by: Stefan Hajnoczi <stefanha@xxxxxxxxxxxxxxxxxx>
---
drivers/power/power_supply_core.c | 1 +
1 files changed, 1 insertions(+), 0 deletions(-)

diff --git a/drivers/power/power_supply_core.c b/drivers/power/power_supply_core.c
index 329b46b..33d4068 100644
--- a/drivers/power/power_supply_core.c
+++ b/drivers/power/power_supply_core.c
@@ -194,6 +194,7 @@ create_triggers_failed:
kobject_set_name_failed:
device_add_failed:
put_device(dev);
+ psy->dev = NULL; /* make it crystal-clear that we failed */
success:
return rc;
}
--
1.7.5.4

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/