From: SF Markus Elfring <elfring@users.sourceforge.net>
To: devel@driverdev.osuosl.org, Chris Cesare <chris.cesare@gmail.com>,
Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
H Hartley Sweeten <hsweeten@visionengravers.com>,
Ian Abbott <abbotti@mev.co.uk>
Cc: LKML <linux-kernel@vger.kernel.org>, kernel-janitors@vger.kernel.org
Subject: [PATCH 1/5] staging: comedi: serial2002: Combine four kcalloc() calls into one in serial2002_setup_subdevs()
Date: Thu, 8 Dec 2016 12:33:02 +0100 [thread overview]
Message-ID: <011f1cb9-481c-9d9c-bcf5-8373f1796a0a@users.sourceforge.net> (raw)
In-Reply-To: <8cddc37d-4529-ada4-ac21-20e09e888d0d@users.sourceforge.net>
From: Markus Elfring <elfring@users.sourceforge.net>
Date: Thu, 8 Dec 2016 07:37:29 +0100
The function "kcalloc" was called in three cases by the function
"serial2002_setup_subdevs" without checking immediately if it failed.
This issue was detected by using the Coccinelle software.
* Perform the desired memory allocation (and release at the end) by a single
function call instead.
* Adjust a jump target so that a redundant check is avoided.
Fixes: 623a73926c7012e3bb132e225621890207f5c611 ("staging: comedi: serial2002: split up serial_2002_open()")
Signed-off-by: Markus Elfring <elfring@users.sourceforge.net>
---
drivers/staging/comedi/drivers/serial2002.c | 22 +++++++++++-----------
1 file changed, 11 insertions(+), 11 deletions(-)
diff --git a/drivers/staging/comedi/drivers/serial2002.c b/drivers/staging/comedi/drivers/serial2002.c
index 0d33e520f635..9542f4f8afe0 100644
--- a/drivers/staging/comedi/drivers/serial2002.c
+++ b/drivers/staging/comedi/drivers/serial2002.c
@@ -392,6 +392,7 @@ static int serial2002_setup_subdevice(struct comedi_subdevice *s,
static int serial2002_setup_subdevs(struct comedi_device *dev)
{
struct serial2002_private *devpriv = dev->private;
+ struct config_t *array;
struct config_t *di_cfg;
struct config_t *do_cfg;
struct config_t *ai_cfg;
@@ -402,15 +403,17 @@ static int serial2002_setup_subdevs(struct comedi_device *dev)
int i;
/* Allocate the temporary structs to hold the configuration data */
- di_cfg = kcalloc(32, sizeof(*cfg), GFP_KERNEL);
- do_cfg = kcalloc(32, sizeof(*cfg), GFP_KERNEL);
- ai_cfg = kcalloc(32, sizeof(*cfg), GFP_KERNEL);
- ao_cfg = kcalloc(32, sizeof(*cfg), GFP_KERNEL);
- if (!di_cfg || !do_cfg || !ai_cfg || !ao_cfg) {
+ array = kcalloc(4 * 32, sizeof(*cfg), GFP_KERNEL);
+ if (!array) {
result = -ENOMEM;
- goto err_alloc_configs;
+ goto check_tty;
}
+ di_cfg = array;
+ do_cfg = array + 1 * 32;
+ ai_cfg = array + 2 * 32;
+ ao_cfg = array + 3 * 32;
+
/* Read the configuration from the connected device */
serial2002_tty_setspeed(devpriv->tty, devpriv->speed);
serial2002_poll_channel(devpriv->tty, 31);
@@ -534,13 +537,10 @@ static int serial2002_setup_subdevs(struct comedi_device *dev)
}
}
-err_alloc_configs:
- kfree(di_cfg);
- kfree(do_cfg);
- kfree(ai_cfg);
- kfree(ao_cfg);
+ kfree(array);
if (result) {
+check_tty:
if (devpriv->tty) {
filp_close(devpriv->tty, NULL);
devpriv->tty = NULL;
--
2.11.0
next prev parent reply other threads:[~2016-12-08 11:33 UTC|newest]
Thread overview: 17+ messages / expand[flat|nested] mbox.gz Atom feed top
2016-12-08 11:30 [PATCH 0/5] staging-COMEDI: Fine-tuning for three functions SF Markus Elfring
2016-12-08 11:33 ` SF Markus Elfring [this message]
2016-12-08 12:22 ` [PATCH 1/5] staging: comedi: serial2002: Combine four kcalloc() calls into one in serial2002_setup_subdevs() Dan Carpenter
2016-12-08 11:34 ` [PATCH 2/5] staging: comedi: usbdux: Split a condition check in usbdux_alloc_usb_buffers() SF Markus Elfring
2016-12-08 12:35 ` Dan Carpenter
2016-12-08 12:37 ` Dan Carpenter
2016-12-08 12:44 ` Ian Abbott
2016-12-08 15:43 ` SF Markus Elfring
2016-12-08 11:35 ` [PATCH 3/5] staging: comedi: usbdux: Move an assignment " SF Markus Elfring
2016-12-08 12:40 ` Dan Carpenter
2016-12-08 11:37 ` [PATCH 4/5] staging: comedi: usbduxsigma: Split a condition check in usbduxsigma_alloc_usb_buffers() SF Markus Elfring
2016-12-08 12:51 ` Ian Abbott
2016-12-08 15:46 ` SF Markus Elfring
2016-12-08 18:12 ` Ian Abbott
2016-12-08 11:38 ` [PATCH 5/5] staging: comedi: usbduxsigma: Move an assignment " SF Markus Elfring
2016-12-08 13:30 ` [PATCH 0/5] staging-COMEDI: Fine-tuning for three functions Greg Kroah-Hartman
2016-12-08 15:26 ` SF Markus Elfring
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=011f1cb9-481c-9d9c-bcf5-8373f1796a0a@users.sourceforge.net \
--to=elfring@users.sourceforge.net \
--cc=abbotti@mev.co.uk \
--cc=chris.cesare@gmail.com \
--cc=devel@driverdev.osuosl.org \
--cc=gregkh@linuxfoundation.org \
--cc=hsweeten@visionengravers.com \
--cc=kernel-janitors@vger.kernel.org \
--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).