linux-mtd.lists.infradead.org archive mirror
 help / color / mirror / Atom feed
From: David Oberhollenzer <david.oberhollenzer@sigma-star.at>
To: linux-mtd@lists.infradead.org
Cc: richard@nod.at, David Oberhollenzer <david.oberhollenzer@sigma-star.at>
Subject: [PATCH 13/15] mtd_debug: cleanup error handling in flash_to_file
Date: Sun, 10 Nov 2019 16:30:57 +0100	[thread overview]
Message-ID: <20191110153059.28878-14-david.oberhollenzer@sigma-star.at> (raw)
In-Reply-To: <20191110153059.28878-1-david.oberhollenzer@sigma-star.at>

The existing code had multiple error handling labels and did things
like checking if a buffer is not NULL before freeing it.

This patch collapses all of this into a single label. We can do this,
because the standard guarantees us that it is safe to call free() with
a NULL pointer.

This also has the side effect of removing the possibility of using the
wrong error label and accidentally leaking something.

Signed-off-by: David Oberhollenzer <david.oberhollenzer@sigma-star.at>
---
 misc-utils/mtd_debug.c | 20 ++++++++------------
 1 file changed, 8 insertions(+), 12 deletions(-)

diff --git a/misc-utils/mtd_debug.c b/misc-utils/mtd_debug.c
index d65ad36..c0b7109 100644
--- a/misc-utils/mtd_debug.c
+++ b/misc-utils/mtd_debug.c
@@ -112,12 +112,12 @@ static int flash_to_file(int fd, off_t offset, size_t len, const char *filename)
 
 	if (offset != lseek(fd, offset, SEEK_SET)) {
 		perror("lseek()");
-		goto err0;
+		return 1;
 	}
 	outfd = creat(filename, 0666);
 	if (outfd < 0) {
 		perror("creat()");
-		goto err1;
+		return 1;
 	}
 
 retry:
@@ -130,7 +130,7 @@ retry:
 			goto retry;
 		}
 		perror("malloc()");
-		goto err0;
+		goto fail;
 	}
 	do {
 		if (n <= size)
@@ -139,7 +139,7 @@ retry:
 		if (err < 0) {
 			fprintf(stderr, "%s: read, size %#x, n %#x\n", __func__, size, n);
 			perror("read()");
-			goto err2;
+			goto fail;
 		}
 		if (err < size) {
 			fprintf(stderr, "%s: short read, requested %#x, read %#x\n", __func__, size, err);
@@ -148,11 +148,11 @@ retry:
 		if (err < 0) {
 			fprintf(stderr, "%s: write, size %#x, n %#x\n", __func__, size, n);
 			perror("write()");
-			goto err2;
+			goto fail;
 		}
 		if (err != size) {
 			fprintf(stderr, "Couldn't copy entire buffer to %s. (%d/%d bytes copied)\n", filename, err, size);
-			goto err2;
+			goto fail;
 		}
 		n -= size;
 	} while (n > 0);
@@ -162,13 +162,9 @@ retry:
 	close(outfd);
 	printf("Copied %zu bytes from address 0x%.8llx in flash to %s\n", len, (unsigned long long)offset, filename);
 	return 0;
-
-err2:
+fail:
 	close(outfd);
-err1:
-	if (buf != NULL)
-		free(buf);
-err0:
+	free(buf);
 	return 1;
 }
 
-- 
2.21.0


______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/

  parent reply	other threads:[~2019-11-10 15:35 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-11-10 15:30 [PATCH 00/15] mtd-utils: cleanup resource leaks David Oberhollenzer
2019-11-10 15:30 ` [PATCH 01/15] mkfs.ubifs: close file descriptor in add_file error path David Oberhollenzer
2019-11-10 15:30 ` [PATCH 02/15] mkfs.ubifs: don't leak copied command line arguments David Oberhollenzer
2019-11-10 15:30 ` [PATCH 03/15] mkfs.ubifs: free derived fscrypt context in add_directory error paths David Oberhollenzer
2019-11-10 15:30 ` [PATCH 04/15] mkfs.ubifs: don't leak hastable iterators David Oberhollenzer
2019-11-10 15:30 ` [PATCH 05/15] mkfs.ubifs: don't leak temporary buffers David Oberhollenzer
2019-11-10 15:30 ` [PATCH 06/15] mkfs.ubifs: propperly cleanup in ALL interpret_table_entry error paths David Oberhollenzer
2019-11-10 15:30 ` [PATCH 07/15] mkfs.jffs2: don't leak temporary buffer if readlink fails David Oberhollenzer
2019-11-10 15:30 ` [PATCH 08/15] libmtd: don't leak temporary buffers David Oberhollenzer
2019-11-10 15:30 ` [PATCH 09/15] ftl_check: " David Oberhollenzer
2019-11-10 15:30 ` [PATCH 10/15] ftl_format: " David Oberhollenzer
2019-11-10 15:30 ` [PATCH 11/15] ubiformat: don't leak file descriptors David Oberhollenzer
2019-11-10 15:30 ` [PATCH 12/15] nanddump: don't leak copied command line arguments David Oberhollenzer
2019-11-10 15:30 ` David Oberhollenzer [this message]
2019-11-10 15:30 ` [PATCH 14/15] jittertest: fix error check for open system call David Oberhollenzer
2019-11-10 15:30 ` [PATCH 15/15] fs-tests: don't leak temporary buffers David Oberhollenzer

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=20191110153059.28878-14-david.oberhollenzer@sigma-star.at \
    --to=david.oberhollenzer@sigma-star.at \
    --cc=linux-mtd@lists.infradead.org \
    --cc=richard@nod.at \
    /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).