diff mbox series

[4/4] libtraceevent: don't return a pointer to a local variable get_field_str()

Message ID 20240607160542.46152-5-jmarchan@redhat.com (mailing list archive)
State New
Headers show
Series libtraceevent: fix misc issues found by static analysis | expand

Commit Message

Jerome Marchand June 7, 2024, 4:05 p.m. UTC
The function get_field_str() can return a pointer to string on the
stack. Replace it by a global variable.

Fixes a RETURN_LOCAL error (CWE-562)

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

Comments

Steven Rostedt June 14, 2024, 7:49 p.m. UTC | #1
On Fri,  7 Jun 2024 18:05:42 +0200
"Jerome Marchand" <jmarchan@redhat.com> wrote:

> The function get_field_str() can return a pointer to string on the
> stack. Replace it by a global variable.
> 
> Fixes a RETURN_LOCAL error (CWE-562)
> 
> Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
> ---
>  src/parse-filter.c | 3 ++-
>  1 file changed, 2 insertions(+), 1 deletion(-)
> 
> diff --git a/src/parse-filter.c b/src/parse-filter.c
> index e448ee2..f0c0ae1 100644
> --- a/src/parse-filter.c
> +++ b/src/parse-filter.c
> @@ -1698,6 +1698,8 @@ static int test_num(struct tep_event *event, struct tep_filter_arg *arg,
>  	}
>  }
>  
> +char hex[64];
> +
>  static const char *get_field_str(struct tep_filter_arg *arg, struct tep_record *record)
>  {
>  	struct tep_event *event;
> @@ -1705,7 +1707,6 @@ static const char *get_field_str(struct tep_filter_arg *arg, struct tep_record *
>  	unsigned long long addr;
>  	const char *val = NULL;
>  	unsigned int size;
> -	char hex[64];

Instead of making hex a global variable (which is incorrect for
multiple reasons, and can caus bugs elsewhere). Just make it a static
variable here.

	static char hex[64];

I'll fix this and give you a reported-by.

-- Steve


>  
>  	/* If the field is not a string convert it */
>  	if (arg->str.field->flags & TEP_FIELD_IS_STRING) {
diff mbox series

Patch

diff --git a/src/parse-filter.c b/src/parse-filter.c
index e448ee2..f0c0ae1 100644
--- a/src/parse-filter.c
+++ b/src/parse-filter.c
@@ -1698,6 +1698,8 @@  static int test_num(struct tep_event *event, struct tep_filter_arg *arg,
 	}
 }
 
+char hex[64];
+
 static const char *get_field_str(struct tep_filter_arg *arg, struct tep_record *record)
 {
 	struct tep_event *event;
@@ -1705,7 +1707,6 @@  static const char *get_field_str(struct tep_filter_arg *arg, struct tep_record *
 	unsigned long long addr;
 	const char *val = NULL;
 	unsigned int size;
-	char hex[64];
 
 	/* If the field is not a string convert it */
 	if (arg->str.field->flags & TEP_FIELD_IS_STRING) {