Re: [PATCH v2] tools lib traceevent: Take care of return value of asprintf

From: Steven Rostedt
Date: Wed Feb 19 2020 - 14:52:59 EST


On Fri, 14 Feb 2020 21:21:21 +0800
<zhe.he@xxxxxxxxxxxxx> wrote:

> From: He Zhe <zhe.he@xxxxxxxxxxxxx>
>
> 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@xxxxxxxxxxxxx>
> ---
> v2: directly check the return value without saving to a variable
>
> tools/lib/traceevent/parse-filter.c | 35 +++++++++++++++++++++--------------
> 1 file changed, 21 insertions(+), 14 deletions(-)
>
> diff --git a/tools/lib/traceevent/parse-filter.c b/tools/lib/traceevent/parse-filter.c
> index 20eed71..6cd0228 100644
> --- a/tools/lib/traceevent/parse-filter.c
> +++ b/tools/lib/traceevent/parse-filter.c
> @@ -274,8 +274,7 @@ find_event(struct tep_handle *tep, struct event_list **events,
> sys_name = NULL;
> }
>
> - ret = asprintf(&reg, "^%s$", event_name);
> - if (ret < 0)
> + if (asprintf(&reg, "^%s$", event_name) < 0)

I know Arnaldo said you don't need to save the return value for the
check, but let's just modify the asprintf() that needs checking, and
not remove those that already save it.

Thanks!

-- Steve