Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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>

authored by

Sumeet Pawnikar and committed by
Rafael J. Wysocki
efc4c35b 7bda1910

+3 -3
+3 -3
drivers/powercap/powercap_sys.c
··· 68 68 int id; \ 69 69 struct powercap_zone_constraint *pconst;\ 70 70 \ 71 - if (!sscanf(dev_attr->attr.name, "constraint_%d_", &id)) \ 71 + if (sscanf(dev_attr->attr.name, "constraint_%d_", &id) != 1) \ 72 72 return -EINVAL; \ 73 73 if (id >= power_zone->const_id_cnt) \ 74 74 return -EINVAL; \ ··· 93 93 int id; \ 94 94 struct powercap_zone_constraint *pconst;\ 95 95 \ 96 - if (!sscanf(dev_attr->attr.name, "constraint_%d_", &id)) \ 96 + if (sscanf(dev_attr->attr.name, "constraint_%d_", &id) != 1) \ 97 97 return -EINVAL; \ 98 98 if (id >= power_zone->const_id_cnt) \ 99 99 return -EINVAL; \ ··· 162 162 ssize_t len = -ENODATA; 163 163 struct powercap_zone_constraint *pconst; 164 164 165 - if (!sscanf(dev_attr->attr.name, "constraint_%d_", &id)) 165 + if (sscanf(dev_attr->attr.name, "constraint_%d_", &id) != 1) 166 166 return -EINVAL; 167 167 if (id >= power_zone->const_id_cnt) 168 168 return -EINVAL;