diff options
author | Jean Delvare <jdelvare@suse.de> | 2017-04-11 11:41:43 +0200 |
---|---|---|
committer | Jean Delvare <jdelvare@suse.de> | 2017-04-11 11:41:43 +0200 |
commit | 6d0486c40d1a68fa5c4c730531cbf32bfd9f76c4 (patch) | |
tree | 6a0233c81f9b77af466d7ddf140862a99b1bb0ad | |
parent | 6953b627a0f11f70662496a77b67aefa9dc40968 (diff) | |
download | dmidecode-git-6d0486c40d1a68fa5c4c730531cbf32bfd9f76c4.tar.gz |
util: Let callers pass an offset to read_file
When reading from a dump file, read_file would be more convenient to
use than mem_chunk, but it lacks an offset parameter.
Signed-off-by: Jean Delvare <jdelvare@suse.de>
-rw-r--r-- | CHANGELOG | 2 | ||||
-rw-r--r-- | dmidecode.c | 4 | ||||
-rw-r--r-- | util.c | 14 | ||||
-rw-r--r-- | util.h | 4 |
4 files changed, 17 insertions, 7 deletions
@@ -1,6 +1,8 @@ 2017-04-11 Jean Delvare <jdelvare@suse.de> * util.c: Don't leak a file descriptor in function read_file. + * util.c, util.c, dmidecode.c: Let callers pass an offset to function + read_file. 2017-04-10 Jean Delvare <jdelvare@suse.de> diff --git a/dmidecode.c b/dmidecode.c index 2d2d672..3f8f03d 100644 --- a/dmidecode.c +++ b/dmidecode.c @@ -4550,7 +4550,7 @@ static void dmi_table(off_t base, u32 len, u16 num, u16 ver, const char *devmem, * result of the kernel truncating the table on parse error. */ size_t size = len; - buf = read_file(&size, devmem); + buf = read_file(0, &size, devmem); if (!(opt.flags & FLAG_QUIET) && num && size != (size_t)len) { fprintf(stderr, "Wrong DMI structures length: %u bytes " @@ -4862,7 +4862,7 @@ int main(int argc, char * const argv[]) */ size = 0x20; if (!(opt.flags & FLAG_NO_SYSFS) - && (buf = read_file(&size, SYS_ENTRY_FILE)) != NULL) + && (buf = read_file(0, &size, SYS_ENTRY_FILE)) != NULL) { if (!(opt.flags & FLAG_QUIET)) printf("Getting SMBIOS data from sysfs.\n"); @@ -2,7 +2,7 @@ * Common "util" functions * This file is part of the dmidecode project. * - * Copyright (C) 2002-2015 Jean Delvare <jdelvare@suse.de> + * Copyright (C) 2002-2017 Jean Delvare <jdelvare@suse.de> * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -89,7 +89,7 @@ int checksum(const u8 *buf, size_t len) } /* - * Reads all of file, up to max_len bytes. + * Reads all of file from given offset, up to max_len bytes. * A buffer of max_len bytes is allocated by this function, and * needs to be freed by the caller. * This provides a similar usage model to mem_chunk() @@ -98,7 +98,7 @@ int checksum(const u8 *buf, size_t len) * sets max_len to the length actually read. * */ -void *read_file(size_t *max_len, const char *filename) +void *read_file(off_t base, size_t *max_len, const char *filename) { int fd; size_t r2 = 0; @@ -116,6 +116,14 @@ void *read_file(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; + } + if ((p = malloc(*max_len)) == NULL) { perror("malloc"); @@ -1,7 +1,7 @@ /* * This file is part of the dmidecode project. * - * Copyright (C) 2003-2015 Jean Delvare <jdelvare@suse.de> + * Copyright (C) 2003-2017 Jean Delvare <jdelvare@suse.de> * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -25,7 +25,7 @@ #define ARRAY_SIZE(x) (sizeof(x)/sizeof((x)[0])) int checksum(const u8 *buf, size_t len); -void *read_file(size_t *len, const char *filename); +void *read_file(off_t base, size_t *len, const char *filename); void *mem_chunk(off_t base, size_t len, const char *devmem); int write_dump(size_t base, size_t len, const void *data, const char *dumpfile, int add); u64 u64_range(u64 start, u64 end); |