From patchwork Fri Dec 2 01:21:26 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: "yiyang \(D\)" X-Patchwork-Id: 630587 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 482E8C4332F for ; Fri, 2 Dec 2022 01:24:51 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230370AbiLBBYt (ORCPT ); Thu, 1 Dec 2022 20:24:49 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44986 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229744AbiLBBYs (ORCPT ); Thu, 1 Dec 2022 20:24:48 -0500 Received: from szxga03-in.huawei.com (szxga03-in.huawei.com [45.249.212.189]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3405695830; Thu, 1 Dec 2022 17:24:47 -0800 (PST) Received: from kwepemm600014.china.huawei.com (unknown [172.30.72.57]) by szxga03-in.huawei.com (SkyGuard) with ESMTP id 4NNZsD0JNxzJp4h; Fri, 2 Dec 2022 09:21:20 +0800 (CST) Received: from ubuntu1804.huawei.com (10.67.175.28) by kwepemm600014.china.huawei.com (7.193.23.54) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.31; Fri, 2 Dec 2022 09:24:44 +0800 From: Yi Yang To: , , , CC: , Subject: [PATCH v2] usb: fotg210-udc: fix potential memory leak in fotg210_udc_probe() Date: Fri, 2 Dec 2022 09:21:26 +0800 Message-ID: <20221202012126.246953-1-yiyang13@huawei.com> X-Mailer: git-send-email 2.17.1 MIME-Version: 1.0 X-Originating-IP: [10.67.175.28] X-ClientProxiedBy: dggems706-chm.china.huawei.com (10.3.19.183) To kwepemm600014.china.huawei.com (7.193.23.54) X-CFilter-Loop: Reflected Precedence: bulk List-ID: X-Mailing-List: linux-usb@vger.kernel.org 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 Reviewed-by: Andrzej Pietrasiewicz Reviewed-by: Linus Walleij --- v2: -Use "return -ENOMEM" instead of "goto err" drivers/usb/fotg210/fotg210-udc.c | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) 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; }