public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Namhyung Kim <namhyung@kernel.org>
To: Arnaldo Carvalho de Melo <acme@ghostprotocols.net>
Cc: Steven Rostedt <rostedt@goodmis.org>,
	Frederic Weisbecker <fweisbec@gmail.com>,
	Peter Zijlstra <a.p.zijlstra@chello.nl>,
	Ingo Molnar <mingo@kernel.org>,
	Namhyung Kim <namhyung.kim@lge.com>,
	LKML <linux-kernel@vger.kernel.org>, Jiri Olsa <jolsa@redhat.com>
Subject: Re: [PATCH 13/14] tools lib traceevent: Get rid of die() in some string conversion funcitons
Date: Fri, 13 Dec 2013 09:15:46 +0900	[thread overview]
Message-ID: <87y53phah9.fsf@sejong.aot.lge.com> (raw)
In-Reply-To: <20131212184147.GE8164@ghostprotocols.net> (Arnaldo Carvalho de Melo's message of "Thu, 12 Dec 2013 15:41:47 -0300")

Hi Arnaldo,

On Thu, 12 Dec 2013 15:41:47 -0300, Arnaldo Carvalho de Melo wrote:
> Em Thu, Dec 12, 2013 at 04:36:16PM +0900, Namhyung Kim escreveu:
>> Those functions are for stringify filter arguments.  As caller of
>> those functions handles NULL string properly, it seems that it's
>> enough to return NULL rather than calling die().
>> 
>> Signed-off-by: Namhyung Kim <namhyung@kernel.org>
>> ---
>>  tools/lib/traceevent/parse-filter.c | 58 +++++++++++++++++++++++--------------
>>  1 file changed, 36 insertions(+), 22 deletions(-)
>> 
>> diff --git a/tools/lib/traceevent/parse-filter.c b/tools/lib/traceevent/parse-filter.c
>> index 9303c55128db..32ab4396653c 100644
>> --- a/tools/lib/traceevent/parse-filter.c
>> +++ b/tools/lib/traceevent/parse-filter.c
>> @@ -1361,8 +1361,10 @@ enum pevent_errno pevent_filter_add_filter_str(struct event_filter *filter,
>>  		if (ret >= 0 && pevent->test_filters) {
>>  			char *test;
>>  			test = pevent_filter_make_string(filter, event->event->id);
>> -			printf(" '%s: %s'\n", event->event->name, test);
>> -			free(test);
>> +			if (test) {
>> +				printf(" '%s: %s'\n", event->event->name, test);
>> +				free(test);
>> +			}
>>  		}
>>  	}
>>  
>> @@ -2097,7 +2099,9 @@ static char *op_to_str(struct event_filter *filter, struct filter_arg *arg)
>>  				default:
>>  					break;
>>  				}
>> -				str = malloc_or_die(6);
>> +				str = malloc(6);
>> +				if (str == NULL)
>> +					break;
>>  				if (val)
>>  					strcpy(str, "TRUE");
>>  				else
>> @@ -2120,7 +2124,9 @@ static char *op_to_str(struct event_filter *filter, struct filter_arg *arg)
>>  		}
>>  
>>  		len = strlen(left) + strlen(right) + strlen(op) + 10;
>> -		str = malloc_or_die(len);
>> +		str = malloc(len);
>> +		if (str == NULL)
>> +			break;
>>  		snprintf(str, len, "(%s) %s (%s)",
>>  			 left, op, right);
>
> Why note:
>
> 		str = NULL;
> 		if (asprintf(&str, "(%s) %s (%s)", left, op, right);
> 			break;
>
> instead?
>
> Ditto for other places here, also there is a typo in the subject line.
>
> I've applied all the previous patches, thanks!

Thank you very much!

Here is the updated patch:


>From d775295fb53930b1e3d946e145e9a6172773d4f5 Mon Sep 17 00:00:00 2001
From: Namhyung Kim <namhyung@kernel.org>
Date: Sat, 7 Dec 2013 15:13:35 +0000
Subject: [PATCH v2.1 13/14] tools lib traceevent: Get rid of die() in some string
 conversion functions

Those functions are for stringify filter arguments.  As caller of
those functions handles NULL string properly, it seems that it's
enough to return NULL rather than calling die().

Signed-off-by: Namhyung Kim <namhyung@kernel.org>
---
 tools/lib/traceevent/parse-filter.c | 59 +++++++++++++++----------------------
 1 file changed, 24 insertions(+), 35 deletions(-)

diff --git a/tools/lib/traceevent/parse-filter.c b/tools/lib/traceevent/parse-filter.c
index 9303c55128db..72fe16562546 100644
--- a/tools/lib/traceevent/parse-filter.c
+++ b/tools/lib/traceevent/parse-filter.c
@@ -1361,8 +1361,10 @@ enum pevent_errno pevent_filter_add_filter_str(struct event_filter *filter,
 		if (ret >= 0 && pevent->test_filters) {
 			char *test;
 			test = pevent_filter_make_string(filter, event->event->id);
-			printf(" '%s: %s'\n", event->event->name, test);
-			free(test);
+			if (test) {
+				printf(" '%s: %s'\n", event->event->name, test);
+				free(test);
+			}
 		}
 	}
 
@@ -2050,7 +2052,6 @@ static char *op_to_str(struct event_filter *filter, struct filter_arg *arg)
 	int left_val = -1;
 	int right_val = -1;
 	int val;
-	int len;
 
 	switch (arg->op.type) {
 	case FILTER_OP_AND:
@@ -2097,7 +2098,9 @@ static char *op_to_str(struct event_filter *filter, struct filter_arg *arg)
 				default:
 					break;
 				}
-				str = malloc_or_die(6);
+				str = malloc(6);
+				if (str == NULL)
+					break;
 				if (val)
 					strcpy(str, "TRUE");
 				else
@@ -2119,10 +2122,7 @@ static char *op_to_str(struct event_filter *filter, struct filter_arg *arg)
 			break;
 		}
 
-		len = strlen(left) + strlen(right) + strlen(op) + 10;
-		str = malloc_or_die(len);
-		snprintf(str, len, "(%s) %s (%s)",
-			 left, op, right);
+		asprintf(&str, "(%s) %s (%s)", left, op, right);
 		break;
 
 	case FILTER_OP_NOT:
@@ -2138,16 +2138,16 @@ static char *op_to_str(struct event_filter *filter, struct filter_arg *arg)
 			right_val = 0;
 		if (right_val >= 0) {
 			/* just return the opposite */
-			str = malloc_or_die(6);
+			str = malloc(6);
+			if (str == NULL)
+				break;
 			if (right_val)
 				strcpy(str, "FALSE");
 			else
 				strcpy(str, "TRUE");
 			break;
 		}
-		len = strlen(right) + strlen(op) + 3;
-		str = malloc_or_die(len);
-		snprintf(str, len, "%s(%s)", op, right);
+		asprintf(&str, "%s(%s)", op, right);
 		break;
 
 	default:
@@ -2161,11 +2161,9 @@ static char *op_to_str(struct event_filter *filter, struct filter_arg *arg)
 
 static char *val_to_str(struct event_filter *filter, struct filter_arg *arg)
 {
-	char *str;
-
-	str = malloc_or_die(30);
+	char *str = NULL;
 
-	snprintf(str, 30, "%lld", arg->value.val);
+	asprintf(&str, "%lld", arg->value.val);
 
 	return str;
 }
@@ -2181,7 +2179,6 @@ static char *exp_to_str(struct event_filter *filter, struct filter_arg *arg)
 	char *rstr;
 	char *op;
 	char *str = NULL;
-	int len;
 
 	lstr = arg_to_str(filter, arg->exp.left);
 	rstr = arg_to_str(filter, arg->exp.right);
@@ -2220,12 +2217,11 @@ static char *exp_to_str(struct event_filter *filter, struct filter_arg *arg)
 		op = "^";
 		break;
 	default:
-		die("oops in exp");
+		op = "[ERROR IN EXPRESSION TYPE]";
+		break;
 	}
 
-	len = strlen(op) + strlen(lstr) + strlen(rstr) + 4;
-	str = malloc_or_die(len);
-	snprintf(str, len, "%s %s %s", lstr, op, rstr);
+	asprintf(&str, "%s %s %s", lstr, op, rstr);
 out:
 	free(lstr);
 	free(rstr);
@@ -2239,7 +2235,6 @@ static char *num_to_str(struct event_filter *filter, struct filter_arg *arg)
 	char *rstr;
 	char *str = NULL;
 	char *op = NULL;
-	int len;
 
 	lstr = arg_to_str(filter, arg->num.left);
 	rstr = arg_to_str(filter, arg->num.right);
@@ -2270,10 +2265,7 @@ static char *num_to_str(struct event_filter *filter, struct filter_arg *arg)
 		if (!op)
 			op = "<=";
 
-		len = strlen(lstr) + strlen(op) + strlen(rstr) + 4;
-		str = malloc_or_die(len);
-		sprintf(str, "%s %s %s", lstr, op, rstr);
-
+		asprintf(&str, "%s %s %s", lstr, op, rstr);
 		break;
 
 	default:
@@ -2291,7 +2283,6 @@ static char *str_to_str(struct event_filter *filter, struct filter_arg *arg)
 {
 	char *str = NULL;
 	char *op = NULL;
-	int len;
 
 	switch (arg->str.type) {
 	case FILTER_CMP_MATCH:
@@ -2309,12 +2300,8 @@ static char *str_to_str(struct event_filter *filter, struct filter_arg *arg)
 		if (!op)
 			op = "!~";
 
-		len = strlen(arg->str.field->name) + strlen(op) +
-			strlen(arg->str.val) + 6;
-		str = malloc_or_die(len);
-		snprintf(str, len, "%s %s \"%s\"",
-			 arg->str.field->name,
-			 op, arg->str.val);
+		asprintf(&str, "%s %s \"%s\"",
+			 arg->str.field->name, op, arg->str.val);
 		break;
 
 	default:
@@ -2330,7 +2317,9 @@ static char *arg_to_str(struct event_filter *filter, struct filter_arg *arg)
 
 	switch (arg->type) {
 	case FILTER_ARG_BOOLEAN:
-		str = malloc_or_die(6);
+		str = malloc(6);
+		if (str == NULL)
+			return NULL;
 		if (arg->boolean.value)
 			strcpy(str, "TRUE");
 		else
@@ -2369,7 +2358,7 @@ static char *arg_to_str(struct event_filter *filter, struct filter_arg *arg)
  *
  * Returns a string that displays the filter contents.
  *  This string must be freed with free(str).
- *  NULL is returned if no filter is found.
+ *  NULL is returned if no filter is found or allocation failed.
  */
 char *
 pevent_filter_make_string(struct event_filter *filter, int event_id)
-- 
1.7.11.7


  reply	other threads:[~2013-12-13  0:15 UTC|newest]

Thread overview: 37+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-12-12  7:36 [PATCHSET 00/14] tools lib traceevent: Get rid of *die() calls from parse-filter.c (v2) Namhyung Kim
2013-12-12  7:36 ` [PATCH 01/14] tools lib traceevent: Get rid of malloc_or_die() in show_error() Namhyung Kim
2013-12-16 15:27   ` [tip:perf/core] " tip-bot for Namhyung Kim
2013-12-12  7:36 ` [PATCH 02/14] tools lib traceevent: Get rid of die in add_filter_type() Namhyung Kim
2013-12-16 15:28   ` [tip:perf/core] " tip-bot for Namhyung Kim
2013-12-12  7:36 ` [PATCH 03/14] tools lib traceevent: Get rid of malloc_or_die() allocate_arg() Namhyung Kim
2013-12-16 15:28   ` [tip:perf/core] " tip-bot for Namhyung Kim
2013-12-12  7:36 ` [PATCH 04/14] tools lib traceevent: Get rid of malloc_or_die() in read_token() Namhyung Kim
2013-12-16 15:28   ` [tip:perf/core] " tip-bot for Namhyung Kim
2013-12-12  7:36 ` [PATCH 05/14] tools lib traceevent: Get rid of malloc_or_die() in find_event() Namhyung Kim
2013-12-16 15:28   ` [tip:perf/core] " tip-bot for Namhyung Kim
2013-12-12  7:36 ` [PATCH 06/14] tools lib traceevent: Get rid of die() in add_right() Namhyung Kim
2013-12-16 15:28   ` [tip:perf/core] " tip-bot for Namhyung Kim
2013-12-12  7:36 ` [PATCH 07/14] tools lib traceevent: Make add_left() return pevent_errno Namhyung Kim
2013-12-16 15:28   ` [tip:perf/core] " tip-bot for Namhyung Kim
2013-12-12  7:36 ` [PATCH 08/14] tools lib traceevent: Get rid of die() in reparent_op_arg() Namhyung Kim
2013-12-16 15:28   ` [tip:perf/core] " tip-bot for Namhyung Kim
2013-12-12  7:36 ` [PATCH 09/14] tools lib traceevent: Refactor create_arg_item() Namhyung Kim
2013-12-16 15:29   ` [tip:perf/core] " tip-bot for Namhyung Kim
2013-12-12  7:36 ` [PATCH 10/14] tools lib traceevent: Refactor process_filter() Namhyung Kim
2013-12-16 15:29   ` [tip:perf/core] " tip-bot for Namhyung Kim
2013-12-12  7:36 ` [PATCH 11/14] tools lib traceevent: Make pevent_filter_add_filter_str() return pevent_errno Namhyung Kim
2013-12-16 15:29   ` [tip:perf/core] " tip-bot for Namhyung Kim
2013-12-12  7:36 ` [PATCH 12/14] tools lib traceevent: Refactor pevent_filter_match() to get rid of die() Namhyung Kim
2013-12-16 15:29   ` [tip:perf/core] " tip-bot for Namhyung Kim
2013-12-12  7:36 ` [PATCH 13/14] tools lib traceevent: Get rid of die() in some string conversion funcitons Namhyung Kim
2013-12-12 18:41   ` Arnaldo Carvalho de Melo
2013-12-13  0:15     ` Namhyung Kim [this message]
2013-12-13 14:52       ` Arnaldo Carvalho de Melo
2013-12-16  4:49         ` Namhyung Kim
2013-12-16 12:40           ` Arnaldo Carvalho de Melo
2013-12-17  0:02             ` Namhyung Kim
2013-12-17 20:02               ` Arnaldo Carvalho de Melo
2013-12-18  4:09                 ` Namhyung Kim
2013-12-18 10:33               ` [tip:perf/core] tools lib traceevent: Get rid of die() in some string conversion functions tip-bot for Namhyung Kim
2013-12-12  7:36 ` [PATCH 14/14] tools lib traceevent: Introduce pevent_filter_strerror() Namhyung Kim
2014-01-12 18:31   ` [tip:perf/core] " tip-bot for Namhyung Kim

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87y53phah9.fsf@sejong.aot.lge.com \
    --to=namhyung@kernel.org \
    --cc=a.p.zijlstra@chello.nl \
    --cc=acme@ghostprotocols.net \
    --cc=fweisbec@gmail.com \
    --cc=jolsa@redhat.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@kernel.org \
    --cc=namhyung.kim@lge.com \
    --cc=rostedt@goodmis.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox