Skip to contentSkip to conversation

platform: x86: huawei-wmi: check the return value of device_create_file()

PATCH Superseded Jia-Ju Bai 2 participants 3 messages Feb 25, 2022 – Mar 3, 2022 x86 platform drivers

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Jia-Ju Bai

author
#0 lore raw

PATCH 1/1 platform: x86: huawei-wmi: check the return value of device_create_file()

The function device_create_file() in huawei_wmi_battery_add() can fail, so its return value should be checked.

Fixes 355a070b09ab ("platform/x86: huawei-wmi: Add battery charging thresholds")
---
 drivers/platform/x86/huawei-wmi.c | 15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)
drivers/platform/x86/huawei-wmi.c +12 -3
index a2d846c4a7ee..2ffd3840f3e8 100644
@@ -470,10 +470,19 @@ static DEVICE_ATTR_RW(charge_control_thresholds);
 
 static int huawei_wmi_battery_add(struct power_supply *battery)
 {
-	device_create_file(&battery->dev, &dev_attr_charge_control_start_threshold);
-	device_create_file(&battery->dev, &dev_attr_charge_control_end_threshold);
+	int err = 0; 
+	err = device_create_file(&battery->dev, 
+						&dev_attr_charge_control_start_threshold);
+	if (err)
+		return err;
+	err = device_create_file(&battery->dev, 
+						&dev_attr_charge_control_end_threshold);
+	if (err) {
+		device_remove_file(&battery->dev, 
+						&dev_attr_charge_control_start_threshold);
+	}
 
-	return 0;
+	return err;
 }
 
 static int huawei_wmi_battery_remove(struct power_supply *battery)
Signature · 1 line
2.17.1
#1 lore raw

Hi,

Quoted 6 lines · Jia-Ju Bai

The function device_create_file() in huawei_wmi_battery_add() can fail, so its return value should be checked.

Fixes 355a070b09ab ("platform/x86: huawei-wmi: Add battery charging thresholds")

Please run check-patch.pl your patches before submitting them.

Quoted 9 lines
---
 drivers/platform/x86/huawei-wmi.c | 15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)
drivers/platform/x86/huawei-wmi.c +0 -0
index a2d846c4a7ee..2ffd3840f3e8 100644

drivers/platform/x86/huawei-wmi.c static DEVICE_ATTR_RW(charge_control_thresholds); · line 473

Quoted 6 lines
drivers/platform/x86/huawei-wmi.c +1 -2
@@ -470,10 +470,19 @@ static DEVICE_ATTR_RW(charge_control_thresholds);
 
 static int huawei_wmi_battery_add(struct power_supply *battery)
 {
-	device_create_file(&battery->dev, &dev_attr_charge_control_start_threshold);
-	device_create_file(&battery->dev, &dev_attr_charge_control_end_threshold);
+	int err = 0; 

There needs to be an empty line here,

drivers/platform/x86/huawei-wmi.c static DEVICE_ATTR_RW(charge_control_thresholds); · line 475

Quoted 2 lines
drivers/platform/x86/huawei-wmi.c +2 -0
+	err = device_create_file(&battery->dev, 
+						&dev_attr_charge_control_start_threshold);

Why the weird continuation of the call on another line? Please make this a single line.

drivers/platform/x86/huawei-wmi.c static DEVICE_ATTR_RW(charge_control_thresholds); · line 477

Quoted 2 lines
drivers/platform/x86/huawei-wmi.c +2 -0
+	if (err)
+		return err;

Empty line here.

drivers/platform/x86/huawei-wmi.c static DEVICE_ATTR_RW(charge_control_thresholds); · line 479

Quoted 2 lines
drivers/platform/x86/huawei-wmi.c +2 -0
+	err = device_create_file(&battery->dev, 
+						&dev_attr_charge_control_end_threshold);

Please make this a single line.

drivers/platform/x86/huawei-wmi.c static DEVICE_ATTR_RW(charge_control_thresholds); · line 483

Quoted 4 lines
drivers/platform/x86/huawei-wmi.c +4 -0
+	if (err) {
+		device_remove_file(&battery->dev, 
+						&dev_attr_charge_control_start_threshold);
+	}

No need for {} here.

drivers/platform/x86/huawei-wmi.c static DEVICE_ATTR_RW(charge_control_thresholds); · line 488

Quoted 6 lines
drivers/platform/x86/huawei-wmi.c +1 -1
-	return 0;
+	return err;
 }
 
 static int huawei_wmi_battery_remove(struct power_supply *battery)

Regards,

Hans

author
#2 lore raw

Hi Hans,

Thanks for the advice!
I will send a V2 patch.

Best wishes,
Jia-Ju Bai

Quoted 56 lines · Hans de Goede

Hi,

Quoted 6 lines · Jia-Ju Bai

The function device_create_file() in huawei_wmi_battery_add() can fail, so its return value should be checked.

Fixes 355a070b09ab ("platform/x86: huawei-wmi: Add battery charging thresholds")

Please run check-patch.pl your patches before submitting them.

Quoted 9 lines
---
  drivers/platform/x86/huawei-wmi.c | 15 ++++++++++++---
  1 file changed, 12 insertions(+), 3 deletions(-)
drivers/platform/x86/huawei-wmi.c +0 -0
index a2d846c4a7ee..2ffd3840f3e8 100644

drivers/platform/x86/huawei-wmi.c static DEVICE_ATTR_RW(charge_control_thresholds); · line 473

Quoted 6 lines
drivers/platform/x86/huawei-wmi.c +1 -2
@@ -470,10 +470,19 @@ static DEVICE_ATTR_RW(charge_control_thresholds);
 
  static int huawei_wmi_battery_add(struct power_supply *battery)
  {
-	device_create_file(&battery->dev, &dev_attr_charge_control_start_threshold);
-	device_create_file(&battery->dev, &dev_attr_charge_control_end_threshold);
+	int err = 0;

There needs to be an empty line here,

drivers/platform/x86/huawei-wmi.c static DEVICE_ATTR_RW(charge_control_thresholds); · line 475

Quoted 2 lines
drivers/platform/x86/huawei-wmi.c +2 -0
+	err = device_create_file(&battery->dev,
+						&dev_attr_charge_control_start_threshold);

Why the weird continuation of the call on another line? Please make this a single line.

drivers/platform/x86/huawei-wmi.c static DEVICE_ATTR_RW(charge_control_thresholds); · line 477

Quoted 2 lines
drivers/platform/x86/huawei-wmi.c +2 -0
+	if (err)
+		return err;

Empty line here.

drivers/platform/x86/huawei-wmi.c static DEVICE_ATTR_RW(charge_control_thresholds); · line 479

Quoted 2 lines
drivers/platform/x86/huawei-wmi.c +2 -0
+	err = device_create_file(&battery->dev,
+						&dev_attr_charge_control_end_threshold);

Please make this a single line.

drivers/platform/x86/huawei-wmi.c static DEVICE_ATTR_RW(charge_control_thresholds); · line 483

Quoted 4 lines
drivers/platform/x86/huawei-wmi.c +4 -0
+	if (err) {
+		device_remove_file(&battery->dev,
+						&dev_attr_charge_control_start_threshold);
+	}

No need for {} here.

drivers/platform/x86/huawei-wmi.c static DEVICE_ATTR_RW(charge_control_thresholds); · line 488

Quoted 6 lines
drivers/platform/x86/huawei-wmi.c +1 -1
-	return 0;
+	return err;
  }
 
  static int huawei_wmi_battery_remove(struct power_supply *battery)

Regards,

Hans