From: Salah Triki <salah.triki@gmail.com>
To: abbotti@mev.co.uk, hsweeten@visionengravers.com,
gregkh@linuxfoundation.org
Cc: linux-kernel@vger.kernel.org
Subject: [PATCH] comedi: usbdux: free allocated memory/urbs on failure
Date: Thu, 15 Jul 2021 03:26:52 +0100 [thread overview]
Message-ID: <20210715022652.GA2054080@pc> (raw)
Free allocated memory/urbs on failure in order to prevent memory leaks.
Signed-off-by: Salah Triki <salah.triki@gmail.com>
---
drivers/comedi/drivers/usbdux.c | 42 ++++++++++++++++++++++++++++-----
1 file changed, 36 insertions(+), 6 deletions(-)
diff --git a/drivers/comedi/drivers/usbdux.c b/drivers/comedi/drivers/usbdux.c
index 0350f303d557..fe388a9a588a 100644
--- a/drivers/comedi/drivers/usbdux.c
+++ b/drivers/comedi/drivers/usbdux.c
@@ -1456,7 +1456,7 @@ static int usbdux_alloc_usb_buffers(struct comedi_device *dev)
/* one frame: 1ms */
urb = usb_alloc_urb(1, GFP_KERNEL);
if (!urb)
- return -ENOMEM;
+ goto free_ai_urbs1;
devpriv->ai_urbs[i] = urb;
urb->dev = usb;
@@ -1465,7 +1465,7 @@ static int usbdux_alloc_usb_buffers(struct comedi_device *dev)
urb->transfer_flags = URB_ISO_ASAP;
urb->transfer_buffer = kzalloc(SIZEINBUF, GFP_KERNEL);
if (!urb->transfer_buffer)
- return -ENOMEM;
+ goto free_ai_urbs2;
urb->complete = usbduxsub_ai_isoc_irq;
urb->number_of_packets = 1;
@@ -1478,7 +1478,7 @@ static int usbdux_alloc_usb_buffers(struct comedi_device *dev)
/* one frame: 1ms */
urb = usb_alloc_urb(1, GFP_KERNEL);
if (!urb)
- return -ENOMEM;
+ goto free_ao_urbs1;
devpriv->ao_urbs[i] = urb;
urb->dev = usb;
@@ -1487,7 +1487,7 @@ static int usbdux_alloc_usb_buffers(struct comedi_device *dev)
urb->transfer_flags = URB_ISO_ASAP;
urb->transfer_buffer = kzalloc(SIZEOUTBUF, GFP_KERNEL);
if (!urb->transfer_buffer)
- return -ENOMEM;
+ goto free_ao_urbs2;
urb->complete = usbduxsub_ao_isoc_irq;
urb->number_of_packets = 1;
@@ -1504,17 +1504,47 @@ static int usbdux_alloc_usb_buffers(struct comedi_device *dev)
if (devpriv->pwm_buf_sz) {
urb = usb_alloc_urb(0, GFP_KERNEL);
if (!urb)
- return -ENOMEM;
+ goto free_ao_urbs0;
devpriv->pwm_urb = urb;
/* max bulk ep size in high speed */
urb->transfer_buffer = kzalloc(devpriv->pwm_buf_sz,
GFP_KERNEL);
if (!urb->transfer_buffer)
- return -ENOMEM;
+ goto free_urb;
}
return 0;
+
+free_urb:
+ usb_free_urb(urb);
+
+free_ao_urbs0:
+ i = devpriv->n_ao_urbs;
+
+free_ao_urbs1:
+ i--;
+
+free_ao_urbs2:
+ while (i >= 0) {
+ kfree(devpriv->ao_urbs[i]->transfer_buffer);
+ usb_free_urb(devpriv->ao_urbs[i]);
+ i--;
+ }
+ i = devpriv->n_ai_urbs;
+
+free_ai_urbs1:
+ i--;
+
+free_ai_urbs2:
+ while (i >= 0) {
+ kfree(devpriv->ai_urbs[i]->transfer_buffer);
+ usb_free_urb(devpriv->ai_urbs[i]);
+ i--;
+ }
+
+ return -ENOMEM;
+
}
static void usbdux_free_usb_buffers(struct comedi_device *dev)
--
2.25.1
next reply other threads:[~2021-07-15 2:27 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-07-15 2:26 Salah Triki [this message]
2021-07-15 11:34 ` [PATCH] comedi: usbdux: free allocated memory/urbs on failure Ian Abbott
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=20210715022652.GA2054080@pc \
--to=salah.triki@gmail.com \
--cc=abbotti@mev.co.uk \
--cc=gregkh@linuxfoundation.org \
--cc=hsweeten@visionengravers.com \
--cc=linux-kernel@vger.kernel.org \
/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;
as well as URLs for NNTP newsgroup(s).