Skip to contentSkip to conversation

power: supply: ltc3651-charger: Fix wrong pointer passed to PTR_ERR()

PATCH In review Wei Yongjun 4 participants 5 messages May 17, 2017 – Jun 7, 2017 Power supply

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 power: supply: ltc3651-charger: Fix wrong pointer passed to PTR_ERR()

From: Wei Yongjun <[email protected]>

PTR_ERR should access the value just tested by IS_ERR, otherwise the wrong error code will be returned.

---
 drivers/power/supply/ltc3651-charger.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/power/supply/ltc3651-charger.c +3 -3
index 5f8d5c0..eea63ff 100644
@@ -110,21 +110,21 @@ static int ltc3651_charger_probe(struct platform_device *pdev)
 	ltc3651_charger->acpr_gpio = devm_gpiod_get(&pdev->dev,
 					"lltc,acpr", GPIOD_IN);
 	if (IS_ERR(ltc3651_charger->acpr_gpio)) {
-		ret = PTR_ERR(ltc3651_charger->charger);
+		ret = PTR_ERR(ltc3651_charger->acpr_gpio);
 		dev_err(&pdev->dev, "Failed to acquire acpr GPIO: %d\n", ret);
 		return ret;
 	}
 	ltc3651_charger->fault_gpio = devm_gpiod_get_optional(&pdev->dev,
 					"lltc,fault", GPIOD_IN);
 	if (IS_ERR(ltc3651_charger->fault_gpio)) {
-		ret = PTR_ERR(ltc3651_charger->charger);
+		ret = PTR_ERR(ltc3651_charger->fault_gpio);
 		dev_err(&pdev->dev, "Failed to acquire fault GPIO: %d\n", ret);
 		return ret;
 	}
 	ltc3651_charger->chrg_gpio = devm_gpiod_get_optional(&pdev->dev,
 					"lltc,chrg", GPIOD_IN);
 	if (IS_ERR(ltc3651_charger->chrg_gpio)) {
-		ret = PTR_ERR(ltc3651_charger->charger);
+		ret = PTR_ERR(ltc3651_charger->chrg_gpio);
 		dev_err(&pdev->dev, "Failed to acquire chrg GPIO: %d\n", ret);
 		return ret;
 	}
#1 lore raw
Quoted 6 lines · Wei Yongjun

From: Wei Yongjun <[email protected]>

PTR_ERR should access the value just tested by IS_ERR, otherwise the wrong error code will be returned.

This does fix the issue, but maybe we want to use PTR_ERR_OR_ZERO()?

	ltc3651_charger->acpr_gpio = devm_gpiod_get(&pdev->dev,
					"lltc,acpr", GPIOD_IN);
	ret = PTR_ERR_OR_ZERO(ltc3651_charger->acpr_gpio);
	if (ret)
		...

Or even

	err = PTR_ERR_OR_ZERO((ltc3651_charger->acpr_gpio =
		devm_gpiod_get(&pdev->dev, "lltc,acpr", GPIOD_IN)));
	if (err)
		...

To make sure we never mix up pointer. Maybe we need ERR_OR_ASSIGN(ptr1, ptr2) to avoid the ugly assignment in argument above...

Thanks.

Quoted 34 lines
---
 drivers/power/supply/ltc3651-charger.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/power/supply/ltc3651-charger.c +3 -3
index 5f8d5c0..eea63ff 100644
@@ -110,21 +110,21 @@ static int ltc3651_charger_probe(struct platform_device *pdev)
 	ltc3651_charger->acpr_gpio = devm_gpiod_get(&pdev->dev,
 					"lltc,acpr", GPIOD_IN);
 	if (IS_ERR(ltc3651_charger->acpr_gpio)) {
-		ret = PTR_ERR(ltc3651_charger->charger);
+		ret = PTR_ERR(ltc3651_charger->acpr_gpio);
 		dev_err(&pdev->dev, "Failed to acquire acpr GPIO: %d\n", ret);
 		return ret;
 	}
 	ltc3651_charger->fault_gpio = devm_gpiod_get_optional(&pdev->dev,
 					"lltc,fault", GPIOD_IN);
 	if (IS_ERR(ltc3651_charger->fault_gpio)) {
-		ret = PTR_ERR(ltc3651_charger->charger);
+		ret = PTR_ERR(ltc3651_charger->fault_gpio);
 		dev_err(&pdev->dev, "Failed to acquire fault GPIO: %d\n", ret);
 		return ret;
 	}
 	ltc3651_charger->chrg_gpio = devm_gpiod_get_optional(&pdev->dev,
 					"lltc,chrg", GPIOD_IN);
 	if (IS_ERR(ltc3651_charger->chrg_gpio)) {
-		ret = PTR_ERR(ltc3651_charger->charger);
+		ret = PTR_ERR(ltc3651_charger->chrg_gpio);
 		dev_err(&pdev->dev, "Failed to acquire chrg GPIO: %d\n", ret);
 		return ret;
 	}
Signature · 1 line
Dmitry
#2 lore raw

Hi, Dmitry

Quoted 21 lines

Subject: Re: [PATCH -next] power: supply: ltc3651-charger: Fix wrong pointer passed to PTR_ERR()

Quoted 6 lines · Wei Yongjun

From: Wei Yongjun <[email protected]>

PTR_ERR should access the value just tested by IS_ERR, otherwise the wrong error code will be returned.

This does fix the issue, but maybe we want to use PTR_ERR_OR_ZERO()?

	ltc3651_charger->acpr_gpio = devm_gpiod_get(&pdev->dev,
					"lltc,acpr", GPIOD_IN);
	ret = PTR_ERR_OR_ZERO(ltc3651_charger->acpr_gpio);
	if (ret)
		...

Or even

Quoted 9 lines
	err = PTR_ERR_OR_ZERO((ltc3651_charger->acpr_gpio =
		devm_gpiod_get(&pdev->dev, "lltc,acpr", GPIOD_IN)));
	if (err)
		...

To make sure we never mix up pointer. Maybe we need ERR_OR_ASSIGN(ptr1,
ptr2) to avoid the ugly assignment in argument above...

I have checked devm_gpiod_get() and seems it never return NULL. And the other places call devm_gpiod_get() never check for NULL return. So I think PTR_ERR_OR_ZERO() is not need here.

Regards,
Yongjun Wei

#3 lore raw

Hi Wei,

Quoted 28 lines · weiyongjun (A)

Hi, Dmitry

Quoted 26 lines

Subject: Re: [PATCH -next] power: supply: ltc3651-charger: Fix wrong pointer passed to PTR_ERR()

Quoted 6 lines · Wei Yongjun

From: Wei Yongjun <[email protected]>

PTR_ERR should access the value just tested by IS_ERR, otherwise the wrong error code will be returned.

This does fix the issue, but maybe we want to use PTR_ERR_OR_ZERO()?

	ltc3651_charger->acpr_gpio = devm_gpiod_get(&pdev->dev,
					"lltc,acpr", GPIOD_IN);
	ret = PTR_ERR_OR_ZERO(ltc3651_charger->acpr_gpio);
	if (ret)
		...

Or even

	err = PTR_ERR_OR_ZERO((ltc3651_charger->acpr_gpio =
		devm_gpiod_get(&pdev->dev, "lltc,acpr", GPIOD_IN)));
	if (err)
		...
Quoted 8 lines
Quoted 4 lines

To make sure we never mix up pointer. Maybe we need ERR_OR_ASSIGN(ptr1,
ptr2) to avoid the ugly assignment in argument above...

I have checked devm_gpiod_get() and seems it never return NULL. And the other places call devm_gpiod_get() never check for NULL return. So I think PTR_ERR_OR_ZERO() is not need here.

The request to use PTR_ERR_OR_ZERO() had nothing to do with devm_gpiod_get() returning NULL and everything with reducing number of times one has to write "ltc3651_charger->acpr_gpio". The original problem was that we checked the wrong value, if we provide macro incorporating assignment and conversion to an error code in one step this will reduce number of times we screw up like that in the future.

Thanks.

Signature · 1 line
Dmitry
#4 lore raw
In reply to Wei Yongjun

Hi,

Quoted 4 lines · Wei Yongjun

From: Wei Yongjun <[email protected]>

PTR_ERR should access the value just tested by IS_ERR, otherwise the wrong error code will be returned.

I queued the same patch from Dan, which had the better description incl. a Fixes, but added your Signed-off-by.

-- Sebastian