dmidecode-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[dmidecode] [PATCH 1/3] Let read_file return the actual data size


From: Jean Delvare
Subject: [dmidecode] [PATCH 1/3] Let read_file return the actual data size
Date: Sat, 17 Oct 2015 15:01:35 +0200

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.
---
 dmidecode.c |    4 +++-
 util.c      |   11 +++++++----
 util.h      |    2 +-
 3 files changed, 11 insertions(+), 6 deletions(-)

--- dmidecode.orig/dmidecode.c  2015-10-17 12:04:17.402477928 +0200
+++ dmidecode/dmidecode.c       2015-10-17 12:04:18.810506923 +0200
@@ -4749,6 +4749,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;
 
@@ -4818,8 +4819,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");
--- dmidecode.orig/util.c       2015-10-17 11:03:16.146847970 +0200
+++ dmidecode/util.c    2015-10-17 12:12:33.974792801 +0200
@@ -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;
 }
 
--- dmidecode.orig/util.h       2015-10-17 11:03:16.146847970 +0200
+++ dmidecode/util.h    2015-10-17 12:04:18.811506943 +0200
@@ -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);

-- 
Jean Delvare
SUSE L3 Support



reply via email to

[Prev in Thread] Current Thread [Next in Thread]