Blame 0007-Let-read_file-return-the-actual-data-size.patch

Anton Arapov 0a7886
From de9a74e1c60210bee229fcf55b1678a99d1b44dd Mon Sep 17 00:00:00 2001
Anton Arapov 0a7886
From: Jean Delvare <jdelvare@suse.de>
Anton Arapov 0a7886
Date: Mon, 2 Nov 2015 09:45:26 +0100
Anton Arapov 0a7886
Subject: [PATCH 7/9] Let read_file return the actual data size
Anton Arapov 0a7886
Anton Arapov 0a7886
Let read_file return the actual data size to the caller. This gives
Anton Arapov 0a7886
the caller the possibility to check that the data size is as expected
Anton Arapov 0a7886
and large enough for the purpose, and report to the user if not.
Anton Arapov 0a7886
---
Anton Arapov 0a7886
 CHANGELOG   |  5 +++++
Anton Arapov 0a7886
 dmidecode.c |  4 +++-
Anton Arapov 0a7886
 util.c      | 11 +++++++----
Anton Arapov 0a7886
 util.h      |  2 +-
Anton Arapov 0a7886
 4 files changed, 16 insertions(+), 6 deletions(-)
Anton Arapov 0a7886
Anton Arapov 0a7886
diff --git a/CHANGELOG b/CHANGELOG
Anton Arapov 0a7886
index be2092a..1e5437a 100644
Anton Arapov 0a7886
--- a/CHANGELOG
Anton Arapov 0a7886
+++ b/CHANGELOG
Anton Arapov 0a7886
@@ -1,3 +1,8 @@
Anton Arapov 0a7886
+2015-11-02  Jean Delvare  <jdelvare@suse.de>
Anton Arapov 0a7886
+
Anton Arapov 0a7886
+	* dmidecode.c, util.c, util.h: Let read_file return the actual data
Anton Arapov 0a7886
+	  size.
Anton Arapov 0a7886
+
Anton Arapov 0a7886
 2015-10-21  Xie XiuQi  <xiexiuqi@huawei.com>
Anton Arapov 0a7886
 
Anton Arapov 0a7886
 	* dmidecode.c: Handle SMBIOS 3.0 entry points on EFI systems.
Anton Arapov 0a7886
diff --git a/dmidecode.c b/dmidecode.c
Anton Arapov 0a7886
index 183ced4..a43cfd1 100644
Anton Arapov 0a7886
--- a/dmidecode.c
Anton Arapov 0a7886
+++ b/dmidecode.c
Anton Arapov 0a7886
@@ -4751,6 +4751,7 @@ int main(int argc, char * const argv[])
Anton Arapov 0a7886
 	int ret = 0;                /* Returned value */
Anton Arapov 0a7886
 	int found = 0;
Anton Arapov 0a7886
 	off_t fp;
Anton Arapov 0a7886
+	size_t size;
Anton Arapov 0a7886
 	int efi;
Anton Arapov 0a7886
 	u8 *buf;
Anton Arapov 0a7886
 
Anton Arapov 0a7886
@@ -4820,8 +4821,9 @@ int main(int argc, char * const argv[])
Anton Arapov 0a7886
 	 * contain one of several types of entry points, so read enough for
Anton Arapov 0a7886
 	 * the largest one, then determine what type it contains.
Anton Arapov 0a7886
 	 */
Anton Arapov 0a7886
+	size = 0x20;
Anton Arapov 0a7886
 	if (!(opt.flags & FLAG_NO_SYSFS)
Anton Arapov 0a7886
-	 && (buf = read_file(0x20, SYS_ENTRY_FILE)) != NULL)
Anton Arapov 0a7886
+	 && (buf = read_file(&size, SYS_ENTRY_FILE)) != NULL)
Anton Arapov 0a7886
 	{
Anton Arapov 0a7886
 		if (!(opt.flags & FLAG_QUIET))
Anton Arapov 0a7886
 			printf("Getting SMBIOS data from sysfs.\n");
Anton Arapov 0a7886
diff --git a/util.c b/util.c
Anton Arapov 0a7886
index f97ac0d..52ed413 100644
Anton Arapov 0a7886
--- a/util.c
Anton Arapov 0a7886
+++ b/util.c
Anton Arapov 0a7886
@@ -94,10 +94,11 @@ int checksum(const u8 *buf, size_t len)
Anton Arapov 0a7886
  * needs to be freed by the caller.
Anton Arapov 0a7886
  * This provides a similar usage model to mem_chunk()
Anton Arapov 0a7886
  *
Anton Arapov 0a7886
- * Returns pointer to buffer of max_len bytes, or NULL on error
Anton Arapov 0a7886
+ * Returns pointer to buffer of max_len bytes, or NULL on error, and
Anton Arapov 0a7886
+ * sets max_len to the length actually read.
Anton Arapov 0a7886
  *
Anton Arapov 0a7886
  */
Anton Arapov 0a7886
-void *read_file(size_t max_len, const char *filename)
Anton Arapov 0a7886
+void *read_file(size_t *max_len, const char *filename)
Anton Arapov 0a7886
 {
Anton Arapov 0a7886
 	int fd;
Anton Arapov 0a7886
 	size_t r2 = 0;
Anton Arapov 0a7886
@@ -115,7 +116,7 @@ void *read_file(size_t max_len, const char *filename)
Anton Arapov 0a7886
 		return(NULL);
Anton Arapov 0a7886
 	}
Anton Arapov 0a7886
 
Anton Arapov 0a7886
-	if ((p = malloc(max_len)) == NULL)
Anton Arapov 0a7886
+	if ((p = malloc(*max_len)) == NULL)
Anton Arapov 0a7886
 	{
Anton Arapov 0a7886
 		perror("malloc");
Anton Arapov 0a7886
 		return NULL;
Anton Arapov 0a7886
@@ -123,7 +124,7 @@ void *read_file(size_t max_len, const char *filename)
Anton Arapov 0a7886
 
Anton Arapov 0a7886
 	do
Anton Arapov 0a7886
 	{
Anton Arapov 0a7886
-		r = read(fd, p + r2, max_len - r2);
Anton Arapov 0a7886
+		r = read(fd, p + r2, *max_len - r2);
Anton Arapov 0a7886
 		if (r == -1)
Anton Arapov 0a7886
 		{
Anton Arapov 0a7886
 			if (errno != EINTR)
Anton Arapov 0a7886
@@ -140,6 +141,8 @@ void *read_file(size_t max_len, const char *filename)
Anton Arapov 0a7886
 	while (r != 0);
Anton Arapov 0a7886
 
Anton Arapov 0a7886
 	close(fd);
Anton Arapov 0a7886
+	*max_len = r2;
Anton Arapov 0a7886
+
Anton Arapov 0a7886
 	return p;
Anton Arapov 0a7886
 }
Anton Arapov 0a7886
 
Anton Arapov 0a7886
diff --git a/util.h b/util.h
Anton Arapov 0a7886
index 9d409cd..b8748f1 100644
Anton Arapov 0a7886
--- a/util.h
Anton Arapov 0a7886
+++ b/util.h
Anton Arapov 0a7886
@@ -25,7 +25,7 @@
Anton Arapov 0a7886
 #define ARRAY_SIZE(x) (sizeof(x)/sizeof((x)[0]))
Anton Arapov 0a7886
 
Anton Arapov 0a7886
 int checksum(const u8 *buf, size_t len);
Anton Arapov 0a7886
-void *read_file(size_t len, const char *filename);
Anton Arapov 0a7886
+void *read_file(size_t *len, const char *filename);
Anton Arapov 0a7886
 void *mem_chunk(off_t base, size_t len, const char *devmem);
Anton Arapov 0a7886
 int write_dump(size_t base, size_t len, const void *data, const char *dumpfile, int add);
Anton Arapov 0a7886
 u64 u64_range(u64 start, u64 end);
Anton Arapov 0a7886
-- 
Anton Arapov 0a7886
2.5.0
Anton Arapov 0a7886