]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
pinctrl: sunxi: Fix memory leak on krealloc failure
authorYuan Chen <chenyuan@kylinos.cn>
Fri, 20 Jun 2025 01:27:08 +0000 (09:27 +0800)
committerLinus Walleij <linus.walleij@linaro.org>
Tue, 24 Jun 2025 19:17:01 +0000 (21:17 +0200)
In sunxi_pctrl_dt_node_to_map(), when krealloc() fails to resize
the pinctrl_map array, the function returns -ENOMEM directly
without freeing the previously allocated *map buffer. This results
in a memory leak of the original kmalloc_array allocation.

Fixes: e11dee2e98f8 ("pinctrl: sunxi: Deal with configless pins")
Signed-off-by: Yuan Chen <chenyuan@kylinos.cn>
Link: https://lore.kernel.org/20250620012708.16709-1-chenyuan_fl@163.com
Signed-off-by: Linus Walleij <linus.walleij@linaro.org>
drivers/pinctrl/sunxi/pinctrl-sunxi.c

index a5ce84621e5a86efd20a3b97de71eabcd42fec6a..0db8429a013fab63cea7f2e0c0fbd54089f4327e 100644 (file)
@@ -408,6 +408,7 @@ static int sunxi_pctrl_dt_node_to_map(struct pinctrl_dev *pctldev,
        const char *function, *pin_prop;
        const char *group;
        int ret, npins, nmaps, configlen = 0, i = 0;
+       struct pinctrl_map *new_map;
 
        *map = NULL;
        *num_maps = 0;
@@ -482,9 +483,13 @@ static int sunxi_pctrl_dt_node_to_map(struct pinctrl_dev *pctldev,
         * We know have the number of maps we need, we can resize our
         * map array
         */
-       *map = krealloc(*map, i * sizeof(struct pinctrl_map), GFP_KERNEL);
-       if (!*map)
-               return -ENOMEM;
+       new_map = krealloc(*map, i * sizeof(struct pinctrl_map), GFP_KERNEL);
+       if (!new_map) {
+               ret = -ENOMEM;
+               goto err_free_map;
+       }
+
+       *map = new_map;
 
        return 0;