summaryrefslogtreecommitdiff
path: root/debian/patches/0120-return_actual_data_size.patch
blob: dc539d94bc5763c625ae80c0ba7bad294fbc22bd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
From de9a74e1c60210bee229fcf55b1678a99d1b44dd Mon Sep 17 00:00:00 2001
From: Jean Delvare <jdelvare@suse.de>
Date: Mon, 02 Nov 2015 08:45:26 +0000
Subject: Let read_file return the actual data size

Let read_file return the actual data size to the caller. This gives
the caller the possibility to check that the data size is as expected
and large enough for the purpose, and report to the user if not.
---
Index: trunk/CHANGELOG
===================================================================
--- trunk.orig/CHANGELOG
+++ trunk/CHANGELOG
@@ -1,3 +1,8 @@
+2015-11-02  Jean Delvare  <jdelvare@suse.de>
+
+	* dmidecode.c, util.c, util.h: Let read_file return the actual data
+	  size.
+
 2015-10-21  Xie XiuQi  <xiexiuqi@huawei.com>
 
 	* dmidecode.c: Handle SMBIOS 3.0 entry points on EFI systems.
Index: trunk/dmidecode.c
===================================================================
--- trunk.orig/dmidecode.c
+++ trunk/dmidecode.c
@@ -4748,6 +4748,7 @@ int main(int argc, char * const argv[])
 	int ret = 0;                /* Returned value */
 	int found = 0;
 	off_t fp;
+	size_t size;
 	int efi;
 	u8 *buf;
 
@@ -4817,8 +4818,9 @@ int main(int argc, char * const argv[])
 	 * contain one of several types of entry points, so read enough for
 	 * the largest one, then determine what type it contains.
 	 */
+	size = 0x20;
 	if (!(opt.flags & FLAG_NO_SYSFS)
-	 && (buf = read_file(0x20, SYS_ENTRY_FILE)) != NULL)
+	 && (buf = read_file(&size, SYS_ENTRY_FILE)) != NULL)
 	{
 		if (!(opt.flags & FLAG_QUIET))
 			printf("Getting SMBIOS data from sysfs.\n");
Index: trunk/util.c
===================================================================
--- trunk.orig/util.c
+++ trunk/util.c
@@ -94,10 +94,11 @@ int checksum(const u8 *buf, size_t len)
  * needs to be freed by the caller.
  * This provides a similar usage model to mem_chunk()
  *
- * Returns pointer to buffer of max_len bytes, or NULL on error
+ * Returns pointer to buffer of max_len bytes, or NULL on error, and
+ * sets max_len to the length actually read.
  *
  */
-void *read_file(size_t max_len, const char *filename)
+void *read_file(size_t *max_len, const char *filename)
 {
 	int fd;
 	size_t r2 = 0;
@@ -115,7 +116,7 @@ void *read_file(size_t max_len, const ch
 		return(NULL);
 	}
 
-	if ((p = malloc(max_len)) == NULL)
+	if ((p = malloc(*max_len)) == NULL)
 	{
 		perror("malloc");
 		return NULL;
@@ -123,7 +124,7 @@ void *read_file(size_t max_len, const ch
 
 	do
 	{
-		r = read(fd, p + r2, max_len - r2);
+		r = read(fd, p + r2, *max_len - r2);
 		if (r == -1)
 		{
 			if (errno != EINTR)
@@ -140,6 +141,8 @@ void *read_file(size_t max_len, const ch
 	while (r != 0);
 
 	close(fd);
+	*max_len = r2;
+
 	return p;
 }
 
Index: trunk/util.h
===================================================================
--- trunk.orig/util.h
+++ trunk/util.h
@@ -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(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);