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

pinctrl: berlin: fix memory leak in berlin_pinctrl_build_state()

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>

authored by

Yuan Chen and committed by
Linus Walleij
8f6f3035 e3507c56

+6 -2
+6 -2
drivers/pinctrl/berlin/berlin.c
··· 204 204 const struct berlin_desc_group *desc_group; 205 205 const struct berlin_desc_function *desc_function; 206 206 int i, max_functions = 0; 207 + struct pinfunction *new_functions; 207 208 208 209 pctrl->nfunctions = 0; 209 210 ··· 230 229 } 231 230 } 232 231 233 - pctrl->functions = krealloc(pctrl->functions, 232 + new_functions = krealloc(pctrl->functions, 234 233 pctrl->nfunctions * sizeof(*pctrl->functions), 235 234 GFP_KERNEL); 236 - if (!pctrl->functions) 235 + if (!new_functions) { 236 + kfree(pctrl->functions); 237 237 return -ENOMEM; 238 + } 238 239 240 + pctrl->functions = new_functions; 239 241 /* map functions to theirs groups */ 240 242 for (i = 0; i < pctrl->desc->ngroups; i++) { 241 243 desc_group = pctrl->desc->groups + i;