mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Tom Rix <trix@redhat.com>
To: jpoimboe@redhat.com, peterz@infradead.org, mingo@kernel.org, bp@suse.de
Cc: linux-kernel@vger.kernel.org, Tom Rix <trix@redhat.com>
Subject: [PATCH] objtool: rework error handling in objtool_create_backup
Date: Sat, 30 Apr 2022 13:32:40 -0400	[thread overview]
Message-ID: <20220430173240.3346735-1-trix@redhat.com> (raw)

The cppcheck reports this issue
[tools/objtool/objtool.c:65]: (error) Memory leak: name

This is a general problem.  When ojbtool_create_backup() fails anywhere it
returns an error without freeing any of the buffers it allocates.  So
rework the error handler to goto the appropriate free level when an error
occurs.

Fixes: 8ad15c690084 ("objtool: Add --backup")
Signed-off-by: Tom Rix <trix@redhat.com>
---
 tools/objtool/objtool.c | 23 +++++++++++++++--------
 1 file changed, 15 insertions(+), 8 deletions(-)

diff --git a/tools/objtool/objtool.c b/tools/objtool/objtool.c
index 512669ce064c..2fd10804e7a7 100644
--- a/tools/objtool/objtool.c
+++ b/tools/objtool/objtool.c
@@ -25,6 +25,7 @@ static bool objtool_create_backup(const char *_objname)
 {
 	int len = strlen(_objname);
 	char *buf, *base, *name = malloc(len+6);
+	bool ret = false;
 	int s, d, l, t;
 
 	if (!name) {
@@ -38,19 +39,19 @@ static bool objtool_create_backup(const char *_objname)
 	d = open(name, O_CREAT|O_WRONLY|O_TRUNC, 0644);
 	if (d < 0) {
 		perror("failed to create backup file");
-		return false;
+		goto err1;
 	}
 
 	s = open(_objname, O_RDONLY);
 	if (s < 0) {
 		perror("failed to open orig file");
-		return false;
+		goto err2;
 	}
 
 	buf = malloc(4096);
 	if (!buf) {
 		perror("failed backup data malloc");
-		return false;
+		goto err3;
 	}
 
 	while ((l = read(s, buf, 4096)) > 0) {
@@ -59,7 +60,7 @@ static bool objtool_create_backup(const char *_objname)
 			t = write(d, base, l);
 			if (t < 0) {
 				perror("failed backup write");
-				return false;
+				goto err4;
 			}
 			base += t;
 			l -= t;
@@ -68,15 +69,21 @@ static bool objtool_create_backup(const char *_objname)
 
 	if (l < 0) {
 		perror("failed backup read");
-		return false;
+		goto err4;
 	}
 
-	free(name);
+	ret = true;
+
+err4:
 	free(buf);
-	close(d);
+err3:
 	close(s);
+err2:
+	close(d);
+err1:
+	free(name);
 
-	return true;
+	return ret;
 }
 
 struct objtool_file *objtool_open_read(const char *_objname)
-- 
2.27.0


             reply	other threads:[~2022-04-30 17:33 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-04-30 17:32 Tom Rix [this message]
2022-04-30 17:40 ` Peter Zijlstra

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=20220430173240.3346735-1-trix@redhat.com \
    --to=trix@redhat.com \
    --cc=bp@suse.de \
    --cc=jpoimboe@redhat.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@kernel.org \
    --cc=peterz@infradead.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

all inboxes | Powered by JetHome®