The Linux Kernel Mailing List
 help / color / mirror / Atom feed
From: Andreas Platschek <andi.platschek@gmail.com>
To: gregkh@linuxfoundation.org
Cc: linux-kernel@vger.kernel.org,
	Andreas Platschek <andi.platschek@gmail.com>
Subject: [PATCH] driver-core: only call kfree() if memory was allocated
Date: Fri,  2 Aug 2013 23:43:50 +0200	[thread overview]
Message-ID: <1375479830-8698-1-git-send-email-andi.platschek@gmail.com> (raw)

There are several cases where we could bail out of show_uevent()
before kzalloc() is even called. In those cases we don't want to
call kfree().

In addition the error handling of kzalloc() is now using the
single point of exit as well.

Signed-off-by: Andreas Platschek <andi.platschek@gmail.com>
---
 drivers/base/core.c |    9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)

diff --git a/drivers/base/core.c b/drivers/base/core.c
index 8856d74..bf4effb 100644
--- a/drivers/base/core.c
+++ b/drivers/base/core.c
@@ -372,8 +372,10 @@ static ssize_t show_uevent(struct device *dev, struct device_attribute *attr,
 			goto out;
 
 	env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL);
-	if (!env)
-		return -ENOMEM;
+	if (!env) {
+		count = -ENOMEM;
+		goto out;
+	}
 
 	/* let the kset specific function add its keys */
 	retval = kset->uevent_ops->uevent(kset, &dev->kobj, env);
@@ -383,8 +385,9 @@ static ssize_t show_uevent(struct device *dev, struct device_attribute *attr,
 	/* copy keys to file */
 	for (i = 0; i < env->envp_idx; i++)
 		count += sprintf(&buf[count], "%s\n", env->envp[i]);
-out:
+out_free:
 	kfree(env);
+out:
 	return count;
 }
 
-- 
1.7.10.4


             reply	other threads:[~2013-08-02 21:45 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-08-02 21:43 Andreas Platschek [this message]
2013-08-02 22:04 ` [PATCH] driver-core: only call kfree() if memory was allocated Greg KH
2013-08-03  9:22   ` Greg KH

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=1375479830-8698-1-git-send-email-andi.platschek@gmail.com \
    --to=andi.platschek@gmail.com \
    --cc=gregkh@linuxfoundation.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