From: Arnaldo Carvalho de Melo <acme@redhat.com>
To: Steven Rostedt <rostedt@goodmis.org>, He Zhe <zhe.he@windriver.com>
Cc: tstoyanov@vmware.com, hewenliang4@huawei.com,
linux-kernel@vger.kernel.org, acme@kernel.org
Subject: Re: [PATCH v3] tools lib traceevent: Take care of return value of asprintf
Date: Wed, 25 Mar 2020 10:35:51 -0300 [thread overview]
Message-ID: <20200325133551.GA19495@redhat.com> (raw)
In-Reply-To: <5f7589c3-323d-1a5c-685c-9becd87a690b@windriver.com>
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, zhe.he@windriver.com wrote:
> > From: He Zhe <zhe.he@windriver.com>
> >
> > 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 <zhe.he@windriver.com>
> > ---
> > 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:
next prev parent reply other threads:[~2020-03-25 13:36 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-02-20 1:58 [PATCH v3] tools lib traceevent: Take care of return value of asprintf zhe.he
2020-03-25 13:05 ` He Zhe
2020-03-25 13:24 ` Steven Rostedt
2020-03-25 13:35 ` Arnaldo Carvalho de Melo [this message]
2020-04-07 2:24 ` Steven Rostedt
2020-04-07 12:33 ` Arnaldo Carvalho de Melo
2020-04-16 16:08 ` Arnaldo Carvalho de Melo
2020-04-22 12:17 ` [tip: perf/core] " tip-bot2 for He Zhe
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20200325133551.GA19495@redhat.com \
--to=acme@redhat.com \
--cc=acme@kernel.org \
--cc=hewenliang4@huawei.com \
--cc=linux-kernel@vger.kernel.org \
--cc=rostedt@goodmis.org \
--cc=tstoyanov@vmware.com \
--cc=zhe.he@windriver.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox