[PATCH 4/4] staging: rtl8723bs: check return value of rtw_wdev_alloc()

Samasth Norway Ananda posted 4 patches 1 month, 3 weeks ago
[PATCH 4/4] staging: rtl8723bs: check return value of rtw_wdev_alloc()
Posted by Samasth Norway Ananda 1 month, 3 weeks ago
Add missing error check for rtw_wdev_alloc() in rtw_sdio_if1_init().

rtw_wdev_alloc() can fail with -ENOMEM when wiphy_new() or rtw_zmalloc()
fails, or with other negative error codes when wiphy_register() fails.
Without checking the return value, initialization continues even when
wireless device allocation fails, potentially leaving the adapter in an
inconsistent state.

Jump to the error cleanup path when rtw_wdev_alloc() fails to ensure
proper resource cleanup and prevent use of an incompletely initialized
adapter.

Signed-off-by: Samasth Norway Ananda <samasth.norway.ananda@oracle.com>
---
 drivers/staging/rtl8723bs/os_dep/sdio_intf.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/drivers/staging/rtl8723bs/os_dep/sdio_intf.c b/drivers/staging/rtl8723bs/os_dep/sdio_intf.c
index 1d0239eef114..432bc6aa1d90 100644
--- a/drivers/staging/rtl8723bs/os_dep/sdio_intf.c
+++ b/drivers/staging/rtl8723bs/os_dep/sdio_intf.c
@@ -296,7 +296,8 @@ static struct adapter *rtw_sdio_if1_init(struct dvobj_priv *dvobj, const struct
 	if (rtw_init_drv_sw(padapter) == _FAIL)
 		goto free_hal_data;
 
-	rtw_wdev_alloc(padapter, dvobj_to_dev(dvobj));
+	if (rtw_wdev_alloc(padapter, dvobj_to_dev(dvobj)))
+		goto free_hal_data;
 
 	/* 3 8. get WLan MAC address */
 	/*  set mac addr */
-- 
2.50.1
Re: [PATCH 4/4] staging: rtl8723bs: check return value of rtw_wdev_alloc()
Posted by Dan Carpenter 1 month, 3 weeks ago
On Wed, Dec 17, 2025 at 05:14:14PM -0800, Samasth Norway Ananda wrote:
> Add missing error check for rtw_wdev_alloc() in rtw_sdio_if1_init().
> 
> rtw_wdev_alloc() can fail with -ENOMEM when wiphy_new() or rtw_zmalloc()
> fails, or with other negative error codes when wiphy_register() fails.
> Without checking the return value, initialization continues even when
> wireless device allocation fails, potentially leaving the adapter in an
> inconsistent state.
> 
> Jump to the error cleanup path when rtw_wdev_alloc() fails to ensure
> proper resource cleanup and prevent use of an incompletely initialized
> adapter.
> 
> Signed-off-by: Samasth Norway Ananda <samasth.norway.ananda@oracle.com>
> ---
>  drivers/staging/rtl8723bs/os_dep/sdio_intf.c | 3 ++-
>  1 file changed, 2 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/staging/rtl8723bs/os_dep/sdio_intf.c b/drivers/staging/rtl8723bs/os_dep/sdio_intf.c
> index 1d0239eef114..432bc6aa1d90 100644
> --- a/drivers/staging/rtl8723bs/os_dep/sdio_intf.c
> +++ b/drivers/staging/rtl8723bs/os_dep/sdio_intf.c
> @@ -296,7 +296,8 @@ static struct adapter *rtw_sdio_if1_init(struct dvobj_priv *dvobj, const struct
>  	if (rtw_init_drv_sw(padapter) == _FAIL)

This call to rtw_init_drv_sw() does a number of allocations.

>  		goto free_hal_data;
>  
> -	rtw_wdev_alloc(padapter, dvobj_to_dev(dvobj));
> +	if (rtw_wdev_alloc(padapter, dvobj_to_dev(dvobj)))
> +		goto free_hal_data;

So this goto should free them as well.  I have a blog about how to
write cleanup code in a systematic way.

https://staticthinking.wordpress.com/2022/04/28/free-the-last-thing-style/

regards,
dan carpenter
Re: [External] : Re: [PATCH 4/4] staging: rtl8723bs: check return value of rtw_wdev_alloc()
Posted by samasth.norway.ananda@oracle.com 1 month, 3 weeks ago

On 12/17/25 11:12 PM, Dan Carpenter wrote:
> On Wed, Dec 17, 2025 at 05:14:14PM -0800, Samasth Norway Ananda wrote:
>> Add missing error check for rtw_wdev_alloc() in rtw_sdio_if1_init().
>>
>> rtw_wdev_alloc() can fail with -ENOMEM when wiphy_new() or rtw_zmalloc()
>> fails, or with other negative error codes when wiphy_register() fails.
>> Without checking the return value, initialization continues even when
>> wireless device allocation fails, potentially leaving the adapter in an
>> inconsistent state.
>>
>> Jump to the error cleanup path when rtw_wdev_alloc() fails to ensure
>> proper resource cleanup and prevent use of an incompletely initialized
>> adapter.
>>
>> Signed-off-by: Samasth Norway Ananda <samasth.norway.ananda@oracle.com>
>> ---
>>   drivers/staging/rtl8723bs/os_dep/sdio_intf.c | 3 ++-
>>   1 file changed, 2 insertions(+), 1 deletion(-)
>>
>> diff --git a/drivers/staging/rtl8723bs/os_dep/sdio_intf.c b/drivers/staging/rtl8723bs/os_dep/sdio_intf.c
>> index 1d0239eef114..432bc6aa1d90 100644
>> --- a/drivers/staging/rtl8723bs/os_dep/sdio_intf.c
>> +++ b/drivers/staging/rtl8723bs/os_dep/sdio_intf.c
>> @@ -296,7 +296,8 @@ static struct adapter *rtw_sdio_if1_init(struct dvobj_priv *dvobj, const struct
>>   	if (rtw_init_drv_sw(padapter) == _FAIL)
> 
> This call to rtw_init_drv_sw() does a number of allocations.
> 
>>   		goto free_hal_data;
>>   
>> -	rtw_wdev_alloc(padapter, dvobj_to_dev(dvobj));
>> +	if (rtw_wdev_alloc(padapter, dvobj_to_dev(dvobj)))
>> +		goto free_hal_data;
> 
> So this goto should free them as well.  I have a blog about how to
> write cleanup code in a systematic way.
> 
> https://urldefense.com/v3/__https://staticthinking.wordpress.com/2022/04/28/free-the-last-thing-style/__;!!ACWV5N9M2RV99hQ!KLPxUw-pZDgh7c5bRyLHFbR9D8QxefnIaHlNgp3DJsspN6yXrwFMCjE6xJIAZhqLWNIzorGl1tTzvpkYINf88wiRyerJrpTr$

Oh okay. Thanks Dan i'll go through it.

regards,
Samasth.

> 
> regards,
> dan carpenter
>