Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

pinctrl: sunxi: Fix memory leak on krealloc failure

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>

authored by

Yuan Chen and committed by
Linus Walleij
e3507c56 b4102e35

+8 -3
+8 -3
drivers/pinctrl/sunxi/pinctrl-sunxi.c
··· 408 408 const char *function, *pin_prop; 409 409 const char *group; 410 410 int ret, npins, nmaps, configlen = 0, i = 0; 411 + struct pinctrl_map *new_map; 411 412 412 413 *map = NULL; 413 414 *num_maps = 0; ··· 483 482 * We know have the number of maps we need, we can resize our 484 483 * map array 485 484 */ 486 - *map = krealloc(*map, i * sizeof(struct pinctrl_map), GFP_KERNEL); 487 - if (!*map) 488 - return -ENOMEM; 485 + new_map = krealloc(*map, i * sizeof(struct pinctrl_map), GFP_KERNEL); 486 + if (!new_map) { 487 + ret = -ENOMEM; 488 + goto err_free_map; 489 + } 490 + 491 + *map = new_map; 489 492 490 493 return 0; 491 494