Received: by 2002:a25:1506:0:0:0:0:0 with SMTP id 6csp137825ybv; Wed, 19 Feb 2020 18:01:27 -0800 (PST) X-Google-Smtp-Source: APXvYqzXGGY/+5wXiCAQ4o1ZFL3I447WRpL2hWmShaPTvtCWkvC6SDFE9b1uaPC+VvA2lVSz5d4O X-Received: by 2002:a9d:7cd9:: with SMTP id r25mr20544932otn.326.1582164087415; Wed, 19 Feb 2020 18:01:27 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1582164087; cv=none; d=google.com; s=arc-20160816; b=l2ZflLR6qmiMzeRVBungh+ALEb+Lq0HxZAA4Yd10/OWjia7sOTVx9zqHeMYkk3wYTz sUOfNXqvwuDf00Txq4eK1OZEv6UchA3Uf5BUT0xD14Eh8oO1p5jSRvdfIX8L/+juM4sH 8ouOGnObCw6LhqJJlBL1cvFvDyRvrXkfAmoieqOjz2rPXkpGXGJIX7l1WiUo60n9+zSh g7FX/NqAThrt6n4QBeusxhY6z1vBSkgKWjbQmBRGGifKiGGktNDSqqOTQzmil2qsE1NS 4sm5jTl4MwJIq+gQcW5qLMNIz2BeeBl/ksdYukWRzH9mK0FUYVyC0jHFINFvPjZ0yQoQ 7LpQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :message-id:date:subject:to:from; bh=U4e44rtDYNieVtFWg+D30jRya4A1BfHB9oXyUIFlpzQ=; b=piigG8TZeydmuqJ57dP5SA20Mh1oPsth3ag43+jdrirdtW/9Jz1dJZt9D4dNGPcDbT 5jd73TXy/LHvbKrdcKytkcCriCpDCB0ZjdwUESao5yyc6RiG/qlA4guWtY8OBBDlg+EQ /vnh1AGK5CB54TW2bcALA2AuB7CFTxzujhI+7fvPs7q+cpe/2PBS0wfr1UZtWgRgXkMy g6bQWh6pJbmmm0/HE5TDavIqQ6A7703rur04g6o7n8nSc/C+d6pvhJKtapNL5MqXt52z V6EWx77t88iZOiUPjj3A0vNvfCEwrjWzr+ZbZTgY5jjx2tmyMUr4U2CHMZjQagSG3k+0 UODQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id n186si10141106oig.191.2020.02.19.18.01.15; Wed, 19 Feb 2020 18:01:27 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727648AbgBTCBD (ORCPT + 99 others); Wed, 19 Feb 2020 21:01:03 -0500 Received: from mail5.windriver.com ([192.103.53.11]:36858 "EHLO mail5.wrs.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727370AbgBTCBD (ORCPT ); Wed, 19 Feb 2020 21:01:03 -0500 Received: from ALA-HCA.corp.ad.wrs.com (ala-hca.corp.ad.wrs.com [147.11.189.40]) by mail5.wrs.com (8.15.2/8.15.2) with ESMTPS id 01K1x2kD023823 (version=TLSv1 cipher=AES256-SHA bits=256 verify=FAIL); Wed, 19 Feb 2020 17:59:13 -0800 Received: from pek-lpg-core2.corp.ad.wrs.com (128.224.153.41) by ALA-HCA.corp.ad.wrs.com (147.11.189.40) with Microsoft SMTP Server id 14.3.468.0; Wed, 19 Feb 2020 17:58:52 -0800 From: To: , , , , , Subject: [PATCH v3] tools lib traceevent: Take care of return value of asprintf Date: Thu, 20 Feb 2020 09:58:50 +0800 Message-ID: <1582163930-233692-1-git-send-email-zhe.he@windriver.com> X-Mailer: git-send-email 2.7.4 MIME-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: He Zhe 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 --- 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