Skip to content

Commit 7bbb904

Browse files
committed
libuio: fix FILE descriptor leak
The function uio_line_from_file() fails to close the FILE pointer when fgets() returns NULL, causing a file descriptor leak. This can be triggered when reading from /sys files that return empty content, leading to resource exhaustion over time. Fix this by using goto-based error handling to ensure fclose() is called on all exit paths. Signed-off-by: Qliangw <qili00001@gmail.com>
1 parent 71cc98a commit 7bbb904

2 files changed

Lines changed: 27 additions & 0 deletions

File tree

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
2+
--- a/src/uio_line_from_file.c
3+
+++ b/src/uio_line_from_file.c
4+
@@ -28,14 +28,17 @@
5+
{
6+
char *s;
7+
int i;
8+
+ int ret = 0;
9+
memset(linebuf, 0, UIO_MAX_NAME_SIZE);
10+
FILE* file = fopen(filename,"r");
11+
if (!file) return -1;
12+
s = fgets(linebuf,UIO_MAX_NAME_SIZE,file);
13+
- if (!s) return -2;
14+
+ if (!s) { ret = -2; goto out; }
15+
for (i=0; (*s)&&(i<UIO_MAX_NAME_SIZE); i++) {
16+
if (*s == '\n') *s = 0;
17+
s++;
18+
}
19+
- return 0;
20+
+out:
21+
+ fclose(file);
22+
+ return ret;
23+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
FILESEXTRAPATHS:prepend := "${THISDIR}/${PN}:"
3+
4+
SRC_URI += "file://fix-fclose-leak-in-uio_line_from_file.patch"

0 commit comments

Comments
 (0)