diff mbox

[v2,1/2] power: bq2415x_charger: Properly handle ENODEV from power_supply_get_by_phandle

Message ID 1413383110-20310-1-git-send-email-k.kozlowski@samsung.com (mailing list archive)
State Not Applicable, archived
Headers show

Commit Message

Krzysztof Kozlowski Oct. 15, 2014, 2:25 p.m. UTC
The power_supply_get_by_phandle() on error returns ENODEV or NULL.
The driver later expects obtained pointer to power supply to be
valid or NULL. If it is not NULL then it dereferences it in
bq2415x_notifier_call() which would lead to dereferencing ENODEV-value
pointer.

Properly handle the power_supply_get_by_phandle() error case by
replacing error value with NULL. This indicates that usb charger
detection won't be used.

Fix also memory leak of 'name' if power_supply_get_by_phandle() fails
with NULL and probe should defer.

Signed-off-by: Krzysztof Kozlowski <k.kozlowski@samsung.com>
Fixes: faffd234cf85 ("bq2415x_charger: Add DT support")
Cc: <stable@vger.kernel.org>

---

Changes since v1:
1. Fix memory leak in probe defer (goto error_2) (suggested by Sebastian
   Reichel).
2. Don't fail the probe if 'ti,usb-charger-detection' is not present.
   Just print message.
---
 drivers/power/bq2415x_charger.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)

Comments

Sebastian Reichel Oct. 27, 2014, 5:43 p.m. UTC | #1
Hi,

On Wed, Oct 15, 2014 at 04:25:09PM +0200, Krzysztof Kozlowski wrote:
> The power_supply_get_by_phandle() on error returns ENODEV or NULL.
> The driver later expects obtained pointer to power supply to be
> valid or NULL. If it is not NULL then it dereferences it in
> bq2415x_notifier_call() which would lead to dereferencing ENODEV-value
> pointer.
> 
> Properly handle the power_supply_get_by_phandle() error case by
> replacing error value with NULL. This indicates that usb charger
> detection won't be used.
> 
> Fix also memory leak of 'name' if power_supply_get_by_phandle() fails
> with NULL and probe should defer.

Applied to next.

-- Sebastian
diff mbox

Patch

diff --git a/drivers/power/bq2415x_charger.c b/drivers/power/bq2415x_charger.c
index e384844a1ae1..edf945a5260f 100644
--- a/drivers/power/bq2415x_charger.c
+++ b/drivers/power/bq2415x_charger.c
@@ -1579,8 +1579,15 @@  static int bq2415x_probe(struct i2c_client *client,
 	if (np) {
 		bq->notify_psy = power_supply_get_by_phandle(np, "ti,usb-charger-detection");
 
-		if (!bq->notify_psy)
-			return -EPROBE_DEFER;
+		if (IS_ERR(bq->notify_psy)) {
+			bq->notify_psy = NULL;
+			dev_info(&client->dev,
+				"no 'ti,usb-charger-detection' property (err=%ld)\n",
+				PTR_ERR(bq->notify_psy));
+		} else if (!bq->notify_psy) {
+			ret = -EPROBE_DEFER;
+			goto error_2;
+		}
 	}
 	else if (pdata->notify_device)
 		bq->notify_psy = power_supply_get_by_name(pdata->notify_device);