linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: xkernel.wang@foxmail.com
To: Larry.Finger@lwfinger.net, phil@philpotter.co.uk,
	gregkh@linuxfoundation.org
Cc: linux-staging@lists.linux.dev, linux-kernel@vger.kernel.org,
	Xiaoke Wang <xkernel.wang@foxmail.com>
Subject: [PATCH 09/12] staging: r8188eu: fix a potential memory leak in _rtw_init_cmd_priv()
Date: Tue,  3 May 2022 15:01:17 +0800	[thread overview]
Message-ID: <tencent_EA8B79235780C38F022D9C4389E75F0B6405@qq.com> (raw)
In-Reply-To: <tencent_A80380E4306BE7BA73E450F084232B4DFC0A@qq.com>

From: Xiaoke Wang <xkernel.wang@foxmail.com>

In _rtw_init_cmd_priv(), if `pcmdpriv->rsp_allocated_buf` is allocated
in failure, then `pcmdpriv->cmd_allocated_buf` will not be properly
released. Besides, considering there are only two error paths and the
first one can directly return, we do not need to implicitly jump to the
`exit` tag to execute the error handling code.

So this patch added `kfree(pcmdpriv->cmd_allocated_buf);` on the error
path to release the resource and simplified the return logic of
_rtw_init_cmd_priv().

Signed-off-by: Xiaoke Wang <xkernel.wang@foxmail.com>
---
 drivers/staging/r8188eu/core/rtw_cmd.c | 16 ++++++----------
 1 file changed, 6 insertions(+), 10 deletions(-)

diff --git a/drivers/staging/r8188eu/core/rtw_cmd.c b/drivers/staging/r8188eu/core/rtw_cmd.c
index 06523d9..37b68a9 100644
--- a/drivers/staging/r8188eu/core/rtw_cmd.c
+++ b/drivers/staging/r8188eu/core/rtw_cmd.c
@@ -58,8 +58,6 @@ static int _rtw_enqueue_cmd(struct __queue *queue, struct cmd_obj *obj)
 
 u32	rtw_init_cmd_priv(struct cmd_priv *pcmdpriv)
 {
-	u32 res = _SUCCESS;
-
 	init_completion(&pcmdpriv->enqueue_cmd);
 	/* sema_init(&(pcmdpriv->cmd_done_sema), 0); */
 	init_completion(&pcmdpriv->start_cmd_thread);
@@ -74,27 +72,25 @@ u32	rtw_init_cmd_priv(struct cmd_priv *pcmdpriv)
 	pcmdpriv->cmd_allocated_buf = kzalloc(MAX_CMDSZ + CMDBUFF_ALIGN_SZ,
 					      GFP_KERNEL);
 
-	if (!pcmdpriv->cmd_allocated_buf) {
-		res = _FAIL;
-		goto exit;
-	}
+	if (!pcmdpriv->cmd_allocated_buf)
+		return _FAIL;
 
 	pcmdpriv->cmd_buf = pcmdpriv->cmd_allocated_buf  +  CMDBUFF_ALIGN_SZ - ((size_t)(pcmdpriv->cmd_allocated_buf) & (CMDBUFF_ALIGN_SZ - 1));
 
 	pcmdpriv->rsp_allocated_buf = kzalloc(MAX_RSPSZ + 4, GFP_KERNEL);
 
 	if (!pcmdpriv->rsp_allocated_buf) {
-		res = _FAIL;
-		goto exit;
+		kfree(pcmdpriv->cmd_allocated_buf);
+		pcmdpriv->cmd_allocated_buf = NULL;
+		return _FAIL;
 	}
 
 	pcmdpriv->rsp_buf = pcmdpriv->rsp_allocated_buf  +  4 - ((size_t)(pcmdpriv->rsp_allocated_buf) & 3);
 
 	pcmdpriv->cmd_done_cnt = 0;
 	pcmdpriv->rsp_cnt = 0;
-exit:
 
-	return res;
+	return _SUCCESS;
 }
 
 u32 rtw_init_evt_priv(struct evt_priv *pevtpriv)
-- 

  parent reply	other threads:[~2022-05-03  7:01 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-05-03  6:56 [PATCH 00/12] staging: some memory-related patches xkernel.wang
2022-05-03  6:57 ` [PATCH 01/12] staging: rtl8712: fix potential memory leak in r8712_xmit_resource_alloc() xkernel.wang
2022-05-03  6:58 ` [PATCH 02/12] staging: rtl8712: fix potential memory leak in _r8712_init_xmit_priv() xkernel.wang
2022-05-03  6:58 ` [PATCH 03/12] staging: rtl8712: fix potential memory leak in r8712_init_drv_sw() xkernel.wang
2022-05-03  6:59 ` [PATCH 04/12] staging: rtl8712: change the type of _r8712_init_recv_priv() xkernel.wang
2022-05-03  6:59 ` [PATCH 05/12] staging: rtl8712: add two validation check in r8712_init_drv_sw() xkernel.wang
2022-05-03  7:00 ` [PATCH 06/12] staging: rtl8723bs: fix a potential memory leak in rtw_init_cmd_priv() xkernel.wang
2022-05-03  7:00 ` [PATCH 07/12] staging: rtl8723bs: fix potential memory leak in _rtw_init_xmit_priv() xkernel.wang
2022-05-03  7:00 ` [PATCH 08/12] staging: rtl8723bs: fix potential memory leak in rtw_init_drv_sw() xkernel.wang
2022-05-03  7:01 ` xkernel.wang [this message]
2022-05-03  7:01 ` [PATCH 10/12] staging: r8188eu: fix potential memory leak in rtw_os_xmit_resource_alloc() xkernel.wang
2022-05-03  7:02 ` [PATCH 11/12] staging: r8188eu: fix potential memory leak in _rtw_init_xmit_priv() xkernel.wang
2022-05-03  7:02 ` [PATCH 12/12] staging: r8188eu: check the return of kzalloc() xkernel.wang
2022-05-03  7:08   ` Joe Perches
2022-05-03  8:22     ` xkernel.wang
2022-06-06  6:11 ` [PATCH 00/12] staging: some memory-related patches Greg KH
2022-06-06 12:08   ` xkernel.wang

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=tencent_EA8B79235780C38F022D9C4389E75F0B6405@qq.com \
    --to=xkernel.wang@foxmail.com \
    --cc=Larry.Finger@lwfinger.net \
    --cc=gregkh@linuxfoundation.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-staging@lists.linux.dev \
    --cc=phil@philpotter.co.uk \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).