[PATCH v2] staging: gpib: Fix memory leak in ni_usb_init()

Zilin Guan posted 1 patch 1 month, 1 week ago
drivers/gpib/ni_usb/ni_usb_gpib.c | 8 +++++---
1 file changed, 5 insertions(+), 3 deletions(-)
[PATCH v2] staging: gpib: Fix memory leak in ni_usb_init()
Posted by Zilin Guan 1 month, 1 week ago
In ni_usb_init(), if ni_usb_setup_init() fails, the function returns
immediately without freeing the allocated memory for writes, leading
to a memory leak.

Fix this by freeing writes before returning the error code.

Fixes: 4e127de14fa7 ("staging: gpib: Add National Instruments USB GPIB driver")
Suggested-by: Dave Penkler <dpenkler@gmail.com>
Co-developed-by: Jianhao Xu <jianhao.xu@seu.edu.cn>
Signed-off-by: Jianhao Xu <jianhao.xu@seu.edu.cn>
Signed-off-by: Zilin Guan <zilin@seu.edu.cn>
---
Changes in v2:
- Use early return to simplify error handling logic.

 drivers/gpib/ni_usb/ni_usb_gpib.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)

diff --git a/drivers/gpib/ni_usb/ni_usb_gpib.c b/drivers/gpib/ni_usb/ni_usb_gpib.c
index 1f8412de9fa3..2352c6817440 100644
--- a/drivers/gpib/ni_usb/ni_usb_gpib.c
+++ b/drivers/gpib/ni_usb/ni_usb_gpib.c
@@ -1799,10 +1799,12 @@ static int ni_usb_init(struct gpib_board *board)
 		return -ENOMEM;
 
 	writes_len = ni_usb_setup_init(board, writes);
-	if (writes_len)
-		retval = ni_usb_write_registers(ni_priv, writes, writes_len, &ibsta);
-	else
+	if (!writes_len) {
+		kfree(writes);
 		return -EFAULT;
+	}
+
+	retval = ni_usb_write_registers(ni_priv, writes, writes_len, &ibsta);
 	kfree(writes);
 	if (retval) {
 		dev_err(&usb_dev->dev, "register write failed, retval=%i\n", retval);
-- 
2.34.1
Re: [PATCH v2] staging: gpib: Fix memory leak in ni_usb_init()
Posted by Greg KH 1 month, 1 week ago
On Mon, Dec 29, 2025 at 03:22:03PM +0000, Zilin Guan wrote:
> In ni_usb_init(), if ni_usb_setup_init() fails, the function returns
> immediately without freeing the allocated memory for writes, leading
> to a memory leak.
> 
> Fix this by freeing writes before returning the error code.
> 
> Fixes: 4e127de14fa7 ("staging: gpib: Add National Instruments USB GPIB driver")
> Suggested-by: Dave Penkler <dpenkler@gmail.com>
> Co-developed-by: Jianhao Xu <jianhao.xu@seu.edu.cn>
> Signed-off-by: Jianhao Xu <jianhao.xu@seu.edu.cn>
> Signed-off-by: Zilin Guan <zilin@seu.edu.cn>
> ---
> Changes in v2:
> - Use early return to simplify error handling logic.
> 
>  drivers/gpib/ni_usb/ni_usb_gpib.c | 8 +++++---

As the code is no longer in staging, that prefix does not need to be
ther.

>  1 file changed, 5 insertions(+), 3 deletions(-)
> 
> diff --git a/drivers/gpib/ni_usb/ni_usb_gpib.c b/drivers/gpib/ni_usb/ni_usb_gpib.c
> index 1f8412de9fa3..2352c6817440 100644
> --- a/drivers/gpib/ni_usb/ni_usb_gpib.c
> +++ b/drivers/gpib/ni_usb/ni_usb_gpib.c
> @@ -1799,10 +1799,12 @@ static int ni_usb_init(struct gpib_board *board)
>  		return -ENOMEM;
>  
>  	writes_len = ni_usb_setup_init(board, writes);
> -	if (writes_len)
> -		retval = ni_usb_write_registers(ni_priv, writes, writes_len, &ibsta);
> -	else
> +	if (!writes_len) {
> +		kfree(writes);
>  		return -EFAULT;

This is not the correct error value, it should only happen if copy
to/from user fails.  I know it's not the issue here, just noticed this.

And why doesn't ni_usb_setup_init() return an error value that should be
propagated upward?

thanks,

greg k-h
Re: [PATCH v2] staging: gpib: Fix memory leak in ni_usb_init()
Posted by Zilin Guan 1 month, 1 week ago
On Mon, Dec 29, 2025 at 04:28:21PM+0100, Greg KH wrote:
> As the code is no longer in staging, that prefix does not need to be
> ther.

Thanks for pointing that out. I will drop the prefix in v3.

> >  1 file changed, 5 insertions(+), 3 deletions(-)
> > 
> > diff --git a/drivers/gpib/ni_usb/ni_usb_gpib.c b/drivers/gpib/ni_usb/ni_usb_gpib.c
> > index 1f8412de9fa3..2352c6817440 100644
> > --- a/drivers/gpib/ni_usb/ni_usb_gpib.c
> > +++ b/drivers/gpib/ni_usb/ni_usb_gpib.c
> > @@ -1799,10 +1799,12 @@ static int ni_usb_init(struct gpib_board *board)
> >  		return -ENOMEM;
> >  
> >  	writes_len = ni_usb_setup_init(board, writes);
> > -	if (writes_len)
> > -		retval = ni_usb_write_registers(ni_priv, writes, writes_len, &ibsta);
> > -	else
> > +	if (!writes_len) {
> > +		kfree(writes);
> >  		return -EFAULT;
> 
> This is not the correct error value, it should only happen if copy
> to/from user fails.  I know it's not the issue here, just noticed this.
> 
> And why doesn't ni_usb_setup_init() return an error value that should be
> propagated upward?
> 
> thanks,
> 
> greg k-h

Thanks for the suggestion. I will modify ni_usb_setup_init() to return 
proper error codes and propagate them in v3.

Thanks,
Zilin Guan