[LTP] [PATCH 1/2] memcg/memcontrol02: fix fd leak in cleanup path

Disha Goel disgoel@linux.ibm.com
Fri Aug 7 15:05:21 CEST 2026


fd is initialized to 0 (a valid file descriptor) so the cleanup()
guard 'if (fd > -1)' would never trigger. Initialize to -1 and
add SAFE_CLOSE to prevent the descriptor leaking when a SAFE_*
macro calls tst_brk() during the pagecache allocation test.

Signed-off-by: Disha Goel <disgoel@linux.ibm.com>
---
 testcases/kernel/controllers/memcg/memcontrol02.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/testcases/kernel/controllers/memcg/memcontrol02.c b/testcases/kernel/controllers/memcg/memcontrol02.c
index 0b79403c9..75a1e6d51 100644
--- a/testcases/kernel/controllers/memcg/memcontrol02.c
+++ b/testcases/kernel/controllers/memcg/memcontrol02.c
@@ -29,7 +29,7 @@
 
 static size_t page_size;
 static struct tst_cg_group *cg_child;
-static int fd;
+static int fd = -1;
 static int file_to_all_error = 10;
 
 static void alloc_anon_50M_check(void)
@@ -128,6 +128,8 @@ static void cleanup(void)
 {
 	if (cg_child)
 		cg_child = tst_cg_group_rm(cg_child);
+	if (fd > -1)
+		SAFE_CLOSE(fd);
 }
 
 static struct tst_test test = {
-- 
2.45.1



More information about the ltp mailing list