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 <[email protected]>
---
 ...ix-fclose-leak-in-uio_line_from_file.patch | 31 +++++++++++++++++++
 .../libuio/libuio_0.2.1.bbappend              |  3 ++
 2 files changed, 34 insertions(+)
 create mode 100644 
meta-oe/recipes-extended/libuio/libuio/fix-fclose-leak-in-uio_line_from_file.patch
 create mode 100644 meta-oe/recipes-extended/libuio/libuio_0.2.1.bbappend

diff --git 
a/meta-oe/recipes-extended/libuio/libuio/fix-fclose-leak-in-uio_line_from_file.patch
 
b/meta-oe/recipes-extended/libuio/libuio/fix-fclose-leak-in-uio_line_from_file.patch
new file mode 100644
index 0000000000..b5c984c23d
--- /dev/null
+++ 
b/meta-oe/recipes-extended/libuio/libuio/fix-fclose-leak-in-uio_line_from_file.patch
@@ -0,0 +1,31 @@
+From: Qliangw <[email protected]>
+Subject: [PATCH] Fix FILE descriptor leak in uio_line_from_file()
+
+The function uio_line_from_file() fails to close the FILE pointer
+when fgets() returns NULL, causing a file descriptor leak.
+
+Upstream-Status: Pending
+Signed-off-by: Qliangw <[email protected]>
+
+--- a/src/uio_line_from_file.c
++++ b/src/uio_line_from_file.c
+@@ -28,14 +28,17 @@
+ {
+       char *s;
+       int i;
++      int ret = 0;
+       memset(linebuf, 0, UIO_MAX_NAME_SIZE);
+       FILE* file = fopen(filename,"r");
+       if (!file) return -1;
+       s = fgets(linebuf,UIO_MAX_NAME_SIZE,file);
+-      if (!s) return -2;
++      if (!s) { ret = -2; goto out; }
+       for (i=0; (*s)&&(i<UIO_MAX_NAME_SIZE); i++) {
+               if (*s == '\n') *s = 0;
+               s++;
+       }
+-      return 0;
++out:
++      fclose(file);
++      return ret;
+ }
diff --git a/meta-oe/recipes-extended/libuio/libuio_0.2.1.bbappend 
b/meta-oe/recipes-extended/libuio/libuio_0.2.1.bbappend
new file mode 100644
index 0000000000..d0e0dbb9a8
--- /dev/null
+++ b/meta-oe/recipes-extended/libuio/libuio_0.2.1.bbappend
@@ -0,0 +1,3 @@
+FILESEXTRAPATHS:prepend := "${THISDIR}/${PN}:"
+
+SRC_URI += "file://fix-fclose-leak-in-uio_line_from_file.patch"
-- 
2.53.0.windows.2

-=-=-=-=-=-=-=-=-=-=-=-
Links: You receive all messages sent to this group.
View/Reply Online (#127271): 
https://lists.openembedded.org/g/openembedded-devel/message/127271
Mute This Topic: https://lists.openembedded.org/mt/119525599/21656
Group Owner: [email protected]
Unsubscribe: https://lists.openembedded.org/g/openembedded-devel/unsub 
[[email protected]]
-=-=-=-=-=-=-=-=-=-=-=-

Reply via email to