diff mbox

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

Message ID 20170517121132.7052-1-weiyj.lk@gmail.com (mailing list archive)
State Not Applicable, archived
Headers show

Commit Message

Wei Yongjun May 17, 2017, 12:11 p.m. UTC
From: Wei Yongjun <weiyongjun1@huawei.com>

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

Signed-off-by: Wei Yongjun <weiyongjun1@huawei.com>
---
 drivers/power/supply/ltc3651-charger.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

Comments

Dmitry Torokhov May 19, 2017, 12:59 a.m. UTC | #1
On Wed, May 17, 2017 at 12:11:32PM +0000, Wei Yongjun wrote:
> From: Wei Yongjun <weiyongjun1@huawei.com>
> 
> PTR_ERR should access the value just tested by IS_ERR, otherwise
> the wrong error code will be returned.
> 
> Signed-off-by: Wei Yongjun <weiyongjun1@huawei.com>

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.

> ---
>  drivers/power/supply/ltc3651-charger.c | 6 +++---
>  1 file changed, 3 insertions(+), 3 deletions(-)
> 
> diff --git a/drivers/power/supply/ltc3651-charger.c b/drivers/power/supply/ltc3651-charger.c
> index 5f8d5c0..eea63ff 100644
> --- a/drivers/power/supply/ltc3651-charger.c
> +++ b/drivers/power/supply/ltc3651-charger.c
> @@ -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;
>  	}
>
Wei Yongjun May 19, 2017, 1:27 a.m. UTC | #2
Hi, Dmitry

> Subject: Re: [PATCH -next] power: supply: ltc3651-charger: Fix wrong pointer
> passed to PTR_ERR()
> 
> On Wed, May 17, 2017 at 12:11:32PM +0000, Wei Yongjun wrote:
> > From: Wei Yongjun <weiyongjun1@huawei.com>
> >
> > PTR_ERR should access the value just tested by IS_ERR, otherwise
> > the wrong error code will be returned.
> >
> > Signed-off-by: Wei Yongjun <weiyongjun1@huawei.com>
> 
> 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...
> 

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
Dmitry Torokhov May 19, 2017, 10:07 p.m. UTC | #3
Hi Wei,

On Fri, May 19, 2017 at 01:27:51AM +0000, weiyongjun (A) wrote:
> Hi, Dmitry
> 
> > Subject: Re: [PATCH -next] power: supply: ltc3651-charger: Fix wrong pointer
> > passed to PTR_ERR()
> > 
> > On Wed, May 17, 2017 at 12:11:32PM +0000, Wei Yongjun wrote:
> > > From: Wei Yongjun <weiyongjun1@huawei.com>
> > >
> > > PTR_ERR should access the value just tested by IS_ERR, otherwise
> > > the wrong error code will be returned.
> > >
> > > Signed-off-by: Wei Yongjun <weiyongjun1@huawei.com>
> > 
> > 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...
> > 
> 
> 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.
Sebastian Reichel June 7, 2017, 8:16 p.m. UTC | #4
Hi,

On Wed, May 17, 2017 at 12:11:32PM +0000, Wei Yongjun wrote:
> From: Wei Yongjun <weiyongjun1@huawei.com>
> 
> 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
diff mbox

Patch

diff --git a/drivers/power/supply/ltc3651-charger.c b/drivers/power/supply/ltc3651-charger.c
index 5f8d5c0..eea63ff 100644
--- a/drivers/power/supply/ltc3651-charger.c
+++ b/drivers/power/supply/ltc3651-charger.c
@@ -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;
 	}