* [PATCH] staging: gpib: Fix memory leak in ni_usb_init()
@ 2025-12-28 8:19 Zilin Guan
2025-12-29 12:19 ` Dave Penkler
0 siblings, 1 reply; 3+ messages in thread
From: Zilin Guan @ 2025-12-28 8:19 UTC (permalink / raw)
To: dpenkler
Cc: gregkh, matchstick, mingo, tglx, linux-kernel, jianhao.xu, Zilin Guan
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 jumping to the out label to ensure the memory is properly
freed.
Fixes: 4e127de14fa7 ("staging: gpib: Add National Instruments USB GPIB driver")
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>
---
drivers/gpib/ni_usb/ni_usb_gpib.c | 6 +++++-
1 file changed, 5 insertions(+), 1 deletion(-)
diff --git a/drivers/gpib/ni_usb/ni_usb_gpib.c b/drivers/gpib/ni_usb/ni_usb_gpib.c
index 1f8412de9fa3..4cf45e94c750 100644
--- a/drivers/gpib/ni_usb/ni_usb_gpib.c
+++ b/drivers/gpib/ni_usb/ni_usb_gpib.c
@@ -1802,7 +1802,7 @@ static int ni_usb_init(struct gpib_board *board)
if (writes_len)
retval = ni_usb_write_registers(ni_priv, writes, writes_len, &ibsta);
else
- return -EFAULT;
+ goto out;
kfree(writes);
if (retval) {
dev_err(&usb_dev->dev, "register write failed, retval=%i\n", retval);
@@ -1810,6 +1810,10 @@ static int ni_usb_init(struct gpib_board *board)
}
ni_usb_soft_update_status(board, ibsta, 0);
return 0;
+
+out:
+ kfree(writes);
+ return -EFAULT;
}
static void ni_usb_interrupt_complete(struct urb *urb)
--
2.34.1
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH] staging: gpib: Fix memory leak in ni_usb_init()
2025-12-28 8:19 [PATCH] staging: gpib: Fix memory leak in ni_usb_init() Zilin Guan
@ 2025-12-29 12:19 ` Dave Penkler
2025-12-29 15:16 ` Zilin Guan
0 siblings, 1 reply; 3+ messages in thread
From: Dave Penkler @ 2025-12-29 12:19 UTC (permalink / raw)
To: Zilin Guan; +Cc: gregkh, matchstick, mingo, tglx, linux-kernel, jianhao.xu
On Sun, Dec 28, 2025 at 08:19:26AM +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 jumping to the out label to ensure the memory is properly
> freed.
>
> Fixes: 4e127de14fa7 ("staging: gpib: Add National Instruments USB GPIB driver")
> 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>
> ---
> drivers/gpib/ni_usb/ni_usb_gpib.c | 6 +++++-
> 1 file changed, 5 insertions(+), 1 deletion(-)
>
> diff --git a/drivers/gpib/ni_usb/ni_usb_gpib.c b/drivers/gpib/ni_usb/ni_usb_gpib.c
> index 1f8412de9fa3..4cf45e94c750 100644
> --- a/drivers/gpib/ni_usb/ni_usb_gpib.c
> +++ b/drivers/gpib/ni_usb/ni_usb_gpib.c
> @@ -1802,7 +1802,7 @@ static int ni_usb_init(struct gpib_board *board)
> if (writes_len)
> retval = ni_usb_write_registers(ni_priv, writes, writes_len, &ibsta);
> else
> - return -EFAULT;
> + goto out;
> kfree(writes);
> if (retval) {
> dev_err(&usb_dev->dev, "register write failed, retval=%i\n", retval);
> @@ -1810,6 +1810,10 @@ static int ni_usb_init(struct gpib_board *board)
> }
> ni_usb_soft_update_status(board, ibsta, 0);
> return 0;
> +
> +out:
> + kfree(writes);
> + return -EFAULT;
> }
>
> static void ni_usb_interrupt_complete(struct urb *urb)
Good catch.
Prefer simpler variant with check for failure first:
if (!writes_len) {
kfree(writes);
return -EFAULT;
}
retval = ni_usb_write_registers(ni_priv, writes, writes_len, &ibsta);
kfree(writes);
if (retval) {
...
cheers,
-Dave
> --
> 2.34.1
>
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH] staging: gpib: Fix memory leak in ni_usb_init()
2025-12-29 12:19 ` Dave Penkler
@ 2025-12-29 15:16 ` Zilin Guan
0 siblings, 0 replies; 3+ messages in thread
From: Zilin Guan @ 2025-12-29 15:16 UTC (permalink / raw)
To: dpenkler; +Cc: gregkh, jianhao.xu, linux-kernel, matchstick, mingo, tglx, zilin
On Mon, Dec 29, 2025 at 01:19:22PM+0100, Dave Penkler wrote:
> Good catch.
> Prefer simpler variant with check for failure first:
> if (!writes_len) {
> kfree(writes);
> return -EFAULT;
> }
> retval = ni_usb_write_registers(ni_priv, writes, writes_len, &ibsta);
> kfree(writes);
> if (retval) {
> ...
> cheers,
> -Dave
Thanks for the review and the suggestion. I will adopt this approach in v2.
Regards,
Zilin Guan
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2025-12-29 15:16 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-12-28 8:19 [PATCH] staging: gpib: Fix memory leak in ni_usb_init() Zilin Guan
2025-12-29 12:19 ` Dave Penkler
2025-12-29 15:16 ` Zilin Guan
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Powered by JetHome