2021-04-14 14:00:37

by zhuguangqing83

[permalink] [raw]
Subject: [PATCH] power: supply: cpcap-battery: fix invalid usage of list cursor

From: Guangqing Zhu <[email protected]>

Fix invalid usage of a list_for_each_entry in cpcap_battery_irq_thread().
When list is empty or if the list is completely traversed (without breaking
from the loop on one of the entries) then the list cursor does not point
to a valid entry and therefore should not be used. "if (!d)" will never
happen, maybe we should use "if (&d->node == &ddata->irq_list)".

Fixes: 874b2adbed125 ("power: supply: cpcap-battery: Add a battery driver")
Signed-off-by: Guangqing Zhu <[email protected]>
---
drivers/power/supply/cpcap-battery.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/power/supply/cpcap-battery.c b/drivers/power/supply/cpcap-battery.c
index 6d5bcdb9f45d..8169dc71a786 100644
--- a/drivers/power/supply/cpcap-battery.c
+++ b/drivers/power/supply/cpcap-battery.c
@@ -786,7 +786,7 @@ static irqreturn_t cpcap_battery_irq_thread(int irq, void *data)
break;
}

- if (!d)
+ if (&d->node == &ddata->irq_list)
return IRQ_NONE;

latest = cpcap_battery_latest(ddata);
--
2.17.1


2021-04-20 14:14:26

by Sebastian Reichel

[permalink] [raw]
Subject: Re: [PATCH] power: supply: cpcap-battery: fix invalid usage of list cursor

[+Cc Tony]

Hi,

On Wed, Apr 14, 2021 at 02:47:24PM +0800, [email protected] wrote:
> From: Guangqing Zhu <[email protected]>
>
> Fix invalid usage of a list_for_each_entry in cpcap_battery_irq_thread().
> When list is empty or if the list is completely traversed (without breaking
> from the loop on one of the entries) then the list cursor does not point
> to a valid entry and therefore should not be used. "if (!d)" will never
> happen, maybe we should use "if (&d->node == &ddata->irq_list)".

Commit message does not make sense. I think you meant to say:

Empty list or fully traversed list points to list head, which is not
NULL (and before the first element containing real data).

> Fixes: 874b2adbed125 ("power: supply: cpcap-battery: Add a battery driver")
> Signed-off-by: Guangqing Zhu <[email protected]>
> ---
> drivers/power/supply/cpcap-battery.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/drivers/power/supply/cpcap-battery.c b/drivers/power/supply/cpcap-battery.c
> index 6d5bcdb9f45d..8169dc71a786 100644
> --- a/drivers/power/supply/cpcap-battery.c
> +++ b/drivers/power/supply/cpcap-battery.c
> @@ -786,7 +786,7 @@ static irqreturn_t cpcap_battery_irq_thread(int irq, void *data)
> break;
> }
>
> - if (!d)
> + if (&d->node == &ddata->irq_list)
> return IRQ_NONE;

Please use list_entry_is_head() instead of open coding it.

> latest = cpcap_battery_latest(ddata);

Thanks,

-- Sebastian


Attachments:
(No filename) (1.48 kB)
signature.asc (849.00 B)
Download all attachments