summaryrefslogtreecommitdiff
path: root/util.c
diff options
context:
space:
mode:
authorJean Delvare <jdelvare@suse.de>2015-11-02 09:45:26 +0100
committerJean Delvare <jdelvare@suse.de>2015-11-02 09:45:26 +0100
commitde9a74e1c60210bee229fcf55b1678a99d1b44dd (patch)
tree6075ed797d82df3624fa077f8610e95373bb8cfa /util.c
parent2330b708a6d57fd2b8b7e353dd64d037f980a042 (diff)
downloaddmidecode-git-de9a74e1c60210bee229fcf55b1678a99d1b44dd.tar.gz
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.
Diffstat (limited to 'util.c')
-rw-r--r--util.c11
1 files changed, 7 insertions, 4 deletions
diff --git a/util.c b/util.c
index f97ac0d..52ed413 100644
--- a/util.c
+++ b/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 char *filename)
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 char *filename)
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 char *filename)
while (r != 0);
close(fd);
+ *max_len = r2;
+
return p;
}