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

Disha Goel disgoel@linux.ibm.com
Tue Aug 18 14:14:34 CEST 2026


Initialize fd to -1 to distinguish an unopened descriptor, and add
a cleanup guard so SAFE_CLOSE is only called when fd was actually
opened. Without this, cleanup would call close(-1) causing TBROK
when the anon test variant (n=0) runs and never opens fd.

Signed-off-by: Disha Goel <disgoel@linux.ibm.com>
---
v1 -> v2:
- Fix commit message: clarify that fd=0 would close stdin (not silently
  skip), and that the guard is added by this patch not pre-existing
- Use 'fd != -1' instead of 'fd > -1' per LTP convention

 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..52096f697 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