2020-02-20 02:01:27

by He Zhe

[permalink] [raw]
Subject: [PATCH v3] tools lib traceevent: Take care of return value of asprintf

From: He Zhe <[email protected]>

According to the API, if memory allocation wasn't possible, or some other
error occurs, asprintf will return -1, and the contents of strp below are
undefined.

int asprintf(char **strp, const char *fmt, ...);

This patch takes care of return value of asprintf to make it less error
prone and prevent the following build warning.

ignoring return value of ‘asprintf’, declared with attribute warn_unused_result [-Wunused-result]

Signed-off-by: He Zhe <[email protected]>
---
v2: directly check the return value without saving to a variable
v3: as asked, not remove those that already save the return value

tools/lib/traceevent/parse-filter.c | 29 +++++++++++++++++++----------
1 file changed, 19 insertions(+), 10 deletions(-)

diff --git a/tools/lib/traceevent/parse-filter.c b/tools/lib/traceevent/parse-filter.c
index 20eed71..c271aee 100644
--- a/tools/lib/traceevent/parse-filter.c
+++ b/tools/lib/traceevent/parse-filter.c
@@ -1958,7 +1958,8 @@ static char *op_to_str(struct tep_event_filter *filter, struct tep_filter_arg *a
default:
break;
}
- asprintf(&str, val ? "TRUE" : "FALSE");
+ if (asprintf(&str, val ? "TRUE" : "FALSE") < 0)
+ str = NULL;
break;
}
}
@@ -1976,7 +1977,8 @@ static char *op_to_str(struct tep_event_filter *filter, struct tep_filter_arg *a
break;
}

- asprintf(&str, "(%s) %s (%s)", left, op, right);
+ if (asprintf(&str, "(%s) %s (%s)", left, op, right) < 0)
+ str = NULL;
break;

case TEP_FILTER_OP_NOT:
@@ -1992,10 +1994,12 @@ static char *op_to_str(struct tep_event_filter *filter, struct tep_filter_arg *a
right_val = 0;
if (right_val >= 0) {
/* just return the opposite */
- asprintf(&str, right_val ? "FALSE" : "TRUE");
+ if (asprintf(&str, right_val ? "FALSE" : "TRUE") < 0)
+ str = NULL;
break;
}
- asprintf(&str, "%s(%s)", op, right);
+ if (asprintf(&str, "%s(%s)", op, right) < 0)
+ str = NULL;
break;

default:
@@ -2011,7 +2015,8 @@ static char *val_to_str(struct tep_event_filter *filter, struct tep_filter_arg *
{
char *str = NULL;

- asprintf(&str, "%lld", arg->value.val);
+ if (asprintf(&str, "%lld", arg->value.val) < 0)
+ str = NULL;

return str;
}
@@ -2069,7 +2074,8 @@ static char *exp_to_str(struct tep_event_filter *filter, struct tep_filter_arg *
break;
}

- asprintf(&str, "%s %s %s", lstr, op, rstr);
+ if (asprintf(&str, "%s %s %s", lstr, op, rstr) < 0)
+ str = NULL;
out:
free(lstr);
free(rstr);
@@ -2113,7 +2119,8 @@ static char *num_to_str(struct tep_event_filter *filter, struct tep_filter_arg *
if (!op)
op = "<=";

- asprintf(&str, "%s %s %s", lstr, op, rstr);
+ if (asprintf(&str, "%s %s %s", lstr, op, rstr) < 0)
+ str = NULL;
break;

default:
@@ -2148,8 +2155,9 @@ static char *str_to_str(struct tep_event_filter *filter, struct tep_filter_arg *
if (!op)
op = "!~";

- asprintf(&str, "%s %s \"%s\"",
- arg->str.field->name, op, arg->str.val);
+ if (asprintf(&str, "%s %s \"%s\"",
+ arg->str.field->name, op, arg->str.val) < 0)
+ str = NULL;
break;

default:
@@ -2165,7 +2173,8 @@ static char *arg_to_str(struct tep_event_filter *filter, struct tep_filter_arg *

switch (arg->type) {
case TEP_FILTER_ARG_BOOLEAN:
- asprintf(&str, arg->boolean.value ? "TRUE" : "FALSE");
+ if (asprintf(&str, arg->boolean.value ? "TRUE" : "FALSE") < 0)
+ str = NULL;
return str;

case TEP_FILTER_ARG_OP:
--
2.7.4


2020-03-25 13:09:05

by He Zhe

[permalink] [raw]
Subject: Re: [PATCH v3] tools lib traceevent: Take care of return value of asprintf

Can this be considered for the moment?

Thanks,
Zhe

On 2/20/20 9:58 AM, [email protected] wrote:
> From: He Zhe <[email protected]>
>
> According to the API, if memory allocation wasn't possible, or some other
> error occurs, asprintf will return -1, and the contents of strp below are
> undefined.
>
> int asprintf(char **strp, const char *fmt, ...);
>
> This patch takes care of return value of asprintf to make it less error
> prone and prevent the following build warning.
>
> ignoring return value of ‘asprintf’, declared with attribute warn_unused_result [-Wunused-result]
>
> Signed-off-by: He Zhe <[email protected]>
> ---
> v2: directly check the return value without saving to a variable
> v3: as asked, not remove those that already save the return value
>
> tools/lib/traceevent/parse-filter.c | 29 +++++++++++++++++++----------
> 1 file changed, 19 insertions(+), 10 deletions(-)
>
> diff --git a/tools/lib/traceevent/parse-filter.c b/tools/lib/traceevent/parse-filter.c
> index 20eed71..c271aee 100644
> --- a/tools/lib/traceevent/parse-filter.c
> +++ b/tools/lib/traceevent/parse-filter.c
> @@ -1958,7 +1958,8 @@ static char *op_to_str(struct tep_event_filter *filter, struct tep_filter_arg *a
> default:
> break;
> }
> - asprintf(&str, val ? "TRUE" : "FALSE");
> + if (asprintf(&str, val ? "TRUE" : "FALSE") < 0)
> + str = NULL;
> break;
> }
> }
> @@ -1976,7 +1977,8 @@ static char *op_to_str(struct tep_event_filter *filter, struct tep_filter_arg *a
> break;
> }
>
> - asprintf(&str, "(%s) %s (%s)", left, op, right);
> + if (asprintf(&str, "(%s) %s (%s)", left, op, right) < 0)
> + str = NULL;
> break;
>
> case TEP_FILTER_OP_NOT:
> @@ -1992,10 +1994,12 @@ static char *op_to_str(struct tep_event_filter *filter, struct tep_filter_arg *a
> right_val = 0;
> if (right_val >= 0) {
> /* just return the opposite */
> - asprintf(&str, right_val ? "FALSE" : "TRUE");
> + if (asprintf(&str, right_val ? "FALSE" : "TRUE") < 0)
> + str = NULL;
> break;
> }
> - asprintf(&str, "%s(%s)", op, right);
> + if (asprintf(&str, "%s(%s)", op, right) < 0)
> + str = NULL;
> break;
>
> default:
> @@ -2011,7 +2015,8 @@ static char *val_to_str(struct tep_event_filter *filter, struct tep_filter_arg *
> {
> char *str = NULL;
>
> - asprintf(&str, "%lld", arg->value.val);
> + if (asprintf(&str, "%lld", arg->value.val) < 0)
> + str = NULL;
>
> return str;
> }
> @@ -2069,7 +2074,8 @@ static char *exp_to_str(struct tep_event_filter *filter, struct tep_filter_arg *
> break;
> }
>
> - asprintf(&str, "%s %s %s", lstr, op, rstr);
> + if (asprintf(&str, "%s %s %s", lstr, op, rstr) < 0)
> + str = NULL;
> out:
> free(lstr);
> free(rstr);
> @@ -2113,7 +2119,8 @@ static char *num_to_str(struct tep_event_filter *filter, struct tep_filter_arg *
> if (!op)
> op = "<=";
>
> - asprintf(&str, "%s %s %s", lstr, op, rstr);
> + if (asprintf(&str, "%s %s %s", lstr, op, rstr) < 0)
> + str = NULL;
> break;
>
> default:
> @@ -2148,8 +2155,9 @@ static char *str_to_str(struct tep_event_filter *filter, struct tep_filter_arg *
> if (!op)
> op = "!~";
>
> - asprintf(&str, "%s %s \"%s\"",
> - arg->str.field->name, op, arg->str.val);
> + if (asprintf(&str, "%s %s \"%s\"",
> + arg->str.field->name, op, arg->str.val) < 0)
> + str = NULL;
> break;
>
> default:
> @@ -2165,7 +2173,8 @@ static char *arg_to_str(struct tep_event_filter *filter, struct tep_filter_arg *
>
> switch (arg->type) {
> case TEP_FILTER_ARG_BOOLEAN:
> - asprintf(&str, arg->boolean.value ? "TRUE" : "FALSE");
> + if (asprintf(&str, arg->boolean.value ? "TRUE" : "FALSE") < 0)
> + str = NULL;
> return str;
>
> case TEP_FILTER_ARG_OP:

2020-03-25 13:25:16

by Steven Rostedt

[permalink] [raw]
Subject: Re: [PATCH v3] tools lib traceevent: Take care of return value of asprintf

On Wed, 25 Mar 2020 21:05:46 +0800
He Zhe <[email protected]> wrote:

> Can this be considered for the moment?
>

Thanks for the reminder. This patch was pushed down in my queue by lots of
other patches I need to review. I'll try to take a look at it this week.

-- Steve

2020-03-25 13:38:08

by Arnaldo Carvalho de Melo

[permalink] [raw]
Subject: Re: [PATCH v3] tools lib traceevent: Take care of return value of asprintf

Em Wed, Mar 25, 2020 at 09:05:46PM +0800, He Zhe escreveu:
> Can this be considered for the moment?

Rostedt, have you looked at this?

- Arnaldo

> Thanks,
> Zhe
>
> On 2/20/20 9:58 AM, [email protected] wrote:
> > From: He Zhe <[email protected]>
> >
> > According to the API, if memory allocation wasn't possible, or some other
> > error occurs, asprintf will return -1, and the contents of strp below are
> > undefined.
> >
> > int asprintf(char **strp, const char *fmt, ...);
> >
> > This patch takes care of return value of asprintf to make it less error
> > prone and prevent the following build warning.
> >
> > ignoring return value of ‘asprintf’, declared with attribute warn_unused_result [-Wunused-result]
> >
> > Signed-off-by: He Zhe <[email protected]>
> > ---
> > v2: directly check the return value without saving to a variable
> > v3: as asked, not remove those that already save the return value
> >
> > tools/lib/traceevent/parse-filter.c | 29 +++++++++++++++++++----------
> > 1 file changed, 19 insertions(+), 10 deletions(-)
> >
> > diff --git a/tools/lib/traceevent/parse-filter.c b/tools/lib/traceevent/parse-filter.c
> > index 20eed71..c271aee 100644
> > --- a/tools/lib/traceevent/parse-filter.c
> > +++ b/tools/lib/traceevent/parse-filter.c
> > @@ -1958,7 +1958,8 @@ static char *op_to_str(struct tep_event_filter *filter, struct tep_filter_arg *a
> > default:
> > break;
> > }
> > - asprintf(&str, val ? "TRUE" : "FALSE");
> > + if (asprintf(&str, val ? "TRUE" : "FALSE") < 0)
> > + str = NULL;
> > break;
> > }
> > }
> > @@ -1976,7 +1977,8 @@ static char *op_to_str(struct tep_event_filter *filter, struct tep_filter_arg *a
> > break;
> > }
> >
> > - asprintf(&str, "(%s) %s (%s)", left, op, right);
> > + if (asprintf(&str, "(%s) %s (%s)", left, op, right) < 0)
> > + str = NULL;
> > break;
> >
> > case TEP_FILTER_OP_NOT:
> > @@ -1992,10 +1994,12 @@ static char *op_to_str(struct tep_event_filter *filter, struct tep_filter_arg *a
> > right_val = 0;
> > if (right_val >= 0) {
> > /* just return the opposite */
> > - asprintf(&str, right_val ? "FALSE" : "TRUE");
> > + if (asprintf(&str, right_val ? "FALSE" : "TRUE") < 0)
> > + str = NULL;
> > break;
> > }
> > - asprintf(&str, "%s(%s)", op, right);
> > + if (asprintf(&str, "%s(%s)", op, right) < 0)
> > + str = NULL;
> > break;
> >
> > default:
> > @@ -2011,7 +2015,8 @@ static char *val_to_str(struct tep_event_filter *filter, struct tep_filter_arg *
> > {
> > char *str = NULL;
> >
> > - asprintf(&str, "%lld", arg->value.val);
> > + if (asprintf(&str, "%lld", arg->value.val) < 0)
> > + str = NULL;
> >
> > return str;
> > }
> > @@ -2069,7 +2074,8 @@ static char *exp_to_str(struct tep_event_filter *filter, struct tep_filter_arg *
> > break;
> > }
> >
> > - asprintf(&str, "%s %s %s", lstr, op, rstr);
> > + if (asprintf(&str, "%s %s %s", lstr, op, rstr) < 0)
> > + str = NULL;
> > out:
> > free(lstr);
> > free(rstr);
> > @@ -2113,7 +2119,8 @@ static char *num_to_str(struct tep_event_filter *filter, struct tep_filter_arg *
> > if (!op)
> > op = "<=";
> >
> > - asprintf(&str, "%s %s %s", lstr, op, rstr);
> > + if (asprintf(&str, "%s %s %s", lstr, op, rstr) < 0)
> > + str = NULL;
> > break;
> >
> > default:
> > @@ -2148,8 +2155,9 @@ static char *str_to_str(struct tep_event_filter *filter, struct tep_filter_arg *
> > if (!op)
> > op = "!~";
> >
> > - asprintf(&str, "%s %s \"%s\"",
> > - arg->str.field->name, op, arg->str.val);
> > + if (asprintf(&str, "%s %s \"%s\"",
> > + arg->str.field->name, op, arg->str.val) < 0)
> > + str = NULL;
> > break;
> >
> > default:
> > @@ -2165,7 +2173,8 @@ static char *arg_to_str(struct tep_event_filter *filter, struct tep_filter_arg *
> >
> > switch (arg->type) {
> > case TEP_FILTER_ARG_BOOLEAN:
> > - asprintf(&str, arg->boolean.value ? "TRUE" : "FALSE");
> > + if (asprintf(&str, arg->boolean.value ? "TRUE" : "FALSE") < 0)
> > + str = NULL;
> > return str;
> >
> > case TEP_FILTER_ARG_OP:

2020-04-07 02:25:02

by Steven Rostedt

[permalink] [raw]
Subject: Re: [PATCH v3] tools lib traceevent: Take care of return value of asprintf

On Wed, 25 Mar 2020 10:35:51 -0300
Arnaldo Carvalho de Melo <[email protected]> wrote:

> Em Wed, Mar 25, 2020 at 09:05:46PM +0800, He Zhe escreveu:
> > Can this be considered for the moment?
>
> Rostedt, have you looked at this?
>

Bah, I did but forgot to reply.

Yes, add my Reviewed-by: Steven Rostedt (VMware) <[email protected]>

Sorry for not sending this out earlier.

-- Steve

2020-04-07 12:34:27

by Arnaldo Carvalho de Melo

[permalink] [raw]
Subject: Re: [PATCH v3] tools lib traceevent: Take care of return value of asprintf

Em Mon, Apr 06, 2020 at 10:24:11PM -0400, Steven Rostedt escreveu:
> On Wed, 25 Mar 2020 10:35:51 -0300
> Arnaldo Carvalho de Melo <[email protected]> wrote:
>
> > Em Wed, Mar 25, 2020 at 09:05:46PM +0800, He Zhe escreveu:
> > > Can this be considered for the moment?
> >
> > Rostedt, have you looked at this?
> >
>
> Bah, I did but forgot to reply.
>
> Yes, add my Reviewed-by: Steven Rostedt (VMware) <[email protected]>
>
> Sorry for not sending this out earlier.

no problem, I'm finishing a round of tests to submit a batch to Ingo,
will pick it up for the next one.

Thanks,

- Arnaldo

2020-04-16 16:10:35

by Arnaldo Carvalho de Melo

[permalink] [raw]
Subject: Re: [PATCH v3] tools lib traceevent: Take care of return value of asprintf

Em Mon, Apr 06, 2020 at 10:24:11PM -0400, Steven Rostedt escreveu:
> On Wed, 25 Mar 2020 10:35:51 -0300
> Arnaldo Carvalho de Melo <[email protected]> wrote:
>
> > Em Wed, Mar 25, 2020 at 09:05:46PM +0800, He Zhe escreveu:
> > > Can this be considered for the moment?
> >
> > Rostedt, have you looked at this?
> >
>
> Bah, I did but forgot to reply.
>
> Yes, add my Reviewed-by: Steven Rostedt (VMware) <[email protected]>
>
> Sorry for not sending this out earlier.

Thanks, finally applied.

- Arnaldo

2020-04-22 12:28:03

by tip-bot2 for Haifeng Xu

[permalink] [raw]
Subject: [tip: perf/core] tools lib traceevent: Take care of return value of asprintf

The following commit has been merged into the perf/core branch of tip:

Commit-ID: f8ff18be1f5c6ba1c2befb043bea6e7eaf9f8987
Gitweb: https://git.kernel.org/tip/f8ff18be1f5c6ba1c2befb043bea6e7eaf9f8987
Author: He Zhe <[email protected]>
AuthorDate: Thu, 20 Feb 2020 09:58:50 +08:00
Committer: Arnaldo Carvalho de Melo <[email protected]>
CommitterDate: Sat, 18 Apr 2020 09:05:00 -03:00

tools lib traceevent: Take care of return value of asprintf

According to the API, if memory allocation wasn't possible, or some
other error occurs, asprintf will return -1, and the contents of strp
below are undefined.

int asprintf(char **strp, const char *fmt, ...);

This patch takes care of return value of asprintf to make it less error
prone and prevent the following build warning.

ignoring return value of ‘asprintf’, declared with attribute warn_unused_result [-Wunused-result]

Signed-off-by: He Zhe <[email protected]>
Reviewed-by: Steven Rostedt (VMware) <[email protected]>
Cc: Tzvetomir Stoyanov <[email protected]>
Cc: [email protected]
Link: http://lore.kernel.org/lkml/[email protected]
Signed-off-by: Arnaldo Carvalho de Melo <[email protected]>
---
tools/lib/traceevent/parse-filter.c | 29 ++++++++++++++++++----------
1 file changed, 19 insertions(+), 10 deletions(-)

diff --git a/tools/lib/traceevent/parse-filter.c b/tools/lib/traceevent/parse-filter.c
index 20eed71..c271aee 100644
--- a/tools/lib/traceevent/parse-filter.c
+++ b/tools/lib/traceevent/parse-filter.c
@@ -1958,7 +1958,8 @@ static char *op_to_str(struct tep_event_filter *filter, struct tep_filter_arg *a
default:
break;
}
- asprintf(&str, val ? "TRUE" : "FALSE");
+ if (asprintf(&str, val ? "TRUE" : "FALSE") < 0)
+ str = NULL;
break;
}
}
@@ -1976,7 +1977,8 @@ static char *op_to_str(struct tep_event_filter *filter, struct tep_filter_arg *a
break;
}

- asprintf(&str, "(%s) %s (%s)", left, op, right);
+ if (asprintf(&str, "(%s) %s (%s)", left, op, right) < 0)
+ str = NULL;
break;

case TEP_FILTER_OP_NOT:
@@ -1992,10 +1994,12 @@ static char *op_to_str(struct tep_event_filter *filter, struct tep_filter_arg *a
right_val = 0;
if (right_val >= 0) {
/* just return the opposite */
- asprintf(&str, right_val ? "FALSE" : "TRUE");
+ if (asprintf(&str, right_val ? "FALSE" : "TRUE") < 0)
+ str = NULL;
break;
}
- asprintf(&str, "%s(%s)", op, right);
+ if (asprintf(&str, "%s(%s)", op, right) < 0)
+ str = NULL;
break;

default:
@@ -2011,7 +2015,8 @@ static char *val_to_str(struct tep_event_filter *filter, struct tep_filter_arg *
{
char *str = NULL;

- asprintf(&str, "%lld", arg->value.val);
+ if (asprintf(&str, "%lld", arg->value.val) < 0)
+ str = NULL;

return str;
}
@@ -2069,7 +2074,8 @@ static char *exp_to_str(struct tep_event_filter *filter, struct tep_filter_arg *
break;
}

- asprintf(&str, "%s %s %s", lstr, op, rstr);
+ if (asprintf(&str, "%s %s %s", lstr, op, rstr) < 0)
+ str = NULL;
out:
free(lstr);
free(rstr);
@@ -2113,7 +2119,8 @@ static char *num_to_str(struct tep_event_filter *filter, struct tep_filter_arg *
if (!op)
op = "<=";

- asprintf(&str, "%s %s %s", lstr, op, rstr);
+ if (asprintf(&str, "%s %s %s", lstr, op, rstr) < 0)
+ str = NULL;
break;

default:
@@ -2148,8 +2155,9 @@ static char *str_to_str(struct tep_event_filter *filter, struct tep_filter_arg *
if (!op)
op = "!~";

- asprintf(&str, "%s %s \"%s\"",
- arg->str.field->name, op, arg->str.val);
+ if (asprintf(&str, "%s %s \"%s\"",
+ arg->str.field->name, op, arg->str.val) < 0)
+ str = NULL;
break;

default:
@@ -2165,7 +2173,8 @@ static char *arg_to_str(struct tep_event_filter *filter, struct tep_filter_arg *

switch (arg->type) {
case TEP_FILTER_ARG_BOOLEAN:
- asprintf(&str, arg->boolean.value ? "TRUE" : "FALSE");
+ if (asprintf(&str, arg->boolean.value ? "TRUE" : "FALSE") < 0)
+ str = NULL;
return str;

case TEP_FILTER_ARG_OP: