diff mbox series

selftests/x86/lam: fix memory leak and resource leak in lam.c

Message ID 20250324124810.883767-1-malayarout91@gmail.com (mailing list archive)
State New
Headers show
Series selftests/x86/lam: fix memory leak and resource leak in lam.c | expand

Commit Message

Malaya Kumar Rout March 24, 2025, 12:47 p.m. UTC
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(-)

Comments

Peter Zijlstra March 24, 2025, 12:51 p.m. UTC | #1
On Mon, Mar 24, 2025 at 06:17:50PM +0530, Malaya Kumar Rout wrote:
> 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.
> 

But but but, doesn't the program just exit on any of those 'errors'
anyway?

That is, iirc this is a single shot program.
Ingo Molnar March 25, 2025, 9:29 a.m. UTC | #2
* Peter Zijlstra <peterz@infradead.org> wrote:

> On Mon, Mar 24, 2025 at 06:17:50PM +0530, Malaya Kumar Rout wrote:
> > 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.
> > 
> 
> But but but, doesn't the program just exit on any of those 'errors'
> anyway?
> 
> That is, iirc this is a single shot program.

While that's true, still proper cleanup of resources is a good practice 
- and in more complicated tools it's useful to fix even these 
semi-false-positives, to make sure other warnings don't get missed. 

Having said that, the error/cleanup control flow here doesn't look 
overly clean here to begin with, so I'd suggest fixing that (with goto 
labels or such) - which would fix the file_fd 'leak' as a happy side 
effect.

Thanks,

	Ingo
Malaya Kumar Rout March 25, 2025, 9:56 a.m. UTC | #3
I appreciate all the feedback and recommendations provided. We will
incorporate the same.

Thanks & Regards,
Malaya Kumar Rout

On Tue, Mar 25, 2025 at 2:59 PM Ingo Molnar <mingo@kernel.org> wrote:
>
>
> * Peter Zijlstra <peterz@infradead.org> wrote:
>
> > On Mon, Mar 24, 2025 at 06:17:50PM +0530, Malaya Kumar Rout wrote:
> > > 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.
> > >
> >
> > But but but, doesn't the program just exit on any of those 'errors'
> > anyway?
> >
> > That is, iirc this is a single shot program.
>
> While that's true, still proper cleanup of resources is a good practice
> - and in more complicated tools it's useful to fix even these
> semi-false-positives, to make sure other warnings don't get missed.
>
> Having said that, the error/cleanup control flow here doesn't look
> overly clean here to begin with, so I'd suggest fixing that (with goto
> labels or such) - which would fix the file_fd 'leak' as a happy side
> effect.
>
> Thanks,
>
>         Ingo
diff mbox series

Patch

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;
 }