tools lib traceevent: Take care of return value of asprintf
authorHe Zhe <zhe.he@windriver.com>
Thu, 20 Feb 2020 01:58:50 +0000 (09:58 +0800)
committerArnaldo Carvalho de Melo <acme@redhat.com>
Sat, 18 Apr 2020 12:05:00 +0000 (09:05 -0300)
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>
tools/lib/traceevent/parse-filter.c

index 20eed71..c271aee 100644 (file)
@@ -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: