Re: [PATCH] clk: tegra: tegra124-emc: fix krealloc() memory leak
From: Thierry Reding
Date: Thu May 28 2026 - 16:35:32 EST
On Tue, May 26, 2026 at 08:13:13AM +0200, Alexander A. Klimov wrote:
> Don't just overwrite the original pointer passed to krealloc()
> with its return value without checking latter:
>
> MEM = krealloc(MEM, SZ, GFP);
>
> If krealloc() returns NULL, that erases the pointer
> to the still allocated memory, hence leaks this memory.
> Instead, use a temporary variable, check it's not NULL
> and only then assign it to the original pointer:
>
> TMP = krealloc(MEM, SZ, GFP);
> if (!TMP) return;
> MEM = TMP;
>
> Fixes: 888ca40e2843 ("clk: tegra: emc: Support multiple RAM codes")
> Signed-off-by: Alexander A. Klimov <grandmaster@xxxxxxxxxxxx>
> ---
> drivers/clk/tegra/clk-tegra124-emc.c | 9 ++++-----
> 1 file changed, 4 insertions(+), 5 deletions(-)
>
> diff --git a/drivers/clk/tegra/clk-tegra124-emc.c b/drivers/clk/tegra/clk-tegra124-emc.c
> index f3b2c96fdcfc..8053fbbb06c8 100644
> --- a/drivers/clk/tegra/clk-tegra124-emc.c
> +++ b/drivers/clk/tegra/clk-tegra124-emc.c
> @@ -446,14 +446,13 @@ static int load_timings_from_dt(struct tegra_clk_emc *tegra,
> struct emc_timing *timings_ptr;
> int child_count = of_get_child_count(node);
> int i = 0, err;
> - size_t size;
> + size_t size = (tegra->num_timings + child_count) * sizeof(struct emc_timing);
> + void *mem = krealloc(tegra->timings, size, GFP_KERNEL);
>
> - size = (tegra->num_timings + child_count) * sizeof(struct emc_timing);
This looks really wild now. I think it'd be better to follow the
original style:
size_t size;
void *mem;
size = ...;
mem = krealloc(...);
if (!mem)
...
With that:
Acked-by: Thierry Reding <treding@xxxxxxxxxx>
Attachment:
signature.asc
Description: PGP signature