public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] powercap: sscanf return value error handling
@ 2025-12-07 15:15 Sumeet Pawnikar
  2025-12-15 11:34 ` Rafael J. Wysocki
  0 siblings, 1 reply; 2+ messages in thread
From: Sumeet Pawnikar @ 2025-12-07 15:15 UTC (permalink / raw)
  To: rafael, linux-pm; +Cc: linux-kernel, sumeet4linux

Inconsistent error handling for sscanf() return value check.
It uses implicit boolean conversion instead of explicit return
value check. The code checks if (!sscanf(...)) which is incorrect
because:
 1. sscanf returns the number of successfully parsed items
 2. On success, it returns 1 (one item passed)
 3. On failure, it returns 0 or EOF
 4. The check 'if (!sscanf(...))' is wrong because it treats
    success (1) as failure

All occurrences of sscanf() now uses explicit return value check.
With this behavior it returns '-EINVAL' when parsing fails (returns
0 or EOF), and continues when parsing succeeds (returns 1).

Signed-off-by: Sumeet Pawnikar <sumeet4linux@gmail.com>
---
 drivers/powercap/powercap_sys.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/drivers/powercap/powercap_sys.c b/drivers/powercap/powercap_sys.c
index bdc65e040d17..e998454e4bf6 100644
--- a/drivers/powercap/powercap_sys.c
+++ b/drivers/powercap/powercap_sys.c
@@ -68,7 +68,7 @@ static ssize_t show_constraint_##_attr(struct device *dev, \
 	int id; \
 	struct powercap_zone_constraint *pconst;\
 	\
-	if (!sscanf(dev_attr->attr.name, "constraint_%d_", &id)) \
+	if (sscanf(dev_attr->attr.name, "constraint_%d_", &id) != 1) \
 		return -EINVAL; \
 	if (id >= power_zone->const_id_cnt)	\
 		return -EINVAL; \
@@ -93,7 +93,7 @@ static ssize_t store_constraint_##_attr(struct device *dev,\
 	int id; \
 	struct powercap_zone_constraint *pconst;\
 	\
-	if (!sscanf(dev_attr->attr.name, "constraint_%d_", &id)) \
+	if (sscanf(dev_attr->attr.name, "constraint_%d_", &id) != 1) \
 		return -EINVAL; \
 	if (id >= power_zone->const_id_cnt)	\
 		return -EINVAL; \
@@ -162,7 +162,7 @@ static ssize_t show_constraint_name(struct device *dev,
 	ssize_t len = -ENODATA;
 	struct powercap_zone_constraint *pconst;
 
-	if (!sscanf(dev_attr->attr.name, "constraint_%d_", &id))
+	if (sscanf(dev_attr->attr.name, "constraint_%d_", &id) != 1)
 		return -EINVAL;
 	if (id >= power_zone->const_id_cnt)
 		return -EINVAL;
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] powercap: sscanf return value error handling
  2025-12-07 15:15 [PATCH] powercap: sscanf return value error handling Sumeet Pawnikar
@ 2025-12-15 11:34 ` Rafael J. Wysocki
  0 siblings, 0 replies; 2+ messages in thread
From: Rafael J. Wysocki @ 2025-12-15 11:34 UTC (permalink / raw)
  To: Sumeet Pawnikar; +Cc: rafael, linux-pm, linux-kernel

On Sun, Dec 7, 2025 at 4:16 PM Sumeet Pawnikar <sumeet4linux@gmail.com> wrote:
>
> Inconsistent error handling for sscanf() return value check.
> It uses implicit boolean conversion instead of explicit return
> value check. The code checks if (!sscanf(...)) which is incorrect
> because:
>  1. sscanf returns the number of successfully parsed items
>  2. On success, it returns 1 (one item passed)
>  3. On failure, it returns 0 or EOF
>  4. The check 'if (!sscanf(...))' is wrong because it treats
>     success (1) as failure
>
> All occurrences of sscanf() now uses explicit return value check.
> With this behavior it returns '-EINVAL' when parsing fails (returns
> 0 or EOF), and continues when parsing succeeds (returns 1).
>
> Signed-off-by: Sumeet Pawnikar <sumeet4linux@gmail.com>
> ---
>  drivers/powercap/powercap_sys.c | 6 +++---
>  1 file changed, 3 insertions(+), 3 deletions(-)
>
> diff --git a/drivers/powercap/powercap_sys.c b/drivers/powercap/powercap_sys.c
> index bdc65e040d17..e998454e4bf6 100644
> --- a/drivers/powercap/powercap_sys.c
> +++ b/drivers/powercap/powercap_sys.c
> @@ -68,7 +68,7 @@ static ssize_t show_constraint_##_attr(struct device *dev, \
>         int id; \
>         struct powercap_zone_constraint *pconst;\
>         \
> -       if (!sscanf(dev_attr->attr.name, "constraint_%d_", &id)) \
> +       if (sscanf(dev_attr->attr.name, "constraint_%d_", &id) != 1) \
>                 return -EINVAL; \
>         if (id >= power_zone->const_id_cnt)     \
>                 return -EINVAL; \
> @@ -93,7 +93,7 @@ static ssize_t store_constraint_##_attr(struct device *dev,\
>         int id; \
>         struct powercap_zone_constraint *pconst;\
>         \
> -       if (!sscanf(dev_attr->attr.name, "constraint_%d_", &id)) \
> +       if (sscanf(dev_attr->attr.name, "constraint_%d_", &id) != 1) \
>                 return -EINVAL; \
>         if (id >= power_zone->const_id_cnt)     \
>                 return -EINVAL; \
> @@ -162,7 +162,7 @@ static ssize_t show_constraint_name(struct device *dev,
>         ssize_t len = -ENODATA;
>         struct powercap_zone_constraint *pconst;
>
> -       if (!sscanf(dev_attr->attr.name, "constraint_%d_", &id))
> +       if (sscanf(dev_attr->attr.name, "constraint_%d_", &id) != 1)
>                 return -EINVAL;
>         if (id >= power_zone->const_id_cnt)
>                 return -EINVAL;
> --

Applied as 6.19-rc material, thanks!

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2025-12-15 11:35 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-12-07 15:15 [PATCH] powercap: sscanf return value error handling Sumeet Pawnikar
2025-12-15 11:34 ` Rafael J. Wysocki

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox