Skip to contentSkip to conversation

ACPI: battery: Avoid dereferencing battery before NULL check

PATCH In review Alper Ak 2 participants 2 messages Feb 9 ACPI

lore
Share & tools
lore

Alper Ak

author
#0 lore raw

PATCH 1/1 ACPI: battery: Avoid dereferencing battery before NULL check

acpi_battery_notify() dereferences the battery pointer before checking for NULL. Reorder the check to ensure the pointer is validated before use.

This issue was reported by the Smatch static analyzer.

Fixes 08b54fd57782 ("ACPI: battery: Adjust event notification routine")
---
 drivers/acpi/battery.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/acpi/battery.c +3 -1
index 3bbddd6f622c..abc82be69215 100644
@@ -1063,12 +1063,14 @@ static void acpi_battery_refresh(struct acpi_battery *battery)
 static void acpi_battery_notify(acpi_handle handle, u32 event, void *data)
 {
 	struct acpi_battery *battery = data;
-	struct acpi_device *device = battery->device;
+	struct acpi_device *device;
 	struct power_supply *old;
 
 	if (!battery)
 		return;
 
+	device = battery->device;
+
 	guard(mutex)(&battery->update_lock);
 
 	old = battery->bat;
Signature · 1 line
2.43.0
#1 lore raw
Quoted 4 lines · Alper Ak

acpi_battery_notify() dereferences the battery pointer before checking for NULL. Reorder the check to ensure the pointer is validated before use.

The check is redundant, please see:

https://lore.kernel.org/linux-acpi/[email protected]/

Quoted 32 lines

This issue was reported by the Smatch static analyzer.

Fixes 08b54fd57782 ("ACPI: battery: Adjust event notification routine")
---
 drivers/acpi/battery.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/acpi/battery.c +3 -2
index 3bbddd6f622c..abc82be69215 100644
@@ -1063,12 +1063,14 @@ static void acpi_battery_refresh(struct acpi_battery *battery)
 static void acpi_battery_notify(acpi_handle handle, u32 event, void *data)
 {
        struct acpi_battery *battery = data;
-       struct acpi_device *device = battery->device;
+       struct acpi_device *device;
        struct power_supply *old;
 
        if (!battery)
                return;
 
+       device = battery->device;
+
        guard(mutex)(&battery->update_lock);
 
        old = battery->bat;
--

2.43.0