File util-dont-leak-a-file-descriptor-in-read_file.patch of Package dmidecode.28672
From: Jean Delvare <jdelvare@suse.de>
Date: Tue, 11 Apr 2017 11:41:38 +0200
Subject: util: Don't leak a file descriptor in read_file
Git-commit: 6953b627a0f11f70662496a77b67aefa9dc40968
Patch-mainline: 3.1
If memory allocation fails, we should close the file descriptor
before returning the error.
Also remove unneeded parentheses on return, return isn't a function.
Signed-off-by: Jean Delvare <jdelvare@suse.de>
Fixes: 2339e722b49e ("util: Add read_file() function for reading sysfs files")
---
util.c | 11 ++++++-----
1 file changed, 6 insertions(+), 5 deletions(-)
--- a/util.c
+++ b/util.c
@@ -113,13 +113,13 @@ void *read_file(size_t *max_len, const c
{
if (errno != ENOENT)
perror(filename);
- return(NULL);
+ return NULL;
}
if ((p = malloc(*max_len)) == NULL)
{
perror("malloc");
- return NULL;
+ goto out;
}
do
@@ -129,10 +129,10 @@ void *read_file(size_t *max_len, const c
{
if (errno != EINTR)
{
- close(fd);
perror(filename);
free(p);
- return NULL;
+ p = NULL;
+ goto out;
}
}
else
@@ -140,8 +140,9 @@ void *read_file(size_t *max_len, const c
}
while (r != 0);
- close(fd);
*max_len = r2;
+out:
+ close(fd);
return p;
}