[PATCH] staging: rtl8723bs: Use kmemdup() to replace kzalloc() followed by memcpy()

Oarora Etimis posted 1 patch 3 weeks, 1 day ago
drivers/staging/rtl8723bs/os_dep/ioctl_cfg80211.c | 8 +++-----
1 file changed, 3 insertions(+), 5 deletions(-)
[PATCH] staging: rtl8723bs: Use kmemdup() to replace kzalloc() followed by memcpy()
Posted by Oarora Etimis 3 weeks, 1 day ago
The kzalloc() function allocates memory and zero-initializes it.
However, in rtw_cfg80211_set_wpa_ie(), the allocated buffer is
immediately overwritten by memcpy(). This makes the zero-initialization
completely unnecessary, wasting CPU cycles.

Use kmemdup() to simplify the code, eliminate the redundant memcpy(),
and remove the unnecessary zeroing overhead.

Signed-off-by: Oarora Etimis <OaroraEtimis@gmail.com>
---
 drivers/staging/rtl8723bs/os_dep/ioctl_cfg80211.c | 8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)

diff --git a/drivers/staging/rtl8723bs/os_dep/ioctl_cfg80211.c b/drivers/staging/rtl8723bs/os_dep/ioctl_cfg80211.c
index 7cb0c6f22bf3..2f7fa5841578 100644
--- a/drivers/staging/rtl8723bs/os_dep/ioctl_cfg80211.c
+++ b/drivers/staging/rtl8723bs/os_dep/ioctl_cfg80211.c
@@ -1430,16 +1430,14 @@ static int rtw_cfg80211_set_wpa_ie(struct adapter *padapter, u8 *pie, size_t iel
 		goto exit;
 	}
 
-	buf = kzalloc(ielen, GFP_KERNEL);
+	buf = kmemdup(pie, ielen, GFP_KERNEL);
 	if (!buf) {
-		ret =  -ENOMEM;
+		ret = -ENOMEM;
 		goto exit;
 	}
 
-	memcpy(buf, pie, ielen);
-
 	if (ielen < RSN_HEADER_LEN) {
-		ret  = -1;
+		ret = -1;
 		goto exit;
 	}
 
-- 
2.47.3
Re: [PATCH] staging: rtl8723bs: Use kmemdup() to replace kzalloc() followed by memcpy()
Posted by Ethan Tidmore 3 weeks, 1 day ago
On Sun Mar 15, 2026 at 2:47 PM CDT, Oarora Etimis wrote:
> The kzalloc() function allocates memory and zero-initializes it.
> However, in rtw_cfg80211_set_wpa_ie(), the allocated buffer is
> immediately overwritten by memcpy(). This makes the zero-initialization
> completely unnecessary, wasting CPU cycles.
>
> Use kmemdup() to simplify the code, eliminate the redundant memcpy(),
> and remove the unnecessary zeroing overhead.
>
> Signed-off-by: Oarora Etimis <OaroraEtimis@gmail.com>
> ---

This doesn't apply to staging-next.

>  
> -	memcpy(buf, pie, ielen);
> -
>  	if (ielen < RSN_HEADER_LEN) {
> -		ret  = -1;
> +		ret = -1;
>  		goto exit;
>  	}
>  

Also, this is a stray change. Keep one logical change per patch.

Thanks,

ET