Re: [PATCH v2] clk: zynq: Prevent null pointer dereference caused by kmalloc failure

From: Stephen Boyd
Date: Thu Feb 29 2024 - 20:22:53 EST


Quoting duoming@xxxxxxxxxx (2024-02-29 06:16:28)
> On Thu, 29 Feb 2024 13:45:54 +0100 Michal Simek wrote:
>
> The length of "mio_clk_00x" is 11 bytes, and the kernel will alloc 16 bytes to it.
> I use a local variable whose size is 16 bytes to replace it. The detail is shown
> below:
>
> diff --git a/drivers/clk/zynq/clkc.c b/drivers/clk/zynq/clkc.c
> index 7bdeaff2bfd..81d530e3357 100644
> --- a/drivers/clk/zynq/clkc.c
> +++ b/drivers/clk/zynq/clkc.c
> @@ -215,7 +215,7 @@ static void __init zynq_clk_setup(struct device_node *np)
> int i;
> u32 tmp;
> int ret;
> - char *clk_name;
> + char clk_name[16];
> unsigned int fclk_enable = 0;
> const char *clk_output_name[clk_max];
> const char *cpu_parents[4];
> @@ -427,7 +427,6 @@ static void __init zynq_clk_setup(struct device_node *np)
> SLCR_GEM1_CLK_CTRL, 0, 0, &gem1clk_lock);
>
> tmp = strlen("mio_clk_00x");

Is tmp used now?

> - clk_name = kmalloc(tmp, GFP_KERNEL);
> for (i = 0; i < NUM_MIO_PINS; i++) {
> int idx;
>
> @@ -439,7 +438,6 @@ static void __init zynq_clk_setup(struct device_node *np)
> else
> can_mio_mux_parents[i] = dummy_nm;
> }
> - kfree(clk_name);
> clk_register_mux(NULL, "can_mux", periph_parents, 4,
> CLK_SET_RATE_NO_REPARENT, SLCR_CAN_CLK_CTRL, 4, 2, 0,
> &canclk_lock);
>
> Do you think the above is better?
>

It's getting there.