Skip to contentSkip to conversation

media: i2c: hi846: Fix memory leak in hi846_parse_dt()

PATCH In review Rafael Mendonca 3 participants 3 messages Sep 19, 2022 – Sep 20, 2022

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 media: i2c: hi846: Fix memory leak in hi846_parse_dt()

If any of the checks related to the supported link frequencies fail, then the V4L2 fwnode resources don't get released before returning, which leads to a memleak. Fix this by properly freeing the V4L2 fwnode data in a designated label.

Fixes e8c0882685f9 ("media: i2c: add driver for the SK Hynix Hi-846 8M pixel camera")
---
 drivers/media/i2c/hi846.c | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)
drivers/media/i2c/hi846.c +10 -4
index ad35c3ff3611..254031503c72 100644
@@ -2008,22 +2008,24 @@ static int hi846_parse_dt(struct hi846 *hi846, struct device *dev)
 	    bus_cfg.bus.mipi_csi2.num_data_lanes != 4) {
 		dev_err(dev, "number of CSI2 data lanes %d is not supported",
 			bus_cfg.bus.mipi_csi2.num_data_lanes);
-		v4l2_fwnode_endpoint_free(&bus_cfg);
-		return -EINVAL;
+		ret = -EINVAL;
+		goto check_hwcfg_error;
 	}
 
 	hi846->nr_lanes = bus_cfg.bus.mipi_csi2.num_data_lanes;
 
 	if (!bus_cfg.nr_of_link_frequencies) {
 		dev_err(dev, "link-frequency property not found in DT\n");
-		return -EINVAL;
+		ret = -EINVAL;
+		goto check_hwcfg_error;
 	}
 
 	/* Check that link frequences for all the modes are in device tree */
 	fq = hi846_check_link_freqs(hi846, &bus_cfg);
 	if (fq) {
 		dev_err(dev, "Link frequency of %lld is not supported\n", fq);
-		return -EINVAL;
+		ret = -EINVAL;
+		goto check_hwcfg_error;
 	}
 
 	v4l2_fwnode_endpoint_free(&bus_cfg);
@@ -2044,6 +2046,10 @@ static int hi846_parse_dt(struct hi846 *hi846, struct device *dev)
 	}
 
 	return 0;
+
+check_hwcfg_error:
+	v4l2_fwnode_endpoint_free(&bus_cfg);
+	return ret;
 }
 
 static int hi846_probe(struct i2c_client *client)
Signature · 1 line
2.34.1
Reviewed-by on v1 1/1
#1 lore raw

Hi Rafael,

Quoted 58 lines · Rafael Mendonca

If any of the checks related to the supported link frequencies fail, then the V4L2 fwnode resources don't get released before returning, which leads to a memleak. Fix this by properly freeing the V4L2 fwnode data in a designated label.

Fixes e8c0882685f9 ("media: i2c: add driver for the SK Hynix Hi-846 8M pixel camera")
---
 drivers/media/i2c/hi846.c | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)
drivers/media/i2c/hi846.c +10 -4
index ad35c3ff3611..254031503c72 100644
@@ -2008,22 +2008,24 @@ static int hi846_parse_dt(struct hi846 *hi846, struct device *dev)
 	    bus_cfg.bus.mipi_csi2.num_data_lanes != 4) {
 		dev_err(dev, "number of CSI2 data lanes %d is not supported",
 			bus_cfg.bus.mipi_csi2.num_data_lanes);
-		v4l2_fwnode_endpoint_free(&bus_cfg);
-		return -EINVAL;
+		ret = -EINVAL;
+		goto check_hwcfg_error;
 	}
 
 	hi846->nr_lanes = bus_cfg.bus.mipi_csi2.num_data_lanes;
 
 	if (!bus_cfg.nr_of_link_frequencies) {
 		dev_err(dev, "link-frequency property not found in DT\n");
-		return -EINVAL;
+		ret = -EINVAL;
+		goto check_hwcfg_error;
 	}
 
 	/* Check that link frequences for all the modes are in device tree */
 	fq = hi846_check_link_freqs(hi846, &bus_cfg);
 	if (fq) {
 		dev_err(dev, "Link frequency of %lld is not supported\n", fq);
-		return -EINVAL;
+		ret = -EINVAL;
+		goto check_hwcfg_error;
 	}
 
 	v4l2_fwnode_endpoint_free(&bus_cfg);
@@ -2044,6 +2046,10 @@ static int hi846_parse_dt(struct hi846 *hi846, struct device *dev)
 	}
 
 	return 0;
+
+check_hwcfg_error:
+	v4l2_fwnode_endpoint_free(&bus_cfg);
+	return ret;
 }
 
 static int hi846_probe(struct i2c_client *client)
Signature · 1 line
2.34.1

Looks good to me.

Regards,
Tommaso

Signature · 10 lines
Tommaso Merciai
Embedded Linux Engineer
[email protected]
__________________________________

Amarula Solutions SRL
Via Le Canevare 30, 31100 Treviso, Veneto, IT
T. +39 042 243 5310
[email protected]
www.amarulasolutions.com
maintainer Reviewed-by on v1 1/1
#2 lore raw
In reply to Rafael Mendonca

Am Sonntag, dem 18.09.2022 um 23:12 -0300 schrieb Rafael Mendonca:

Quoted 61 lines

If any of the checks related to the supported link frequencies fail, then
the V4L2 fwnode resources don't get released before returning, which leads
to a memleak. Fix this by properly freeing the V4L2 fwnode data in a designated label.

Fixes e8c0882685f9 ("media: i2c: add driver for the SK Hynix Hi-846

8M pixel camera")

---
 drivers/media/i2c/hi846.c | 14 ++++++++++----

 1 file changed, 10 insertions(+), 4 deletions(-)
drivers/media/i2c/hi846.c +0 -0
index ad35c3ff3611..254031503c72 100644
@@ -2008,22 +2008,24 @@ static int hi846_parse_dt(struct hi846
 *hi846, struct device *dev)
             bus_cfg.bus.mipi_csi2.num_data_lanes != 4) {

                dev_err(dev, "number of CSI2 data lanes %d is not supported",
                        bus_cfg.bus.mipi_csi2.num_data_lanes); -               v4l2_fwnode_endpoint_free(&bus_cfg);
-               return -EINVAL;
+               ret = -EINVAL;
+               goto check_hwcfg_error;
        }

        hi846->nr_lanes = bus_cfg.bus.mipi_csi2.num_data_lanes;
        if (!bus_cfg.nr_of_link_frequencies) { dev_err(dev, "link-frequency property not found in DT\n");
-               return -EINVAL;
+               ret = -EINVAL;
+               goto check_hwcfg_error;
        }

        /* Check that link frequences for all the modes are in device tree */
        fq = hi846_check_link_freqs(hi846, &bus_cfg);
        if (fq) {
                dev_err(dev, "Link frequency of %lld is not supported\n", fq);
-               return -EINVAL;
+               ret = -EINVAL;
+               goto check_hwcfg_error;
        }

        v4l2_fwnode_endpoint_free(&bus_cfg);

drivers/media/i2c/hi846.c +0 -0
@@ -2044,6 +2046,10 @@ static int hi846_parse_dt(struct hi846 *hi846,
 struct device *dev)
         }


        return 0;

drivers/media/i2c/hi846.c +4 -0
+
+check_hwcfg_error:
+       v4l2_fwnode_endpoint_free(&bus_cfg);
+       return ret;
Quoted 3 lines

 }

 static int hi846_probe(struct i2c_client *client)

thank you very much for the patch. lgtm,

martin