Skip to content

Commit 6d0486c

Browse files
committed
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 <[email protected]>
1 parent 6953b62 commit 6d0486c

File tree

4 files changed

+17
-7
lines changed

4 files changed

+17
-7
lines changed

CHANGELOG

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
2017-04-11 Jean Delvare <[email protected]>
22

33
* util.c: Don't leak a file descriptor in function read_file.
4+
* util.c, util.c, dmidecode.c: Let callers pass an offset to function
5+
read_file.
46

57
2017-04-10 Jean Delvare <[email protected]>
68

dmidecode.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4550,7 +4550,7 @@ static void dmi_table(off_t base, u32 len, u16 num, u16 ver, const char *devmem,
45504550
* result of the kernel truncating the table on parse error.
45514551
*/
45524552
size_t size = len;
4553-
buf = read_file(&size, devmem);
4553+
buf = read_file(0, &size, devmem);
45544554
if (!(opt.flags & FLAG_QUIET) && num && size != (size_t)len)
45554555
{
45564556
fprintf(stderr, "Wrong DMI structures length: %u bytes "
@@ -4862,7 +4862,7 @@ int main(int argc, char * const argv[])
48624862
*/
48634863
size = 0x20;
48644864
if (!(opt.flags & FLAG_NO_SYSFS)
4865-
&& (buf = read_file(&size, SYS_ENTRY_FILE)) != NULL)
4865+
&& (buf = read_file(0, &size, SYS_ENTRY_FILE)) != NULL)
48664866
{
48674867
if (!(opt.flags & FLAG_QUIET))
48684868
printf("Getting SMBIOS data from sysfs.\n");

util.c

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* Common "util" functions
33
* This file is part of the dmidecode project.
44
*
5-
* Copyright (C) 2002-2015 Jean Delvare <[email protected]>
5+
* Copyright (C) 2002-2017 Jean Delvare <[email protected]>
66
*
77
* This program is free software; you can redistribute it and/or modify
88
* 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)
8989
}
9090

9191
/*
92-
* Reads all of file, up to max_len bytes.
92+
* Reads all of file from given offset, up to max_len bytes.
9393
* A buffer of max_len bytes is allocated by this function, and
9494
* needs to be freed by the caller.
9595
* This provides a similar usage model to mem_chunk()
@@ -98,7 +98,7 @@ int checksum(const u8 *buf, size_t len)
9898
* sets max_len to the length actually read.
9999
*
100100
*/
101-
void *read_file(size_t *max_len, const char *filename)
101+
void *read_file(off_t base, size_t *max_len, const char *filename)
102102
{
103103
int fd;
104104
size_t r2 = 0;
@@ -116,6 +116,14 @@ void *read_file(size_t *max_len, const char *filename)
116116
return NULL;
117117
}
118118

119+
if (lseek(fd, base, SEEK_SET) == -1)
120+
{
121+
fprintf(stderr, "%s: ", filename);
122+
perror("lseek");
123+
p = NULL;
124+
goto out;
125+
}
126+
119127
if ((p = malloc(*max_len)) == NULL)
120128
{
121129
perror("malloc");

util.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* This file is part of the dmidecode project.
33
*
4-
* Copyright (C) 2003-2015 Jean Delvare <[email protected]>
4+
* Copyright (C) 2003-2017 Jean Delvare <[email protected]>
55
*
66
* This program is free software; you can redistribute it and/or modify
77
* it under the terms of the GNU General Public License as published by
@@ -25,7 +25,7 @@
2525
#define ARRAY_SIZE(x) (sizeof(x)/sizeof((x)[0]))
2626

2727
int checksum(const u8 *buf, size_t len);
28-
void *read_file(size_t *len, const char *filename);
28+
void *read_file(off_t base, size_t *len, const char *filename);
2929
void *mem_chunk(off_t base, size_t len, const char *devmem);
3030
int write_dump(size_t base, size_t len, const void *data, const char *dumpfile, int add);
3131
u64 u64_range(u64 start, u64 end);

0 commit comments

Comments
 (0)