[PATCH v3] media: rj54n1cb0c: Fix memleak in rj54n1_probe()

Zhang Shurong posted 1 patch 3 months, 2 weeks ago
There is a newer version of this series
drivers/media/i2c/rj54n1cb0c.c | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
[PATCH v3] media: rj54n1cb0c: Fix memleak in rj54n1_probe()
Posted by Zhang Shurong 3 months, 2 weeks ago
rj54n1_probe() won't clean all the allocated resources in fail
path, which may causes the memleaks. Add v4l2_ctrl_handler_free() to
prevent memleak.

Fixes: f187352dcd45 ("media: i2c: Copy rj54n1cb0c soc_camera sensor driver")

Signed-off-by: Zhang Shurong <zhang_shurong@foxmail.com>
---
Changes in v2:
- Fixed compile error
Changes in v3:
- Fixed coding style warnings identified by scripts/checkpatch.pl
---
 drivers/media/i2c/rj54n1cb0c.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/drivers/media/i2c/rj54n1cb0c.c b/drivers/media/i2c/rj54n1cb0c.c
index b7ca39f63dba..a2fe906408c5 100644
--- a/drivers/media/i2c/rj54n1cb0c.c
+++ b/drivers/media/i2c/rj54n1cb0c.c
@@ -1330,8 +1330,10 @@ static int rj54n1_probe(struct i2c_client *client)
 	v4l2_ctrl_new_std(&rj54n1->hdl, &rj54n1_ctrl_ops,
 			V4L2_CID_AUTO_WHITE_BALANCE, 0, 1, 1, 1);
 	rj54n1->subdev.ctrl_handler = &rj54n1->hdl;
-	if (rj54n1->hdl.error)
+	if (rj54n1->hdl.error) {
+		v4l2_ctrl_handler_free(&rj54n1->hdl);
 		return rj54n1->hdl.error;
+	}
 
 	rj54n1->clk_div		= clk_div;
 	rj54n1->rect.left	= RJ54N1_COLUMN_SKIP;
-- 
2.39.5
Re: [PATCH v3] media: rj54n1cb0c: Fix memleak in rj54n1_probe()
Posted by Jacopo Mondi 3 months, 2 weeks ago
Hi Zhang

On Mon, Jun 23, 2025 at 10:26:04AM +0800, Zhang Shurong wrote:
> rj54n1_probe() won't clean all the allocated resources in fail
> path, which may causes the memleaks. Add v4l2_ctrl_handler_free() to
> prevent memleak.
>
> Fixes: f187352dcd45 ("media: i2c: Copy rj54n1cb0c soc_camera sensor driver")

No empty line between this two I think

>
> Signed-off-by: Zhang Shurong <zhang_shurong@foxmail.com>
> ---
> Changes in v2:
> - Fixed compile error
> Changes in v3:
> - Fixed coding style warnings identified by scripts/checkpatch.pl
> ---
>  drivers/media/i2c/rj54n1cb0c.c | 4 +++-
>  1 file changed, 3 insertions(+), 1 deletion(-)
>
> diff --git a/drivers/media/i2c/rj54n1cb0c.c b/drivers/media/i2c/rj54n1cb0c.c
> index b7ca39f63dba..a2fe906408c5 100644
> --- a/drivers/media/i2c/rj54n1cb0c.c
> +++ b/drivers/media/i2c/rj54n1cb0c.c
> @@ -1330,8 +1330,10 @@ static int rj54n1_probe(struct i2c_client *client)
>  	v4l2_ctrl_new_std(&rj54n1->hdl, &rj54n1_ctrl_ops,
>  			V4L2_CID_AUTO_WHITE_BALANCE, 0, 1, 1, 1);
>  	rj54n1->subdev.ctrl_handler = &rj54n1->hdl;

Could you move this after the  below check ?

> -	if (rj54n1->hdl.error)
> +	if (rj54n1->hdl.error) {
> +		v4l2_ctrl_handler_free(&rj54n1->hdl);
>  		return rj54n1->hdl.error;

There already is a label "err_free_ctrl" you could jump to with

                ret = rj54n1->hdl.error;
                goto err_free_ctrl;

Doesn't make much difference, so I guess it's up to what you like the
best here.

Thanks
  j

> +	}
>
>  	rj54n1->clk_div		= clk_div;
>  	rj54n1->rect.left	= RJ54N1_COLUMN_SKIP;
> --
> 2.39.5
>