platform/x86: huawei-wmi: Fix misuse of strcmp() function
PATCH 1/1 platform/x86: huawei-wmi: Fix misuse of strcmp() function
Comparing the result of strcmp directly with 1 may cause it to be misinterpreted. Note that strcmp may return an integer besides -1, 0, or 1.
Fix this by returning -ENODEV only when strcmp() returns a value other than 0.
Addresses-Coverity-ID: 1487035 ("Misuse of memcmp-style function")
--- drivers/platform/x86/huawei-wmi.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/platform/x86/huawei-wmi.c +1 -1
@@ -470,7 +470,7 @@ static DEVICE_ATTR_RW(charge_control_thresholds);static int huawei_wmi_battery_add (struct power_supply *battery) {/* Huawei laptops come with one battery only */ -if (strcmp (battery->desc->name,"BAT" ) !=1 ) +if (strcmp (battery->desc->name,"BAT" ))return -ENODEV ;device_create_file (&battery->dev, &dev_attr_charge_control_start_threshold);
Signature · 1 line
2.23.0
Hi Gustavo,
Quoted 21 lines · Gustavo A. R. Silva
Comparing the result of strcmp directly with 1 may cause it to be misinterpreted. Note that strcmp may return an integer besides -1, 0, or 1.
Fix this by returning
-ENODEVonly whenstrcmp()returns a value other than 0.Addresses-Coverity-ID: 1487035 ("Misuse of memcmp-style function")
Fixes b7527d0f4502 ("platform/x86: huawei-wmi: Add battery chargingthresholds")
Signed-off-by Gustavo A. R. Silva <[email protected]>--- drivers/platform/x86/huawei-wmi.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)drivers/platform/x86/huawei-wmi.c +0
-0
drivers/platform/x86/huawei-wmi.c static DEVICE_ATTR_RW(charge_control_thresholds); · line 473
Quoted 4 lines
drivers/platform/x86/huawei-wmi.c +0
-1@@ -470,7 +470,7 @@ static DEVICE_ATTR_RW(charge_control_thresholds);static int huawei_wmi_battery_add (struct power_supply *battery) {/* Huawei laptops come with one battery only */ -if (strcmp (battery->desc->name,"BAT" ) !=1 )
Note we don't have a battery number in BATx, strcmp would return 1 if battery->desc->name is "BAT0" or any one digit. This is a desired behavior where some Huawei laptops identify the first battery as "BAT1" and this would match if name is greater than "BAT" by one digit.
Maybe strcmp(battery->desc->name, "BAT") < 0 is a better way to go.
drivers/platform/x86/huawei-wmi.c static DEVICE_ATTR_RW(charge_control_thresholds); · line 474
Quoted 2 lines
drivers/platform/x86/huawei-wmi.c +1
-0+if (strcmp (battery->desc->name,"BAT" ))return -ENODEV ;
Now this would always return ENODEV.
Thank you,
Ayman
Quoted 3 lines
drivers/platform/x86/huawei-wmi.c +0
-0device_create_file (&battery->dev, &dev_attr_charge_control_start_threshold);
Hi Ayman,
Quoted 8 lines · On 10/15/19 14:08, [email protected] wrote:
drivers/platform/x86/huawei-wmi.c static DEVICE_ATTR_RW(charge_control_thresholds); · line 473
Quoted 2 lines
drivers/platform/x86/huawei-wmi.c +0
-1/* Huawei laptops come with one battery only */ -if (strcmp (battery->desc->name,"BAT" ) !=1 )Note we don't have a battery number in BATx, strcmp would return 1 if
battery->desc->nameis "BAT0" or any one digit. This is a desired behavior where some Huawei laptops identify the first battery as "BAT1" and this would match if name is greater than "BAT" by one digit.
Great to know this is not a bug. I don't think there is any other instance in the whole codebase like this one: strcmp(...) != 1 So, that's an odd thing to see... Maybe you can consider adding the whole description above as a code comment? :)
Quoted 2 lines
Maybe
strcmp(battery->desc->name, "BAT")< 0 is a better way to go.
Yeah; this is bit more specific.
Thanks!
--
Gustavo