Received: by 2002:a25:1506:0:0:0:0:0 with SMTP id 6csp1721901ybv; Fri, 14 Feb 2020 04:58:42 -0800 (PST) X-Google-Smtp-Source: APXvYqyvROht00BwacuJU8HL+qvJOdzNG2n+bcD1+FdOuq0nYwFCyyY8XyzMeU2gCp2eyuRUZkBB X-Received: by 2002:a05:6830:1d7a:: with SMTP id l26mr2106572oti.138.1581685122301; Fri, 14 Feb 2020 04:58:42 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1581685122; cv=none; d=google.com; s=arc-20160816; b=oadPmFQXXVzPNybqHxnw0ZHb75iEsJ6H9/1ZnkCUdZwJujTkkVn4tkF/aR5ob4iS0z dTmlNaipgiLPs/CBZAw1maarUQ4EcXr4qoqoNBvYtJpbnJaZBxWR7oBaKaGWQ6RrTBbN IqpN1rCI8P9HKqFl+51apw76ed67KMme26HIV9idCDGrWIxTiHkeLq1pJNU0AZCRgS+A onB7py7AS3p+H9t4RuEBGbhrJQmky7Fx17n86ER3Pmbcs2JcC+Y3nW8t8kKAr6iqISMw tOKGkPDDca5aag8Y7SVVHn5vJGiy3RFhEhN4hbI+kjRsGe2/PRPibwcDkD4Q+IaCueg3 j9Dw== 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:user-agent :in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:dkim-signature; bh=zCLwBU2AIrLiq97oVJcKrk3eBDD/n+Xy8GshU+OVGfo=; b=uV68bIeEaXaRR7hVJ7bo0UL8+h0b77oeGm8uYeY77LozJzhTPQbQdd3gUz9zu4l6d6 UkwqF78YgfoS2Heb2/VBcG84xxqEFDLD+9XMRDsKwyYNnmfLRhAbD77HE3sfyH4Po0VR nIP1EOzbTsl8p2Yy20tkZ71pH9eb9YlJlTZ4/FqlMHvLGjWYNvhWoBszNqtpQ/X6Blo4 l8tVzaOv+vRmbQfYlUYwKB+lUV8FM4HZEqzOGWprvBPqRrR5au5kcQZ3CGIz3GsZNkpX CZFdqXU/MdtAAV+6Ugj/U+HbvpjZufNHF1SwtvQjS6SiIB4tjf1oTwdiFhkLlgIudQjx mExw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=gB2ufWCG; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id p19si2953978otk.251.2020.02.14.04.58.29; Fri, 14 Feb 2020 04:58:42 -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; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=gB2ufWCG; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728452AbgBNM6E (ORCPT + 99 others); Fri, 14 Feb 2020 07:58:04 -0500 Received: from us-smtp-delivery-1.mimecast.com ([207.211.31.120]:31756 "EHLO us-smtp-1.mimecast.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1726191AbgBNM6E (ORCPT ); Fri, 14 Feb 2020 07:58:04 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1581685082; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=zCLwBU2AIrLiq97oVJcKrk3eBDD/n+Xy8GshU+OVGfo=; b=gB2ufWCGECpxOKgQUPDR6soK8q+RN6hhtb0XHMPWeu9CdOXzwOuJ6/EKOcSGFL2qsww+L3 oKEr0ZETvyJMiuj4wjp3G4oT9zQtpf6H6a3Ai9PmR427RihGu7M3O7jj+kr76+W5nNLeYb RDdYn/cM94Pj9y/thHFu5UkkLOovHOI= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-289-zSDu1ixMO2OxRTz8M6LmzQ-1; Fri, 14 Feb 2020 07:57:58 -0500 X-MC-Unique: zSDu1ixMO2OxRTz8M6LmzQ-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 6160D13F8; Fri, 14 Feb 2020 12:57:57 +0000 (UTC) Received: from sandy.ghostprotocols.net (ovpn-112-10.phx2.redhat.com [10.3.112.10]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 7C5B45DA7D; Fri, 14 Feb 2020 12:57:56 +0000 (UTC) Received: by sandy.ghostprotocols.net (Postfix, from userid 1000) id 970F411E8; Fri, 14 Feb 2020 09:57:54 -0300 (BRT) Date: Fri, 14 Feb 2020 09:57:54 -0300 From: Arnaldo Carvalho de Melo To: zhe.he@windriver.com Cc: rostedt@goodmis.org, tstoyanov@vmware.com, hewenliang4@huawei.com, linux-kernel@vger.kernel.org Subject: Re: [PATCH] tools lib traceevent: Take care of return value of asprintf Message-ID: <20200214125754.GA2974@redhat.com> References: <1581665486-20386-1-git-send-email-zhe.he@windriver.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: <1581665486-20386-1-git-send-email-zhe.he@windriver.com> X-Url: http://acmel.wordpress.com User-Agent: Mutt/1.5.20 (2009-12-10) X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 Content-Transfer-Encoding: quoted-printable Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Em Fri, Feb 14, 2020 at 03:31:26PM +0800, zhe.he@windriver.com escreveu: > From: He Zhe >=20 > According to the API, if memory allocation wasn't possible, or some oth= er > error occurs, asprintf will return -1, and the contents of strp below a= re > undefined. >=20 > int asprintf(char **strp, const char *fmt, ...); >=20 > This patch takes care of return value of asprintf to make it less error > prone and prevent the following build warning. >=20 > ignoring return value of =E2=80=98asprintf=E2=80=99, declared with attr= ibute warn_unused_result [-Wunused-result] Sure this fixes problems, but can you make it more compact, i.e. no need to add most if not all those 'int ret;' lines, just check the asprintf result directly, i.e.: if (asprintf(&str, val ? "TRUE" : "FALSE") <= 0) str =3D NULL; saving the return value for that function is interesting when you need to know how many bytes it printed to do some extra calculation, but if all you need is to know if it failed, checking against < 0 is enough, no? - Arnaldo =20 > Signed-off-by: He Zhe > --- > tools/lib/traceevent/parse-filter.c | 42 +++++++++++++++++++++++++++++= -------- > 1 file changed, 33 insertions(+), 9 deletions(-) >=20 > diff --git a/tools/lib/traceevent/parse-filter.c b/tools/lib/traceevent= /parse-filter.c > index 20eed71..279b572 100644 > --- a/tools/lib/traceevent/parse-filter.c > +++ b/tools/lib/traceevent/parse-filter.c > @@ -1912,6 +1912,7 @@ static char *op_to_str(struct tep_event_filter *f= ilter, struct tep_filter_arg *a > int left_val =3D -1; > int right_val =3D -1; > int val; > + int ret; > =20 > switch (arg->op.type) { > case TEP_FILTER_OP_AND: > @@ -1958,7 +1959,9 @@ static char *op_to_str(struct tep_event_filter *f= ilter, struct tep_filter_arg *a > default: > break; > } > - asprintf(&str, val ? "TRUE" : "FALSE"); > + ret =3D asprintf(&str, val ? "TRUE" : "FALSE"); > + if (ret < 0) > + str =3D NULL; > break; > } > } > @@ -1976,7 +1979,9 @@ static char *op_to_str(struct tep_event_filter *f= ilter, struct tep_filter_arg *a > break; > } > =20 > - asprintf(&str, "(%s) %s (%s)", left, op, right); > + ret =3D asprintf(&str, "(%s) %s (%s)", left, op, right); > + if (ret < 0) > + str =3D NULL; > break; > =20 > case TEP_FILTER_OP_NOT: > @@ -1992,10 +1997,14 @@ static char *op_to_str(struct tep_event_filter = *filter, struct tep_filter_arg *a > right_val =3D 0; > if (right_val >=3D 0) { > /* just return the opposite */ > - asprintf(&str, right_val ? "FALSE" : "TRUE"); > + ret =3D asprintf(&str, right_val ? "FALSE" : "TRUE"); > + if (ret < 0) > + str =3D NULL; > break; > } > - asprintf(&str, "%s(%s)", op, right); > + ret =3D asprintf(&str, "%s(%s)", op, right); > + if (ret < 0) > + str =3D NULL; > break; > =20 > default: > @@ -2010,8 +2019,11 @@ static char *op_to_str(struct tep_event_filter *= filter, struct tep_filter_arg *a > static char *val_to_str(struct tep_event_filter *filter, struct tep_fi= lter_arg *arg) > { > char *str =3D NULL; > + int ret; > =20 > - asprintf(&str, "%lld", arg->value.val); > + ret =3D asprintf(&str, "%lld", arg->value.val); > + if (ret < 0) > + str =3D NULL; > =20 > return str; > } > @@ -2027,6 +2039,7 @@ static char *exp_to_str(struct tep_event_filter *= filter, struct tep_filter_arg * > char *rstr; > char *op; > char *str =3D NULL; > + int ret; > =20 > lstr =3D arg_to_str(filter, arg->exp.left); > rstr =3D arg_to_str(filter, arg->exp.right); > @@ -2069,7 +2082,9 @@ static char *exp_to_str(struct tep_event_filter *= filter, struct tep_filter_arg * > break; > } > =20 > - asprintf(&str, "%s %s %s", lstr, op, rstr); > + ret =3D asprintf(&str, "%s %s %s", lstr, op, rstr); > + if (ret < 0) > + str =3D NULL; > out: > free(lstr); > free(rstr); > @@ -2083,6 +2098,7 @@ static char *num_to_str(struct tep_event_filter *= filter, struct tep_filter_arg * > char *rstr; > char *str =3D NULL; > char *op =3D NULL; > + int ret; > =20 > lstr =3D arg_to_str(filter, arg->num.left); > rstr =3D arg_to_str(filter, arg->num.right); > @@ -2113,7 +2129,9 @@ static char *num_to_str(struct tep_event_filter *= filter, struct tep_filter_arg * > if (!op) > op =3D "<=3D"; > =20 > - asprintf(&str, "%s %s %s", lstr, op, rstr); > + ret =3D asprintf(&str, "%s %s %s", lstr, op, rstr); > + if (ret < 0) > + str =3D NULL; > break; > =20 > default: > @@ -2131,6 +2149,7 @@ static char *str_to_str(struct tep_event_filter *= filter, struct tep_filter_arg * > { > char *str =3D NULL; > char *op =3D NULL; > + int ret; > =20 > switch (arg->str.type) { > case TEP_FILTER_CMP_MATCH: > @@ -2148,8 +2167,10 @@ static char *str_to_str(struct tep_event_filter = *filter, struct tep_filter_arg * > if (!op) > op =3D "!~"; > =20 > - asprintf(&str, "%s %s \"%s\"", > + ret =3D asprintf(&str, "%s %s \"%s\"", > arg->str.field->name, op, arg->str.val); > + if (ret < 0) > + str =3D NULL; > break; > =20 > default: > @@ -2162,10 +2183,13 @@ static char *str_to_str(struct tep_event_filter= *filter, struct tep_filter_arg * > static char *arg_to_str(struct tep_event_filter *filter, struct tep_fi= lter_arg *arg) > { > char *str =3D NULL; > + int ret; > =20 > switch (arg->type) { > case TEP_FILTER_ARG_BOOLEAN: > - asprintf(&str, arg->boolean.value ? "TRUE" : "FALSE"); > + ret =3D asprintf(&str, arg->boolean.value ? "TRUE" : "FALSE"); > + if (ret < 0) > + str =3D NULL; > return str; > =20 > case TEP_FILTER_ARG_OP: > --=20 > 2.7.4