linux-spi.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Jesper Juhl <jj@chaosbits.net>
To: linux-kernel@vger.kernel.org
Cc: spi-devel-general@lists.sourceforge.net,
	Grant Likely <grant.likely@secretlab.ca>,
	David Brownell <dbrownell@users.sourceforge.net>
Subject: [PATCH] SPI: Fix a mem leak in dw_spi_setup()
Date: Sun, 13 Mar 2011 22:22:45 +0100 (CET)	[thread overview]
Message-ID: <alpine.LNX.2.00.1103132216530.29706@swampdragon.chaosbits.net> (raw)

In drivers/spi/dw_spi.c:dw_spi_setup() we may dynamically allocate memory 
for 'chip' with 'kmalloc'. If we then later leave the function early due 
to '!spi->max_speed_hz' we fail to release this memory again - causing a 
leak when the 'chip' variable goes out of scope.
As far as I can see, the cleanest fix for this is to simply move the test 
for '!spi->max_speed_hz' to the top of the function before we even do the 
dynamic allocation. This way we avoid the issue of having to remember to 
free the memory again and we also avoid a lot of assignments that are 
completely pointless in the failure case.

Signed-off-by: Jesper Juhl <jj@chaosbits.net>
---
 dw_spi.c |   11 +++++------
 1 file changed, 5 insertions(+), 6 deletions(-)

 compile tested only.

diff --git a/drivers/spi/dw_spi.c b/drivers/spi/dw_spi.c
index 22af77f..6cd678c 100644
--- a/drivers/spi/dw_spi.c
+++ b/drivers/spi/dw_spi.c
@@ -702,6 +702,11 @@ static int dw_spi_setup(struct spi_device *spi)
 	if (spi->bits_per_word != 8 && spi->bits_per_word != 16)
 		return -EINVAL;
 
+	if (!spi->max_speed_hz) {
+		dev_err(&spi->dev, "No max speed HZ parameter\n");
+		return -EINVAL;
+	}
+
 	/* Only alloc on first setup */
 	chip = spi_get_ctldata(spi);
 	if (!chip) {
@@ -746,13 +751,7 @@ static int dw_spi_setup(struct spi_device *spi)
 		return -EINVAL;
 	}
 	chip->bits_per_word = spi->bits_per_word;
-
-	if (!spi->max_speed_hz) {
-		dev_err(&spi->dev, "No max speed HZ parameter\n");
-		return -EINVAL;
-	}
 	chip->speed_hz = spi->max_speed_hz;
-
 	chip->tmode = 0; /* Tx & Rx */
 	/* Default SPI mode is SCPOL = 0, SCPH = 0 */
 	chip->cr0 = (chip->bits_per_word - 1)


-- 
Jesper Juhl <jj@chaosbits.net>            http://www.chaosbits.net/
Plain text mails only, please.
Don't top-post http://www.catb.org/~esr/jargon/html/T/top-post.html

                 reply	other threads:[~2011-03-13 21:22 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=alpine.LNX.2.00.1103132216530.29706@swampdragon.chaosbits.net \
    --to=jj@chaosbits.net \
    --cc=dbrownell@users.sourceforge.net \
    --cc=grant.likely@secretlab.ca \
    --cc=linux-kernel@vger.kernel.org \
    --cc=spi-devel-general@lists.sourceforge.net \
    /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).