Skip to content

Commit efc4c35

Browse files
sumeet4linuxrafaeljw
authored andcommitted
powercap: fix sscanf() error return value handling
Fix inconsistent error handling for sscanf() return value check. Implicit boolean conversion is used instead of explicit return value checks. 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> [ rjw: Subject and changelog edits ] Link: https://patch.msgid.link/20251207151549.202452-1-sumeet4linux@gmail.com Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
1 parent 7bda191 commit efc4c35

1 file changed

Lines changed: 3 additions & 3 deletions

File tree

drivers/powercap/powercap_sys.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ static ssize_t show_constraint_##_attr(struct device *dev, \
6868
int id; \
6969
struct powercap_zone_constraint *pconst;\
7070
\
71-
if (!sscanf(dev_attr->attr.name, "constraint_%d_", &id)) \
71+
if (sscanf(dev_attr->attr.name, "constraint_%d_", &id) != 1) \
7272
return -EINVAL; \
7373
if (id >= power_zone->const_id_cnt) \
7474
return -EINVAL; \
@@ -93,7 +93,7 @@ static ssize_t store_constraint_##_attr(struct device *dev,\
9393
int id; \
9494
struct powercap_zone_constraint *pconst;\
9595
\
96-
if (!sscanf(dev_attr->attr.name, "constraint_%d_", &id)) \
96+
if (sscanf(dev_attr->attr.name, "constraint_%d_", &id) != 1) \
9797
return -EINVAL; \
9898
if (id >= power_zone->const_id_cnt) \
9999
return -EINVAL; \
@@ -162,7 +162,7 @@ static ssize_t show_constraint_name(struct device *dev,
162162
ssize_t len = -ENODATA;
163163
struct powercap_zone_constraint *pconst;
164164

165-
if (!sscanf(dev_attr->attr.name, "constraint_%d_", &id))
165+
if (sscanf(dev_attr->attr.name, "constraint_%d_", &id) != 1)
166166
return -EINVAL;
167167
if (id >= power_zone->const_id_cnt)
168168
return -EINVAL;

0 commit comments

Comments
 (0)