Skip to content

Commit 6953b62

Browse files
committed
util: Don't leak a file descriptor in read_file
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 <[email protected]> Fixes: 2339e72 ("util: Add read_file() function for reading sysfs files")
1 parent 2dd4896 commit 6953b62

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

CHANGELOG

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
2017-04-11 Jean Delvare <[email protected]>
2+
3+
* util.c: Don't leak a file descriptor in function read_file.
4+
15
2017-04-10 Jean Delvare <[email protected]>
26

37
* dmidecode.c: Decode the processor ID of the Intel Core M, AMD

util.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -113,13 +113,13 @@ void *read_file(size_t *max_len, const char *filename)
113113
{
114114
if (errno != ENOENT)
115115
perror(filename);
116-
return(NULL);
116+
return NULL;
117117
}
118118

119119
if ((p = malloc(*max_len)) == NULL)
120120
{
121121
perror("malloc");
122-
return NULL;
122+
goto out;
123123
}
124124

125125
do
@@ -129,19 +129,20 @@ void *read_file(size_t *max_len, const char *filename)
129129
{
130130
if (errno != EINTR)
131131
{
132-
close(fd);
133132
perror(filename);
134133
free(p);
135-
return NULL;
134+
p = NULL;
135+
goto out;
136136
}
137137
}
138138
else
139139
r2 += r;
140140
}
141141
while (r != 0);
142142

143-
close(fd);
144143
*max_len = r2;
144+
out:
145+
close(fd);
145146

146147
return p;
147148
}

0 commit comments

Comments
 (0)