All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] selftests/x86/lam: fix memory leak and resource leak in lam.c
@ 2025-03-24 12:47 Malaya Kumar Rout
  2025-03-24 12:51 ` Peter Zijlstra
  0 siblings, 1 reply; 13+ messages in thread
From: Malaya Kumar Rout @ 2025-03-24 12:47 UTC (permalink / raw)
  To: malayarout91
  Cc: Thomas Gleixner, Ingo Molnar, Borislav Petkov, Dave Hansen, x86,
	H. Peter Anvin, Shuah Khan, linux-kernel, linux-kselftest

Static Analyis for bench_htab_mem.c with cppcheck:error
tools/testing/selftests/x86/lam.c:585:3:
error: Resource leak: file_fd [resourceLeak]
tools/testing/selftests/x86/lam.c:593:3:
error: Resource leak: file_fd [resourceLeak]
tools/testing/selftests/x86/lam.c:600:3:
error: Memory leak: fi [memleak]
tools/testing/selftests/x86/lam.c:1066:2:
error: Resource leak: fd [resourceLeak]

fix the issue by closing the file descriptors and 
releasing the allocated memory.

Signed-off-by: Malaya Kumar Rout <malayarout91@gmail.com>
---
 tools/testing/selftests/x86/lam.c | 20 +++++++++++++-------
 1 file changed, 13 insertions(+), 7 deletions(-)

diff --git a/tools/testing/selftests/x86/lam.c b/tools/testing/selftests/x86/lam.c
index 4d4a76532dc9..0b43b83ad142 100644
--- a/tools/testing/selftests/x86/lam.c
+++ b/tools/testing/selftests/x86/lam.c
@@ -581,24 +581,28 @@ int do_uring(unsigned long lam)
 	if (file_fd < 0)
 		return 1;
 
-	if (fstat(file_fd, &st) < 0)
+	if (fstat(file_fd, &st) < 0) {
+		close(file_fd);
 		return 1;
-
+		}
 	off_t file_sz = st.st_size;
 
 	int blocks = (int)(file_sz + URING_BLOCK_SZ - 1) / URING_BLOCK_SZ;
 
 	fi = malloc(sizeof(*fi) + sizeof(struct iovec) * blocks);
-	if (!fi)
+	if (!fi) {
+		close(file_fd);
 		return 1;
-
+		}
 	fi->file_sz = file_sz;
 	fi->file_fd = file_fd;
 
 	ring = malloc(sizeof(*ring));
-	if (!ring)
+	if (!ring) {
+		close(file_fd);
+		free(fi);
 		return 1;
-
+		}
 	memset(ring, 0, sizeof(struct io_ring));
 
 	if (setup_io_uring(ring))
@@ -1060,8 +1064,10 @@ void *allocate_dsa_pasid(void)
 
 	wq = mmap(NULL, 0x1000, PROT_WRITE,
 			   MAP_SHARED | MAP_POPULATE, fd, 0);
-	if (wq == MAP_FAILED)
+	if (wq == MAP_FAILED) {
+		close(fd);
 		perror("mmap");
+	}
 
 	return wq;
 }
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 13+ messages in thread

end of thread, other threads:[~2025-04-09 19:41 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-03-24 12:47 [PATCH] selftests/x86/lam: fix memory leak and resource leak in lam.c Malaya Kumar Rout
2025-03-24 12:51 ` Peter Zijlstra
2025-03-25  9:29   ` Ingo Molnar
2025-03-25  9:56     ` malaya kumar rout
2025-03-25 13:25       ` Malaya Kumar Rout
2025-04-07 10:44       ` [PATCH v2] selftests/x86/lam: fix resource leak in do_uring() and allocate_dsa_pasid() Malaya Kumar Rout
2025-04-07 18:20         ` Ingo Molnar
2025-04-07 19:34           ` [PATCH v3] " Malaya Kumar Rout
2025-04-08  8:09             ` Ingo Molnar
2025-04-08 18:52               ` RE:[PATCH RESEND x86-next v4] " Malaya Kumar Rout
2025-04-08 20:12                 ` [PATCH " Ingo Molnar
2025-04-09 13:53                   ` [PATCH " Malaya Kumar Rout
2025-04-09 19:41                     ` [tip: x86/mm] selftests/x86/lam: Fix clean up fds " tip-bot2 for Malaya Kumar Rout

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.