All of lore.kernel.org
 help / color / mirror / Atom feed
From: tip-bot for Namhyung Kim <namhyung.kim@lge.com>
To: linux-tip-commits@vger.kernel.org
Cc: linux-kernel@vger.kernel.org, hpa@zytor.com, mingo@kernel.org,
	namhyung.kim@lge.com, namhyung@kernel.org, bp@alien8.de,
	fweisbec@gmail.com, rostedt@goodmis.org, acme@infradead.org,
	dsahern@gmail.com, tglx@linutronix.de
Subject: [tip:perf/core] tools lib traceevent: Introduce extend_token()
Date: Fri, 6 Jul 2012 04:13:40 -0700	[thread overview]
Message-ID: <tip-deba3fb26fd1ed3235b00dccced9784a7f76ec3c@git.kernel.org> (raw)
In-Reply-To: <1333940074-19052-4-git-send-email-namhyung.kim@lge.com>

Commit-ID:  deba3fb26fd1ed3235b00dccced9784a7f76ec3c
Gitweb:     http://git.kernel.org/tip/deba3fb26fd1ed3235b00dccced9784a7f76ec3c
Author:     Namhyung Kim <namhyung.kim@lge.com>
AuthorDate: Mon, 9 Apr 2012 11:54:30 +0900
Committer:  Namhyung Kim <namhyung@kernel.org>
CommitDate: Wed, 4 Jul 2012 13:40:31 +0900

tools lib traceevent: Introduce extend_token()

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 |   52 +++++++++++++++++------------------
 1 files changed, 25 insertions(+), 27 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) {

  reply	other threads:[~2012-07-06 11:14 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-04-09  2:54 [PATCH 0/7] trace-cmd: Small fixes and cleanups on libparsevent Namhyung Kim
2012-04-09  2:54 ` [PATCH 1/7] trace-cmd: Add NO_PYTHON option for Makefile Namhyung Kim
2012-04-10 17:36   ` Steven Rostedt
2012-04-09  2:54 ` [PATCH 2/7] parse-events: Fix printk_cmp() Namhyung Kim
2012-04-10 17:36   ` Steven Rostedt
2012-07-06 11:12   ` [tip:perf/core] tools lib traceevent: " tip-bot for Namhyung Kim
2012-04-09  2:54 ` [PATCH 3/7] parse-events: Introduce extend_token() Namhyung Kim
2012-07-06 11:13   ` tip-bot for Namhyung Kim [this message]
2012-04-09  2:54 ` [PATCH 4/7] parse-events: Handle strdup failure cases Namhyung Kim
2012-04-23 15:51   ` Steven Rostedt
2012-04-24  0:35     ` Namhyung Kim
2012-04-24  0:41       ` Steven Rostedt
2012-07-06 11:14   ` [tip:perf/core] tools lib traceevent: " tip-bot for Namhyung Kim
2012-04-09  2:54 ` [PATCH 5/7] parse-events: Fix a possible memory leak Namhyung Kim
2012-04-09  2:54 ` [PATCH 6/7] parse-events: Handle realloc() failure path Namhyung Kim
2012-07-06 11:15   ` [tip:perf/core] tools lib traceevent: " tip-bot for Namhyung Kim
2012-04-09  2:54 ` [PATCH 7/7] parse-events: Fix a possibly wrong memory dereference Namhyung Kim
2012-04-09 17:22 ` [PATCH 0/7] trace-cmd: Small fixes and cleanups on libparsevent Frederic Weisbecker
2012-04-10  0:53   ` 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=tip-deba3fb26fd1ed3235b00dccced9784a7f76ec3c@git.kernel.org \
    --to=namhyung.kim@lge.com \
    --cc=acme@infradead.org \
    --cc=bp@alien8.de \
    --cc=dsahern@gmail.com \
    --cc=fweisbec@gmail.com \
    --cc=hpa@zytor.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-tip-commits@vger.kernel.org \
    --cc=mingo@kernel.org \
    --cc=namhyung@kernel.org \
    --cc=rostedt@goodmis.org \
    --cc=tglx@linutronix.de \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.