Re: [PATCH] ACPI: scan: Add missing check for kstrdup()

From: Rafael J. Wysocki
Date: Tue May 14 2024 - 09:03:54 EST


On Sat, May 11, 2024 at 9:36 PM Jiasheng Jiang
<jiashengjiangcool@xxxxxxxxxxx> wrote:
>
> Add check for the return value of kstrdup() in order to gurantee
> the success of allocation.
> Moreover, move the code forward to simplify the error handling.
>
> Fixes: ccf78040265b ("ACPI: Add _UID support for ACPI devices.")
> Signed-off-by: Jiasheng Jiang <jiashengjiangcool@xxxxxxxxxxx>
> ---
> drivers/acpi/scan.c | 12 +++++++++---
> 1 file changed, 9 insertions(+), 3 deletions(-)
>
> diff --git a/drivers/acpi/scan.c b/drivers/acpi/scan.c
> index d1464324de95..59246757a207 100644
> --- a/drivers/acpi/scan.c
> +++ b/drivers/acpi/scan.c
> @@ -1385,6 +1385,15 @@ static void acpi_set_pnp_ids(acpi_handle handle, struct acpi_device_pnp *pnp,
> return;
> }
>
> + if (info->valid & ACPI_VALID_UID) {
> + pnp->unique_id = kstrdup(info->unique_id.string,
> + GFP_KERNEL);
> + if (!pnp->unique_id) {
> + kfree(info);
> + return;

No, this is not sufficient for the function to return.

> + }
> + }
> +
> if (info->valid & ACPI_VALID_HID) {
> acpi_add_id(pnp, info->hardware_id.string);
> pnp->type.platform_id = 1;
> @@ -1398,9 +1407,6 @@ static void acpi_set_pnp_ids(acpi_handle handle, struct acpi_device_pnp *pnp,
> pnp->bus_address = info->address;
> pnp->type.bus_address = 1;
> }
> - if (info->valid & ACPI_VALID_UID)
> - pnp->unique_id = kstrdup(info->unique_id.string,
> - GFP_KERNEL);

This is optional, so it can be NULL.

> if (info->valid & ACPI_VALID_CLS)
> acpi_add_id(pnp, info->class_code.string);
>
> --