Re: [PATCH] selftests/x86/lam: fix memory leak and resource leak in lam.c

Ingo Molnar
2025-03-25T09:29:56Z

* 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