]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
pinctrl: berlin: fix memory leak in berlin_pinctrl_build_state()
authorYuan Chen <chenyuan@kylinos.cn>
Fri, 20 Jun 2025 01:53:43 +0000 (09:53 +0800)
committerLinus Walleij <linus.walleij@linaro.org>
Tue, 24 Jun 2025 19:17:11 +0000 (21:17 +0200)
In the original implementation, krealloc() failure handling incorrectly
assigned the original memory pointer to NULL after kfree(), causing a
memory leak when reallocation failed.

Fixes: de845036f997 ("pinctrl: berlin: fix error return code of berlin_pinctrl_build_state()")
Signed-off-by: Yuan Chen <chenyuan@kylinos.cn>
Link: https://lore.kernel.org/20250620015343.21494-1-chenyuan_fl@163.com
Signed-off-by: Linus Walleij <linus.walleij@linaro.org>
drivers/pinctrl/berlin/berlin.c

index e5a35b803ce66d247c5e5ad78e6677570a1add60..8afcfa4e56942e923c0824de768379203559c238 100644 (file)
@@ -204,6 +204,7 @@ static int berlin_pinctrl_build_state(struct platform_device *pdev)
        const struct berlin_desc_group *desc_group;
        const struct berlin_desc_function *desc_function;
        int i, max_functions = 0;
+       struct pinfunction *new_functions;
 
        pctrl->nfunctions = 0;
 
@@ -229,12 +230,15 @@ static int berlin_pinctrl_build_state(struct platform_device *pdev)
                }
        }
 
-       pctrl->functions = krealloc(pctrl->functions,
+       new_functions = krealloc(pctrl->functions,
                                    pctrl->nfunctions * sizeof(*pctrl->functions),
                                    GFP_KERNEL);
-       if (!pctrl->functions)
+       if (!new_functions) {
+               kfree(pctrl->functions);
                return -ENOMEM;
+       }
 
+       pctrl->functions = new_functions;
        /* map functions to theirs groups */
        for (i = 0; i < pctrl->desc->ngroups; i++) {
                desc_group = pctrl->desc->groups + i;