diff options
author | Jean Delvare <jdelvare@suse.de> | 2018-08-09 09:34:24 +0200 |
---|---|---|
committer | Jean Delvare <jdelvare@suse.de> | 2018-08-09 09:34:24 +0200 |
commit | 815cee74215bf30dc629c8dfa34bca88d67d5733 (patch) | |
tree | 924c721c7e701e79317083e9f54ea81782772443 | |
parent | 83a77865a13c5d73ee80beb9434019c0e01b16ac (diff) | |
download | dmidecode-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.c | 19 |
1 files changed, 10 insertions, 9 deletions
@@ -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; } |