[PATCH] [ACPI] fan_core: Add error handling for acpi_driver_data

Haoran Liu posted 1 patch 2 years ago
drivers/acpi/fan_core.c | 13 +++++++++++++
1 file changed, 13 insertions(+)
[PATCH] [ACPI] fan_core: Add error handling for acpi_driver_data
Posted by Haoran Liu 2 years ago
This patch introduces error handling for the acpi_driver_data call
in function acpi_fan_get_fif and acpi_fan_get_fps, within
drivers/acpi/fan_core.c. Previously, there was no check for a null
return from acpi_driver_data, which could lead to potential
instability in scenarios where acpi_driver_data fails.

Signed-off-by: Haoran Liu <liuhaoran14@163.com>
---
 drivers/acpi/fan_core.c | 13 +++++++++++++
 1 file changed, 13 insertions(+)

diff --git a/drivers/acpi/fan_core.c b/drivers/acpi/fan_core.c
index 9dccbae9e8ea..f3228fb9c90f 100644
--- a/drivers/acpi/fan_core.c
+++ b/drivers/acpi/fan_core.c
@@ -215,6 +215,13 @@ static int acpi_fan_get_fif(struct acpi_device *device)
 {
 	struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
 	struct acpi_fan *fan = acpi_driver_data(device);
+
+	if (!fan) {
+		dev_err(&device->dev, "No ACPI fan data associated "
+			"with the device\n");
+		return -EINVAL;
+	}
+
 	struct acpi_buffer format = { sizeof("NNNN"), "NNNN" };
 	u64 fields[4];
 	struct acpi_buffer fif = { sizeof(fields), fields };
@@ -265,6 +272,12 @@ static int acpi_fan_speed_cmp(const void *a, const void *b)
 static int acpi_fan_get_fps(struct acpi_device *device)
 {
 	struct acpi_fan *fan = acpi_driver_data(device);
+
+	if (!fan) {
+		dev_err(&device->dev, "Failed to retrieve ACPI fan data\n");
+		return -ENODEV;
+	}
+
 	struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
 	union acpi_object *obj;
 	acpi_status status;
-- 
2.17.1
Re: [PATCH] [ACPI] fan_core: Add error handling for acpi_driver_data
Posted by Rafael J. Wysocki 2 years ago
On Wed, Nov 29, 2023 at 9:14 AM Haoran Liu <liuhaoran14@163.com> wrote:
>
> This patch introduces error handling for the acpi_driver_data call
> in function acpi_fan_get_fif and acpi_fan_get_fps, within
> drivers/acpi/fan_core.c. Previously, there was no check for a null
> return from acpi_driver_data, which could lead to potential
> instability in scenarios where acpi_driver_data fails.

This needs to explain why/how it is possible for acpi_driver_data() to
return NULL in the 2 places modified by the patch.

> Signed-off-by: Haoran Liu <liuhaoran14@163.com>
> ---
>  drivers/acpi/fan_core.c | 13 +++++++++++++
>  1 file changed, 13 insertions(+)
>
> diff --git a/drivers/acpi/fan_core.c b/drivers/acpi/fan_core.c
> index 9dccbae9e8ea..f3228fb9c90f 100644
> --- a/drivers/acpi/fan_core.c
> +++ b/drivers/acpi/fan_core.c
> @@ -215,6 +215,13 @@ static int acpi_fan_get_fif(struct acpi_device *device)
>  {
>         struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
>         struct acpi_fan *fan = acpi_driver_data(device);
> +
> +       if (!fan) {
> +               dev_err(&device->dev, "No ACPI fan data associated "
> +                       "with the device\n");
> +               return -EINVAL;
> +       }
> +
>         struct acpi_buffer format = { sizeof("NNNN"), "NNNN" };
>         u64 fields[4];
>         struct acpi_buffer fif = { sizeof(fields), fields };
> @@ -265,6 +272,12 @@ static int acpi_fan_speed_cmp(const void *a, const void *b)
>  static int acpi_fan_get_fps(struct acpi_device *device)
>  {
>         struct acpi_fan *fan = acpi_driver_data(device);
> +
> +       if (!fan) {
> +               dev_err(&device->dev, "Failed to retrieve ACPI fan data\n");
> +               return -ENODEV;
> +       }
> +
>         struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
>         union acpi_object *obj;
>         acpi_status status;
> --
> 2.17.1
>