[PATCH] media: hi846: Fix memleak in hi846_init_controls()

Wei Chen posted 1 patch 1 year, 1 month ago
There is a newer version of this series
drivers/media/i2c/hi846.c | 1 +
1 file changed, 1 insertion(+)
[PATCH] media: hi846: Fix memleak in hi846_init_controls()
Posted by Wei Chen 1 year, 1 month ago
hi846_init_controls doesn't clean the allocated ctrl_hdlr
in case there is a failure, which causes memleak. Add
v4l2_ctrl_handler_free to free the resource properly.

Signed-off-by: Wei Chen <harperchen1110@gmail.com>
---
 drivers/media/i2c/hi846.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/drivers/media/i2c/hi846.c b/drivers/media/i2c/hi846.c
index 7c61873b7198..c45a6511d2c1 100644
--- a/drivers/media/i2c/hi846.c
+++ b/drivers/media/i2c/hi846.c
@@ -1472,6 +1472,7 @@ static int hi846_init_controls(struct hi846 *hi846)
 	if (ctrl_hdlr->error) {
 		dev_err(&client->dev, "v4l ctrl handler error: %d\n",
 			ctrl_hdlr->error);
+		v4l2_ctrl_handler_free(ctrl_hdlr);
 		return ctrl_hdlr->error;
 	}
 
-- 
2.25.1
Re: [PATCH] media: hi846: Fix memleak in hi846_init_controls()
Posted by Martin Kepplinger 1 year ago
Am Freitag, dem 24.03.2023 um 17:24 +0000 schrieb Wei Chen:
> hi846_init_controls doesn't clean the allocated ctrl_hdlr
> in case there is a failure, which causes memleak. Add
> v4l2_ctrl_handler_free to free the resource properly.
> 
> Signed-off-by: Wei Chen <harperchen1110@gmail.com>
> ---
>  drivers/media/i2c/hi846.c | 1 +
>  1 file changed, 1 insertion(+)
> 
> diff --git a/drivers/media/i2c/hi846.c b/drivers/media/i2c/hi846.c
> index 7c61873b7198..c45a6511d2c1 100644
> --- a/drivers/media/i2c/hi846.c
> +++ b/drivers/media/i2c/hi846.c
> @@ -1472,6 +1472,7 @@ static int hi846_init_controls(struct hi846
> *hi846)
>         if (ctrl_hdlr->error) {
>                 dev_err(&client->dev, "v4l ctrl handler error: %d\n",
>                         ctrl_hdlr->error);
> +               v4l2_ctrl_handler_free(ctrl_hdlr);
>                 return ctrl_hdlr->error;
>         }
>  

hi Wei, thanks for the patch. It looks like I forgot that indeed, but
to me it looks like the subsequent error paths of
v4l2_fwnode_device_parse() and v4l2_ctrl_new_fwnode_properties() would
leak the same thing.

And since we only assign it to the subdev's ctrl_handler in the success
case later, you could simply add an error label:


@@ -1663,21 +1663,26 @@ static int hi846_init_controls(struct hi846
*hi846)
        if (ctrl_hdlr->error) {
                dev_err(&client->dev, "v4l ctrl handler error: %d\n",
                        ctrl_hdlr->error);
-               return ctrl_hdlr->error;
+               ret = ctrl_hdlr->error;
+               goto error;
        }
 
        ret = v4l2_fwnode_device_parse(&client->dev, &props);
        if (ret)
-               return ret;
+               goto error;
 
        ret = v4l2_ctrl_new_fwnode_properties(ctrl_hdlr,
&hi846_ctrl_ops,
                                              &props);
        if (ret)
-               return ret;
+               goto error;
 
        hi846->sd.ctrl_handler = ctrl_hdlr;
 
        return 0;
+
+error:
+       v4l2_ctrl_handler_free(ctrl_hdlr);
+       return ret;
 }
 

what do you think? thanks!

                              martin


Re: [PATCH] media: hi846: Fix memleak in hi846_init_controls()
Posted by Wei Chen 1 year ago
Dear Martin,

Thanks for pointing out my mistake.

I cross-checked other implementations of drivers under media/i2c. I
agree with you. Let me send the second version of my patch.

Thanks,
Wei

On Sun, 26 Mar 2023 at 16:50, Martin Kepplinger <martink@posteo.de> wrote:
>
> Am Freitag, dem 24.03.2023 um 17:24 +0000 schrieb Wei Chen:
> > hi846_init_controls doesn't clean the allocated ctrl_hdlr
> > in case there is a failure, which causes memleak. Add
> > v4l2_ctrl_handler_free to free the resource properly.
> >
> > Signed-off-by: Wei Chen <harperchen1110@gmail.com>
> > ---
> >  drivers/media/i2c/hi846.c | 1 +
> >  1 file changed, 1 insertion(+)
> >
> > diff --git a/drivers/media/i2c/hi846.c b/drivers/media/i2c/hi846.c
> > index 7c61873b7198..c45a6511d2c1 100644
> > --- a/drivers/media/i2c/hi846.c
> > +++ b/drivers/media/i2c/hi846.c
> > @@ -1472,6 +1472,7 @@ static int hi846_init_controls(struct hi846
> > *hi846)
> >         if (ctrl_hdlr->error) {
> >                 dev_err(&client->dev, "v4l ctrl handler error: %d\n",
> >                         ctrl_hdlr->error);
> > +               v4l2_ctrl_handler_free(ctrl_hdlr);
> >                 return ctrl_hdlr->error;
> >         }
> >
>
> hi Wei, thanks for the patch. It looks like I forgot that indeed, but
> to me it looks like the subsequent error paths of
> v4l2_fwnode_device_parse() and v4l2_ctrl_new_fwnode_properties() would
> leak the same thing.
>
> And since we only assign it to the subdev's ctrl_handler in the success
> case later, you could simply add an error label:
>
>
> @@ -1663,21 +1663,26 @@ static int hi846_init_controls(struct hi846
> *hi846)
>         if (ctrl_hdlr->error) {
>                 dev_err(&client->dev, "v4l ctrl handler error: %d\n",
>                         ctrl_hdlr->error);
> -               return ctrl_hdlr->error;
> +               ret = ctrl_hdlr->error;
> +               goto error;
>         }
>
>         ret = v4l2_fwnode_device_parse(&client->dev, &props);
>         if (ret)
> -               return ret;
> +               goto error;
>
>         ret = v4l2_ctrl_new_fwnode_properties(ctrl_hdlr,
> &hi846_ctrl_ops,
>                                               &props);
>         if (ret)
> -               return ret;
> +               goto error;
>
>         hi846->sd.ctrl_handler = ctrl_hdlr;
>
>         return 0;
> +
> +error:
> +       v4l2_ctrl_handler_free(ctrl_hdlr);
> +       return ret;
>  }
>
>
> what do you think? thanks!
>
>                               martin
>
>