mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Andrey Smirnov <andrew.smirnov@gmail.com>
To: barebox@lists.infradead.org
Cc: Andrey Smirnov <andrew.smirnov@gmail.com>
Subject: [PATCH 01/17] libfile: Make failure path of open_and_lseek() consistent
Date: Wed,  6 Mar 2019 23:49:10 -0800	[thread overview]
Message-ID: <20190307074926.20539-2-andrew.smirnov@gmail.com> (raw)
In-Reply-To: <20190307074926.20539-1-andrew.smirnov@gmail.com>

Change the code of open_and_lseek() to make sure that opened file is
closed in every failure path as well. While at it make sure we don't
mix returning function return code and returning errno by opting to
always return the former.

Signed-off-by: Andrey Smirnov <andrew.smirnov@gmail.com>
---
 lib/libfile.c | 26 ++++++++++++--------------
 1 file changed, 12 insertions(+), 14 deletions(-)

diff --git a/lib/libfile.c b/lib/libfile.c
index 9a223d232..089749253 100644
--- a/lib/libfile.c
+++ b/lib/libfile.c
@@ -522,12 +522,12 @@ err_out1:
  * @mode:	The file open mode
  * @pos:	The position to lseek to
  *
- * Return: If successful this function returns a positive filedescriptor
- *         number, otherwise a negative error code is returned
+ * Return: If successful this function returns a positive
+ *         filedescriptor number, otherwise -1 is returned
  */
 int open_and_lseek(const char *filename, int mode, loff_t pos)
 {
-	int fd, ret;
+	int fd;
 
 	fd = open(filename, mode);
 	if (fd < 0) {
@@ -541,28 +541,26 @@ int open_and_lseek(const char *filename, int mode, loff_t pos)
 	if (mode & (O_WRONLY | O_RDWR)) {
 		struct stat s;
 
-		ret = fstat(fd, &s);
-		if (ret) {
+		if (fstat(fd, &s)) {
 			perror("fstat");
-			return ret;
+			goto out;
 		}
 
-		if (s.st_size < pos) {
-			ret = ftruncate(fd, pos);
-			if (ret) {
-				perror("ftruncate");
-				return ret;
-			}
+		if (s.st_size < pos && ftruncate(fd, pos)) {
+			perror("ftruncate");
+			goto out;
 		}
 	}
 
 	if (lseek(fd, pos, SEEK_SET) != pos) {
 		perror("lseek");
-		close(fd);
-		return -errno;
+		goto out;
 	}
 
 	return fd;
+out:
+	close(fd);
+	return -1;
 }
 
 /**
-- 
2.20.1


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

  reply	other threads:[~2019-03-07  7:49 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-03-07  7:49 [PATCH 00/17] lseek related fixes Andrey Smirnov
2019-03-07  7:49 ` Andrey Smirnov [this message]
2019-03-07  7:49 ` [PATCH 02/17] common: Always return enum filetype in file_name_detect_type_offset() Andrey Smirnov
2019-03-07  7:49 ` [PATCH 03/17] MIPS: ath79: Use errno to get error code from open_and_lseek() Andrey Smirnov
2019-03-07  7:49 ` [PATCH 04/17] ARM: i.MX: bbu: Fix variable type in imx_bbu_external_nand_update() Andrey Smirnov
2019-03-07  7:49 ` [PATCH 05/17] ARM: i.MX: bbu: Fix lseek error check " Andrey Smirnov
2019-03-07  7:49 ` [PATCH 06/17] ARM: i.MX: bbu: Fix lseek error check in imx_bbu_internal_v2_write_nand_dbbt() Andrey Smirnov
2019-03-07  7:49 ` [PATCH 07/17] ARM: i.MX: bbu: Fix variable type " Andrey Smirnov
2019-03-07  7:49 ` [PATCH 08/17] bpkfs: Fix lseek error check in bpkfs_probe() Andrey Smirnov
2019-03-07  7:49 ` [PATCH 09/17] uimage: Fix lseek error check in uimage_verify() Andrey Smirnov
2019-03-07  7:49 ` [PATCH 10/17] uimage: Fix lseek error check in uimage_load() Andrey Smirnov
2019-03-07  7:49 ` [PATCH 11/17] uimage: Fix lseek error check in uimage_load_to_buf() Andrey Smirnov
2019-03-07  7:49 ` [PATCH 12/17] state: Fix lseek error check in state_backend_bucket_direct_read() Andrey Smirnov
2019-03-07  7:49 ` [PATCH 13/17] state: Fix lseek error check in state_backend_bucket_direct_write() Andrey Smirnov
2019-03-07  7:49 ` [PATCH 14/17] state: Fix lseek error check in state_mtd_peb_read() Andrey Smirnov
2019-03-07  7:49 ` [PATCH 15/17] state: Fix lseek error check in state_mtd_peb_write() Andrey Smirnov
2019-03-07  7:49 ` [PATCH 16/17] commands: loadxy: Make use of open_and_lseek() Andrey Smirnov
2019-03-07  7:49 ` [PATCH 17/17] commands: loadb: " Andrey Smirnov
2019-03-11  6:56 ` [PATCH 00/17] lseek related fixes Sascha Hauer

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=20190307074926.20539-2-andrew.smirnov@gmail.com \
    --to=andrew.smirnov@gmail.com \
    --cc=barebox@lists.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