* [PATCH] SPI: Fix a mem leak in dw_spi_setup()
@ 2011-03-13 21:22 Jesper Juhl
0 siblings, 0 replies; only message in thread
From: Jesper Juhl @ 2011-03-13 21:22 UTC (permalink / raw)
To: linux-kernel; +Cc: spi-devel-general, Grant Likely, David Brownell
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
^ permalink raw reply related [flat|nested] only message in thread
only message in thread, other threads:[~2011-03-13 21:22 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-03-13 21:22 [PATCH] SPI: Fix a mem leak in dw_spi_setup() Jesper Juhl
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).