Message ID | 20221129105820.1539875-1-cuigaosheng1@huawei.com |
---|---|
State | New |
Headers | show |
Series | pinctrl: thunderbay: fix possible memory leak in thunderbay_add_functions() | expand |
On 29.11.2022 11:58, Gaosheng Cui wrote: > The memory of funcs is allocated by thunderbay_build_functions(), > thunderbay_add_functions() will free funcs when everything is ok, > but it will not be freed when thunderbay_add_functions() fails, > then there will be a memory leak, so add kfree() when > thunderbay_add_functions() fails to fix it. > > Fixes: 25d2e41cf59b ("pinctrl: thunderbay: rework loops looking for groups names") > Signed-off-by: Gaosheng Cui <cuigaosheng1@huawei.com> > --- > drivers/pinctrl/pinctrl-thunderbay.c | 4 +++- > 1 file changed, 3 insertions(+), 1 deletion(-) > > diff --git a/drivers/pinctrl/pinctrl-thunderbay.c b/drivers/pinctrl/pinctrl-thunderbay.c > index 9328b17485cf..10a7380af8e6 100644 > --- a/drivers/pinctrl/pinctrl-thunderbay.c > +++ b/drivers/pinctrl/pinctrl-thunderbay.c > @@ -784,8 +784,10 @@ static int thunderbay_add_functions(struct thunderbay_pinctrl *tpc, struct funct > > group_names = devm_kcalloc(tpc->dev, func->num_group_names, > sizeof(*group_names), GFP_KERNEL); > - if (!group_names) > + if (!group_names) { > + kfree(funcs); > return -ENOMEM; > + } > > for (j = 0; j < tpc->soc->npins; j++) { > const struct pinctrl_pin_desc *pin_info = &thunderbay_pins[j]; I'm wondering if moving that kfree(funcs) to the caller (thunderbay_build_functions()) would make code a bit cleaner. If you take a look at thunderbay_build_functions() it takes care of allocating and reallocating so maybe kfree() should belong there too?
diff --git a/drivers/pinctrl/pinctrl-thunderbay.c b/drivers/pinctrl/pinctrl-thunderbay.c index 9328b17485cf..10a7380af8e6 100644 --- a/drivers/pinctrl/pinctrl-thunderbay.c +++ b/drivers/pinctrl/pinctrl-thunderbay.c @@ -784,8 +784,10 @@ static int thunderbay_add_functions(struct thunderbay_pinctrl *tpc, struct funct group_names = devm_kcalloc(tpc->dev, func->num_group_names, sizeof(*group_names), GFP_KERNEL); - if (!group_names) + if (!group_names) { + kfree(funcs); return -ENOMEM; + } for (j = 0; j < tpc->soc->npins; j++) { const struct pinctrl_pin_desc *pin_info = &thunderbay_pins[j];
The memory of funcs is allocated by thunderbay_build_functions(), thunderbay_add_functions() will free funcs when everything is ok, but it will not be freed when thunderbay_add_functions() fails, then there will be a memory leak, so add kfree() when thunderbay_add_functions() fails to fix it. Fixes: 25d2e41cf59b ("pinctrl: thunderbay: rework loops looking for groups names") Signed-off-by: Gaosheng Cui <cuigaosheng1@huawei.com> --- drivers/pinctrl/pinctrl-thunderbay.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)