The Linux Kernel Mailing List
 help / color / mirror / Atom feed
From: Greg KH <gregkh@linuxfoundation.org>
To: Zilin Guan <zilin@seu.edu.cn>
Cc: dpenkler@gmail.com, matchstick@neverthere.org, mingo@kernel.org,
	tglx@linutronix.de, linux-kernel@vger.kernel.org,
	Jianhao Xu <jianhao.xu@seu.edu.cn>
Subject: Re: [PATCH v2] staging: gpib: Fix memory leak in ni_usb_init()
Date: Mon, 29 Dec 2025 16:28:21 +0100	[thread overview]
Message-ID: <2025122925-navigator-settle-9873@gregkh> (raw)
In-Reply-To: <20251229152203.475658-1-zilin@seu.edu.cn>

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

  reply	other threads:[~2025-12-29 15:28 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-12-29 15:22 [PATCH v2] staging: gpib: Fix memory leak in ni_usb_init() Zilin Guan
2025-12-29 15:28 ` Greg KH [this message]
2025-12-30  3:29   ` Zilin Guan

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=2025122925-navigator-settle-9873@gregkh \
    --to=gregkh@linuxfoundation.org \
    --cc=dpenkler@gmail.com \
    --cc=jianhao.xu@seu.edu.cn \
    --cc=linux-kernel@vger.kernel.org \
    --cc=matchstick@neverthere.org \
    --cc=mingo@kernel.org \
    --cc=tglx@linutronix.de \
    --cc=zilin@seu.edu.cn \
    /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