Skip to content

Commit

Permalink
util: Let callers pass an offset to read_file
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
jdelvare committed Apr 11, 2017
1 parent 6953b62 commit 6d0486c
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 7 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -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>

Expand Down
4 changes: 2 additions & 2 deletions dmidecode.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 "
Expand Down Expand Up @@ -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");
Expand Down
14 changes: 11 additions & 3 deletions util.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand All @@ -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;
Expand All @@ -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");
Expand Down
4 changes: 2 additions & 2 deletions util.h
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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);

0 comments on commit 6d0486c

Please sign in to comment.