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 <[email protected]> --- util.c | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) --- dmidecode.orig/util.c 2018-08-01 10:55:18.678591293 +0200 +++ dmidecode/util.c 2018-08-01 11:00:03.589815035 +0200 @@ -112,14 +112,6 @@ void *read_file(off_t base, size_t *max_ 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_ 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; } -- Jean Delvare SUSE L3 Support _______________________________________________ https://lists.nongnu.org/mailman/listinfo/dmidecode-devel
