Skip to content

Commit

Permalink
tools lib traceevent: Take care of return value of asprintf
Browse files Browse the repository at this point in the history
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>
Reviewed-by: Steven Rostedt (VMware) <rostedt@goodmis.org>
Cc: Tzvetomir Stoyanov <tstoyanov@vmware.com>
Cc: hewenliang4@huawei.com
Link: http://lore.kernel.org/lkml/1582163930-233692-1-git-send-email-zhe.he@windriver.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
  • Loading branch information
He Zhe authored and Arnaldo Carvalho de Melo committed Apr 18, 2020
1 parent bec49a9 commit f8ff18b
Showing 1 changed file with 19 additions and 10 deletions.
29 changes: 19 additions & 10 deletions tools/lib/traceevent/parse-filter.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -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;
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand All @@ -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:
Expand Down

0 comments on commit f8ff18b

Please sign in to comment.