linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Arnaldo Carvalho de Melo <acme@infradead.org>
To: Ingo Molnar <mingo@kernel.org>
Cc: linux-kernel@vger.kernel.org, Namhyung Kim <namhyung.kim@lge.com>,
	Frederic Weisbecker <fweisbec@gmail.com>,
	Arnaldo Carvalho de Melo <acme@infradead.org>,
	Borislav Petkov <bp@alien8.de>, David Ahern <dsahern@gmail.com>,
	Steven Rostedt <rostedt@goodmis.org>,
	Namhyung Kim <namhyung@kernel.org>
Subject: [PATCH 07/20] tools lib traceevent: Introduce extend_token()
Date: Thu,  5 Jul 2012 12:45:05 -0300	[thread overview]
Message-ID: <1341503118-13198-8-git-send-email-acme@infradead.org> (raw)
In-Reply-To: <1341503118-13198-1-git-send-email-acme@infradead.org>

From: Namhyung Kim <namhyung.kim@lge.com>

The __read_token() function has some duplicated code to handle
internal buffer overflow. Factor them out to new extend_token().

According to the man pages of realloc/free(3), they can handle NULL
pointer input so that it can be ended up to compact the code.  Also
handle error path correctly.

Signed-off-by: Namhyung Kim <namhyung.kim@lge.com>
Cc: Frederic Weisbecker <fweisbec@gmail.com>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: Arnaldo Carvalho de Melo <acme@infradead.org>
Cc: Borislav Petkov <bp@alien8.de>
Cc: David Ahern <dsahern@gmail.com>
Link: http://lkml.kernel.org/r/1333940074-19052-4-git-send-email-namhyung.kim@lge.com
[rostedt@goodmis.org: added some extra whitespace]
Signed-off-by: Steven Rostedt <rostedt@goodmis.org>
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
---
 tools/lib/traceevent/event-parse.c |   50 +++++++++++++++++-------------------
 1 file changed, 24 insertions(+), 26 deletions(-)

diff --git a/tools/lib/traceevent/event-parse.c b/tools/lib/traceevent/event-parse.c
index 7815b8d..768fab5 100644
--- a/tools/lib/traceevent/event-parse.c
+++ b/tools/lib/traceevent/event-parse.c
@@ -781,6 +781,25 @@ int pevent_peek_char(void)
 	return __peek_char();
 }
 
+static int extend_token(char **tok, char *buf, int size)
+{
+	char *newtok = realloc(*tok, size);
+
+	if (!newtok) {
+		free(*tok);
+		*tok = NULL;
+		return -1;
+	}
+
+	if (!*tok)
+		strcpy(newtok, buf);
+	else
+		strcat(newtok, buf);
+	*tok = newtok;
+
+	return 0;
+}
+
 static enum event_type force_token(const char *str, char **tok);
 
 static enum event_type __read_token(char **tok)
@@ -865,17 +884,10 @@ static enum event_type __read_token(char **tok)
 		do {
 			if (i == (BUFSIZ - 1)) {
 				buf[i] = 0;
-				if (*tok) {
-					*tok = realloc(*tok, tok_size + BUFSIZ);
-					if (!*tok)
-						return EVENT_NONE;
-					strcat(*tok, buf);
-				} else
-					*tok = strdup(buf);
+				tok_size += BUFSIZ;
 
-				if (!*tok)
+				if (extend_token(tok, buf, tok_size) < 0)
 					return EVENT_NONE;
-				tok_size += BUFSIZ;
 				i = 0;
 			}
 			last_ch = ch;
@@ -914,17 +926,10 @@ static enum event_type __read_token(char **tok)
 	while (get_type(__peek_char()) == type) {
 		if (i == (BUFSIZ - 1)) {
 			buf[i] = 0;
-			if (*tok) {
-				*tok = realloc(*tok, tok_size + BUFSIZ);
-				if (!*tok)
-					return EVENT_NONE;
-				strcat(*tok, buf);
-			} else
-				*tok = strdup(buf);
+			tok_size += BUFSIZ;
 
-			if (!*tok)
+			if (extend_token(tok, buf, tok_size) < 0)
 				return EVENT_NONE;
-			tok_size += BUFSIZ;
 			i = 0;
 		}
 		ch = __read_char();
@@ -933,14 +938,7 @@ static enum event_type __read_token(char **tok)
 
  out:
 	buf[i] = 0;
-	if (*tok) {
-		*tok = realloc(*tok, tok_size + i);
-		if (!*tok)
-			return EVENT_NONE;
-		strcat(*tok, buf);
-	} else
-		*tok = strdup(buf);
-	if (!*tok)
+	if (extend_token(tok, buf, tok_size + i + 1) < 0)
 		return EVENT_NONE;
 
 	if (type == EVENT_ITEM) {
-- 
1.7.9.2.358.g22243


  parent reply	other threads:[~2012-07-05 15:49 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-07-05 15:44 [GIT PULL 00/20] perf/core improvements and fixes Arnaldo Carvalho de Melo
2012-07-05 15:44 ` [PATCH 01/20] tools lib traceevent: Let filtering numbers by string use function names Arnaldo Carvalho de Melo
2012-07-05 15:45 ` [PATCH 02/20] tools lib traceevent: Add support for "%.*s" in bprintk events Arnaldo Carvalho de Melo
2012-07-05 15:45 ` [PATCH 03/20] tools lib traceevent: Add support to show migrate disable counter Arnaldo Carvalho de Melo
2012-07-05 15:45 ` [PATCH 04/20] tools lib traceevent: Fix %pM print format arg handling Arnaldo Carvalho de Melo
2012-07-05 15:45 ` [PATCH 05/20] tools lib traceevent: Fix trace_printk for long integers Arnaldo Carvalho de Melo
2012-07-05 15:45 ` [PATCH 06/20] tools lib traceevent: Fix printk_cmp() Arnaldo Carvalho de Melo
2012-07-05 15:45 ` Arnaldo Carvalho de Melo [this message]
2012-07-05 15:45 ` [PATCH 08/20] tools lib traceevent: Handle strdup failure cases Arnaldo Carvalho de Melo
2012-07-05 15:45 ` [PATCH 09/20] tools lib traceevent: Handle realloc() failure path Arnaldo Carvalho de Melo
2012-07-05 15:45 ` [PATCH 10/20] tools lib traceevent: Pass string type argument to args Arnaldo Carvalho de Melo
2012-07-05 15:45 ` [PATCH 11/20] tools lib traceevent: Do not call add_event() again if allocation failed Arnaldo Carvalho de Melo
2012-07-05 15:45 ` [PATCH 12/20] tools lib traceevent: Fix some comments Arnaldo Carvalho de Melo
2012-07-05 15:45 ` [PATCH 13/20] tools lib traceevent: Check result of malloc() during reading token Arnaldo Carvalho de Melo
2012-07-05 15:45 ` [PATCH 14/20] tools lib traceevent: Check return value of arg_to_str() Arnaldo Carvalho de Melo
2012-07-05 15:45 ` [PATCH 15/20] tools lib traceevent: Add missing break in make_bprint_args Arnaldo Carvalho de Melo
2012-07-05 15:45 ` [PATCH 16/20] tools lib traceevent: Cleanup realloc use Arnaldo Carvalho de Melo
2012-07-05 15:45 ` [PATCH 17/20] perf test: Use ARRAY_SIZE in parse events tests Arnaldo Carvalho de Melo
2012-07-05 15:45 ` [PATCH 18/20] perf tools: Add empty rule for new line in event syntax parsing Arnaldo Carvalho de Melo
2012-07-05 15:45 ` [PATCH 19/20] perf tools: Split out PE_VALUE_SYM parsing token to SW and HW tokens Arnaldo Carvalho de Melo
2012-07-05 15:45 ` [PATCH 20/20] perf tools: Split event symbols arrays to hw and sw parts Arnaldo Carvalho de Melo
2012-07-06  8:22 ` [GIT PULL 00/20] perf/core improvements and fixes Ingo Molnar

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=1341503118-13198-8-git-send-email-acme@infradead.org \
    --to=acme@infradead.org \
    --cc=bp@alien8.de \
    --cc=dsahern@gmail.com \
    --cc=fweisbec@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@kernel.org \
    --cc=namhyung.kim@lge.com \
    --cc=namhyung@kernel.org \
    --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;
as well as URLs for NNTP newsgroup(s).