diff mbox series

[08/38] trace-cmd lib: prevent a memory leak in create_event_list_item()

Message ID 20240605134054.2626953-9-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
Free ptr if malloc() fails.

Fixes a RESOURCE_LEAK error (CWE-772)

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

Comments

Steven Rostedt July 17, 2024, 8:31 p.m. UTC | #1
On Wed,  5 Jun 2024 15:40:23 +0200
"Jerome Marchand" <jmarchan@redhat.com> wrote:

> Free ptr if malloc() fails.
> 
> Fixes a RESOURCE_LEAK error (CWE-772)
> 
> Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
> ---
>  lib/trace-cmd/trace-output.c | 3 ++-
>  1 file changed, 2 insertions(+), 1 deletion(-)
> 
> diff --git a/lib/trace-cmd/trace-output.c b/lib/trace-cmd/trace-output.c
> index 59498edc..4a9ecc5d 100644
> --- a/lib/trace-cmd/trace-output.c
> +++ b/lib/trace-cmd/trace-output.c
> @@ -955,7 +955,8 @@ create_event_list_item(struct tracecmd_output *handle,
>  	free(str);
>  	return;
>   err_mem:
> -	 tracecmd_warning("Insufficient memory");
> +	free(ptr);
> +	tracecmd_warning("Insufficient memory");

This function starts with:

	char *ptr;
	char *str;

	str = strdup(list->glob);
	if (!str)
		goto err_mem;

Where ptr is not defined yet.

I'm going to skip this patch.

-- Steve

>  }
>  
>  static int read_ftrace_files(struct tracecmd_output *handle, bool compress)
Jerome Marchand Oct. 29, 2024, 6:26 a.m. UTC | #2
Hi Steven,

Sorry for the long delay. I was on PTO and then this got at the bottom 
of my pile.

On 17/07/2024 22:31, Steven Rostedt wrote:
> On Wed,  5 Jun 2024 15:40:23 +0200
> "Jerome Marchand" <jmarchan@redhat.com> wrote:
> 
>> Free ptr if malloc() fails.
>>
>> Fixes a RESOURCE_LEAK error (CWE-772)
>>
>> Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
>> ---
>>   lib/trace-cmd/trace-output.c | 3 ++-
>>   1 file changed, 2 insertions(+), 1 deletion(-)
>>
>> diff --git a/lib/trace-cmd/trace-output.c b/lib/trace-cmd/trace-output.c
>> index 59498edc..4a9ecc5d 100644
>> --- a/lib/trace-cmd/trace-output.c
>> +++ b/lib/trace-cmd/trace-output.c
>> @@ -955,7 +955,8 @@ create_event_list_item(struct tracecmd_output *handle,
>>   	free(str);
>>   	return;
>>    err_mem:
>> -	 tracecmd_warning("Insufficient memory");
>> +	free(ptr);
>> +	tracecmd_warning("Insufficient memory");
> 
> This function starts with:
> 
> 	char *ptr;
> 	char *str;
> 
> 	str = strdup(list->glob);
> 	if (!str)
> 		goto err_mem;
> 
> Where ptr is not defined yet.
> 
> I'm going to skip this patch.

After taking an other look at this, it looks like it was a false 
positive of the static analyzer anyway.

Jerome

> 
> -- Steve
> 
>>   }
>>   
>>   static int read_ftrace_files(struct tracecmd_output *handle, bool compress)
>
diff mbox series

Patch

diff --git a/lib/trace-cmd/trace-output.c b/lib/trace-cmd/trace-output.c
index 59498edc..4a9ecc5d 100644
--- a/lib/trace-cmd/trace-output.c
+++ b/lib/trace-cmd/trace-output.c
@@ -955,7 +955,8 @@  create_event_list_item(struct tracecmd_output *handle,
 	free(str);
 	return;
  err_mem:
-	 tracecmd_warning("Insufficient memory");
+	free(ptr);
+	tracecmd_warning("Insufficient memory");
 }
 
 static int read_ftrace_files(struct tracecmd_output *handle, bool compress)