mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: David Woodhouse <dwmw2@infradead.org>
To: "Jörn Engel" <joern@wohnheim.fh-wedel.de>
Cc: "Jesper Juhl" <jesper.juhl@gmail.com>,
	linux-kernel@vger.kernel.org,
	"Jochen Schäuble" <psionic@psionic.de>,
	"Thomas Gleixner" <tglx@linutronix.de>
Subject: Re: [PATCH] mtd: fix memory leaks in phram_setup
Date: Sun, 14 May 2006 12:03:49 +0100	[thread overview]
Message-ID: <1147604629.12379.4.camel@pmac.infradead.org> (raw)
In-Reply-To: <20060514063731.GA22033@wohnheim.fh-wedel.de>

On Sun, 2006-05-14 at 08:37 +0200, Jörn Engel wrote:
> The only question is: does it make the code better?  The code has
> seven printk/return combinations.  Each of them would chew up 2 more
> lines without the macro.  So phram_setup would grow from 44 to 58
> lines, not nice either.

How about this then...

diff --git a/drivers/mtd/devices/phram.c b/drivers/mtd/devices/phram.c
index e09e416..fd2d43f 100644
--- a/drivers/mtd/devices/phram.c
+++ b/drivers/mtd/devices/phram.c
@@ -187,36 +187,41 @@ static int ustrtoul(const char *cp, char
 	return result;
 }
 
-static int parse_num32(uint32_t *num32, const char *token)
+static int parse_num32(uint32_t *num32, const char *token, char *name)
 {
 	char *endp;
 	unsigned long n;
 
 	n = ustrtoul(token, &endp, 0);
-	if (*endp)
+	if (*endp) {
+		ERROR("Illegal %s\n", name);
 		return -EINVAL;
+	}
 
 	*num32 = n;
 	return 0;
 }
 
-static int parse_name(char **pname, const char *token)
+static char *parse_name(const char *token)
 {
 	size_t len;
 	char *name;
 
 	len = strlen(token) + 1;
-	if (len > 64)
-		return -ENOSPC;
+	if (len > 64) {
+		ERROR("name too long");
+		return ERR_PTR(-ENOSPC);
+	}
 
 	name = kmalloc(len, GFP_KERNEL);
-	if (!name)
-		return -ENOMEM;
+	if (!name) {
+		ERROR("out of memory");
+		return ERR_PTR(-ENOMEM);
+	}
 
 	strcpy(name, token);
 
-	*pname = name;
-	return 0;
+	return name;
 }
 
 
@@ -228,11 +233,6 @@ static inline void kill_final_newline(ch
 }
 
 
-#define parse_err(fmt, args...) do {	\
-	ERROR(fmt , ## args);	\
-	return 0;		\
-} while (0)
-
 static int phram_setup(const char *val, struct kernel_param *kp)
 {
 	char buf[64+12+12], *str = buf;
@@ -242,8 +242,10 @@ static int phram_setup(const char *val, 
 	uint32_t len;
 	int i, ret;
 
-	if (strnlen(val, sizeof(buf)) >= sizeof(buf))
-		parse_err("parameter too long\n");
+	if (strnlen(val, sizeof(buf)) >= sizeof(buf)) {
+		ERROR("parameter too long\n");
+		return -EINVAL;
+	}
 
 	strcpy(str, val);
 	kill_final_newline(str);
@@ -251,30 +253,24 @@ static int phram_setup(const char *val, 
 	for (i=0; i<3; i++)
 		token[i] = strsep(&str, ",");
 
-	if (str)
-		parse_err("too many arguments\n");
-
-	if (!token[2])
-		parse_err("not enough arguments\n");
-
-	ret = parse_name(&name, token[0]);
-	if (ret == -ENOMEM)
-		parse_err("out of memory\n");
-	if (ret == -ENOSPC)
-		parse_err("name too long\n");
-	if (ret)
-		return 0;
+	if (str) {
+		ERROR("too many arguments\n");
+		return -EINVAL;
+	}
 
-	ret = parse_num32(&start, token[1]);
-	if (ret) {
-		kfree(name);
-		parse_err("illegal start address\n");
+	if (!token[2]) {
+		ERROR("not enough arguments\n");
+		return -EINVAL;
 	}
 
-	ret = parse_num32(&len, token[2]);
-	if (ret) {
+	name = prase_name(token[0]);
+	if (IS_ERR(name))
+	    return PTR_ERR(name);
+
+	if ((ret = parse_num32(&start, token[1], "start address")) ||
+	    (ret = parse_num32(&len, token[2], "device length"))) {
 		kfree(name);
-		parse_err("illegal device length\n");
+		return ret;
 	}
 
 	register_device(name, start, len);

-- 
dwmw2


  reply	other threads:[~2006-05-14 11:03 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-05-13 23:07 Jesper Juhl
2006-05-13 23:18 ` David Woodhouse
2006-05-13 23:34   ` Jesper Juhl
2006-05-13 23:40     ` David Woodhouse
2006-05-14  6:37       ` Jörn Engel
2006-05-14 11:03         ` David Woodhouse [this message]
2006-05-14 13:21           ` Jörn Engel
2006-05-14 16:37             ` Ingo Oeser
2006-05-14 20:07               ` Jörn Engel

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=1147604629.12379.4.camel@pmac.infradead.org \
    --to=dwmw2@infradead.org \
    --cc=jesper.juhl@gmail.com \
    --cc=joern@wohnheim.fh-wedel.de \
    --cc=linux-kernel@vger.kernel.org \
    --cc=psionic@psionic.de \
    --cc=tglx@linutronix.de \
    /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

all inboxes | Powered by JetHome®