diff mbox series

[v2] usb: fotg210-udc: fix potential memory leak in fotg210_udc_probe()

Message ID 20221202012126.246953-1-yiyang13@huawei.com
State New
Headers show
Series [v2] usb: fotg210-udc: fix potential memory leak in fotg210_udc_probe() | expand

Commit Message

yiyang (D) Dec. 2, 2022, 1:21 a.m. UTC
In fotg210_udc_probe(), if devm_clk_get() or clk_prepare_enable()
fails, 'fotg210' will not be freed, which will lead to a memory leak.
Fix it by moving kfree() to a proper location.

In addition,we can use "return -ENOMEM" instead of "goto err"
to simplify the code.

Fixes: 718a38d092ec ("fotg210-udc: Handle PCLK")
Signed-off-by: Yi Yang <yiyang13@huawei.com>
Reviewed-by: Andrzej Pietrasiewicz <andrzej.p@collabora.com>
---
v2:
-Use "return -ENOMEM" instead of "goto err"
 drivers/usb/fotg210/fotg210-udc.c | 9 +++------
 1 file changed, 3 insertions(+), 6 deletions(-)

Comments

Linus Walleij Dec. 3, 2022, 9:41 a.m. UTC | #1
On Fri, Dec 2, 2022 at 2:25 AM Yi Yang <yiyang13@huawei.com> wrote:

> In fotg210_udc_probe(), if devm_clk_get() or clk_prepare_enable()
> fails, 'fotg210' will not be freed, which will lead to a memory leak.
> Fix it by moving kfree() to a proper location.
>
> In addition,we can use "return -ENOMEM" instead of "goto err"
> to simplify the code.
>
> Fixes: 718a38d092ec ("fotg210-udc: Handle PCLK")
> Signed-off-by: Yi Yang <yiyang13@huawei.com>
> Reviewed-by: Andrzej Pietrasiewicz <andrzej.p@collabora.com>

Reviewed-by: Linus Walleij <linus.walleij@linaro.org>

I have some cleanup patches switching around to devm* handling
cooking for v6.3, but let's do this for now.

Yours,
Linus Walleij
diff mbox series

Patch

diff --git a/drivers/usb/fotg210/fotg210-udc.c b/drivers/usb/fotg210/fotg210-udc.c
index b9ea6c6d931c..66e1b7ee3346 100644
--- a/drivers/usb/fotg210/fotg210-udc.c
+++ b/drivers/usb/fotg210/fotg210-udc.c
@@ -1163,12 +1163,10 @@  int fotg210_udc_probe(struct platform_device *pdev)
 		return -ENODEV;
 	}
 
-	ret = -ENOMEM;
-
 	/* initialize udc */
 	fotg210 = kzalloc(sizeof(struct fotg210_udc), GFP_KERNEL);
 	if (fotg210 == NULL)
-		goto err;
+		return -ENOMEM;
 
 	fotg210->dev = dev;
 
@@ -1178,7 +1176,7 @@  int fotg210_udc_probe(struct platform_device *pdev)
 		ret = clk_prepare_enable(fotg210->pclk);
 		if (ret) {
 			dev_err(dev, "failed to enable PCLK\n");
-			return ret;
+			goto err;
 		}
 	} else if (PTR_ERR(fotg210->pclk) == -EPROBE_DEFER) {
 		/*
@@ -1302,8 +1300,7 @@  int fotg210_udc_probe(struct platform_device *pdev)
 	if (!IS_ERR(fotg210->pclk))
 		clk_disable_unprepare(fotg210->pclk);
 
-	kfree(fotg210);
-
 err:
+	kfree(fotg210);
 	return ret;
 }