diff mbox series

[34/38] trace-cmd mem: prevent a memory leak in trace_mem()

Message ID 20240605134054.2626953-35-jmarchan@redhat.com (mailing list archive)
State Superseded
Headers show
Series trace-cmd: fix misc issues found by static analysis | expand

Commit Message

Jerome Marchand June 5, 2024, 1:40 p.m. UTC
Close the tracecmd handle in the error path.

Fixes a RESOURCE_LEAK error (CWE-772)

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
---
 tracecmd/trace-mem.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

Comments

Steven Rostedt July 18, 2024, 1:53 a.m. UTC | #1
On Wed,  5 Jun 2024 15:40:49 +0200
"Jerome Marchand" <jmarchan@redhat.com> wrote:

> --- a/tracecmd/trace-mem.c
> +++ b/tracecmd/trace-mem.c
> @@ -555,9 +555,10 @@ void trace_mem(int argc, char **argv)
>  
>  	ret = tracecmd_read_headers(handle, 0);
>  	if (ret)
> -		return;
> +		goto out;
>  
>  	do_trace_mem(handle);
>  
> +out:
>  	tracecmd_close(handle);
>  }
> -- 

Would be nicer to have:

	ret = tracecmd_read_headers(handle, 0);
	if (!ret)
		do_trace_mem(handle);

	tracecmd_close(handle);

-- Steve
Jerome Marchand Oct. 29, 2024, 6:38 a.m. UTC | #2
On 18/07/2024 03:53, Steven Rostedt wrote:
> On Wed,  5 Jun 2024 15:40:49 +0200
> "Jerome Marchand" <jmarchan@redhat.com> wrote:
> 
>> --- a/tracecmd/trace-mem.c
>> +++ b/tracecmd/trace-mem.c
>> @@ -555,9 +555,10 @@ void trace_mem(int argc, char **argv)
>>   
>>   	ret = tracecmd_read_headers(handle, 0);
>>   	if (ret)
>> -		return;
>> +		goto out;
>>   
>>   	do_trace_mem(handle);
>>   
>> +out:
>>   	tracecmd_close(handle);
>>   }
>> -- 
> 
> Would be nicer to have:
> 
> 	ret = tracecmd_read_headers(handle, 0);
> 	if (!ret)
> 		do_trace_mem(handle);
> 
> 	tracecmd_close(handle);

Indeed. I'll send an updated patch.

Jerome

> 
> -- Steve
diff mbox series

Patch

diff --git a/tracecmd/trace-mem.c b/tracecmd/trace-mem.c
index 3e1ac9f3..6182b096 100644
--- a/tracecmd/trace-mem.c
+++ b/tracecmd/trace-mem.c
@@ -555,9 +555,10 @@  void trace_mem(int argc, char **argv)
 
 	ret = tracecmd_read_headers(handle, 0);
 	if (ret)
-		return;
+		goto out;
 
 	do_trace_mem(handle);
 
+out:
 	tracecmd_close(handle);
 }