Re: [PATCH] cxl: core-pmu: Fix the usage of uninitialized variable
From: Robert Richter
Date: Wed Oct 23 2024 - 08:26:51 EST
On 23.10.24 10:56:10, Zicheng Qu wrote:
> In the devm_cxl_pmu_add() function, the variable rc might be
> uninitialized before its first use 'if (rc) goto err', leading
> to undefined behavior since its value depends on the compiler.
> Currently, the switch statement is limited to the CXL_PMU_MEMDEV type.
> If additional types are introduced, it could lead to similar concerns.
> If the type range remains unchanged, using a switch case is unnecessary.
> To enhance code extensibility and stability, it is recommended to
> address this potential aspect.
>
> Cc: stable@xxxxxxxxxxxxxxx # v6.6+
> Fixes: 1ad3f701c399 ("cxl/pci: Find and register CXL PMU devices")
> Signed-off-by: Zicheng Qu <quzicheng@xxxxxxxxxx>
> ---
> drivers/cxl/core/pmu.c | 3 +++
> 1 file changed, 3 insertions(+)
>
> diff --git a/drivers/cxl/core/pmu.c b/drivers/cxl/core/pmu.c
> index 5d8e06b0ba6e..2d12887c9915 100644
> --- a/drivers/cxl/core/pmu.c
> +++ b/drivers/cxl/core/pmu.c
> @@ -51,6 +51,9 @@ int devm_cxl_pmu_add(struct device *parent, struct cxl_pmu_regs *regs,
> case CXL_PMU_MEMDEV:
> rc = dev_set_name(dev, "pmu_mem%d.%d", assoc_id, index);
> break;
> + default:
> + rc = -EINVAL;
> + break;
You might consider to use the -ENOENT error code and/or to
preinitialize rc with the error code. The default case can be dropped
then.
However, all those variants look ok:
Reviewed-by: Robert Richter <rrichter@xxxxxxx>
> }
> if (rc)
> goto err;
> --
> 2.34.1
>