summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJean Delvare <jdelvare@suse.de>2018-08-09 09:34:24 +0200
committerJean Delvare <jdelvare@suse.de>2018-08-09 09:34:24 +0200
commit815cee74215bf30dc629c8dfa34bca88d67d5733 (patch)
tree924c721c7e701e79317083e9f54ea81782772443
parent83a77865a13c5d73ee80beb9434019c0e01b16ac (diff)
downloaddmidecode-git-815cee74215bf30dc629c8dfa34bca88d67d5733.tar.gz
util: Align read_file() further with mem_chunk()
Make the code flow of read_file() as similar as possible to mem_chunk() to make it easier to read and to avoid bugs: * Move the call to lseek() right before reading the data. This even saves one instruction. * Check for error on close(). This is good practice anyway. Signed-off-by: Jean Delvare <jdelvare@suse.de>
-rw-r--r--util.c19
1 files changed, 10 insertions, 9 deletions
diff --git a/util.c b/util.c
index 276edfe..eff1e41 100644
--- a/util.c
+++ b/util.c
@@ -112,14 +112,6 @@ void *read_file(off_t base, size_t *max_len, const char *filename)
return NULL;
}
- if (lseek(fd, base, SEEK_SET) == -1)
- {
- fprintf(stderr, "%s: ", filename);
- perror("lseek");
- p = NULL;
- goto out;
- }
-
/*
* Check file size, don't allocate more than can be read.
*/
@@ -135,14 +127,23 @@ void *read_file(off_t base, size_t *max_len, const char *filename)
goto out;
}
+ if (lseek(fd, base, SEEK_SET) == -1)
+ {
+ fprintf(stderr, "%s: ", filename);
+ perror("lseek");
+ goto err_free;
+ }
+
if (myread(fd, p, *max_len, filename) == 0)
goto out;
+err_free:
free(p);
p = NULL;
out:
- close(fd);
+ if (close(fd) == -1)
+ perror(filename);
return p;
}