diff options
author | Namhyung Kim <namhyung.kim@lge.com> | 2012-04-08 22:54:30 -0400 |
---|---|---|
committer | Namhyung Kim <namhyung@kernel.org> | 2012-07-04 00:40:31 -0400 |
commit | deba3fb26fd1ed3235b00dccced9784a7f76ec3c (patch) | |
tree | 03708b0edc3f380e316cb130d822d377032e9d69 /tools/lib | |
parent | 0fc45ef5202a34b5862ca246740e6ab50bc3e3e1 (diff) |
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>
Diffstat (limited to 'tools/lib')
-rw-r--r-- | tools/lib/traceevent/event-parse.c | 50 |
1 files changed, 24 insertions, 26 deletions
diff --git a/tools/lib/traceevent/event-parse.c b/tools/lib/traceevent/event-parse.c index 7815b8d2eabd..768fab5fbcb7 100644 --- a/tools/lib/traceevent/event-parse.c +++ b/tools/lib/traceevent/event-parse.c | |||
@@ -781,6 +781,25 @@ int pevent_peek_char(void) | |||
781 | return __peek_char(); | 781 | return __peek_char(); |
782 | } | 782 | } |
783 | 783 | ||
784 | static int extend_token(char **tok, char *buf, int size) | ||
785 | { | ||
786 | char *newtok = realloc(*tok, size); | ||
787 | |||
788 | if (!newtok) { | ||
789 | free(*tok); | ||
790 | *tok = NULL; | ||
791 | return -1; | ||
792 | } | ||
793 | |||
794 | if (!*tok) | ||
795 | strcpy(newtok, buf); | ||
796 | else | ||
797 | strcat(newtok, buf); | ||
798 | *tok = newtok; | ||
799 | |||
800 | return 0; | ||
801 | } | ||
802 | |||
784 | static enum event_type force_token(const char *str, char **tok); | 803 | static enum event_type force_token(const char *str, char **tok); |
785 | 804 | ||
786 | static enum event_type __read_token(char **tok) | 805 | static enum event_type __read_token(char **tok) |
@@ -865,17 +884,10 @@ static enum event_type __read_token(char **tok) | |||
865 | do { | 884 | do { |
866 | if (i == (BUFSIZ - 1)) { | 885 | if (i == (BUFSIZ - 1)) { |
867 | buf[i] = 0; | 886 | buf[i] = 0; |
868 | if (*tok) { | 887 | tok_size += BUFSIZ; |
869 | *tok = realloc(*tok, tok_size + BUFSIZ); | ||
870 | if (!*tok) | ||
871 | return EVENT_NONE; | ||
872 | strcat(*tok, buf); | ||
873 | } else | ||
874 | *tok = strdup(buf); | ||
875 | 888 | ||
876 | if (!*tok) | 889 | if (extend_token(tok, buf, tok_size) < 0) |
877 | return EVENT_NONE; | 890 | return EVENT_NONE; |
878 | tok_size += BUFSIZ; | ||
879 | i = 0; | 891 | i = 0; |
880 | } | 892 | } |
881 | last_ch = ch; | 893 | last_ch = ch; |
@@ -914,17 +926,10 @@ static enum event_type __read_token(char **tok) | |||
914 | while (get_type(__peek_char()) == type) { | 926 | while (get_type(__peek_char()) == type) { |
915 | if (i == (BUFSIZ - 1)) { | 927 | if (i == (BUFSIZ - 1)) { |
916 | buf[i] = 0; | 928 | buf[i] = 0; |
917 | if (*tok) { | 929 | tok_size += BUFSIZ; |
918 | *tok = realloc(*tok, tok_size + BUFSIZ); | ||
919 | if (!*tok) | ||
920 | return EVENT_NONE; | ||
921 | strcat(*tok, buf); | ||
922 | } else | ||
923 | *tok = strdup(buf); | ||
924 | 930 | ||
925 | if (!*tok) | 931 | if (extend_token(tok, buf, tok_size) < 0) |
926 | return EVENT_NONE; | 932 | return EVENT_NONE; |
927 | tok_size += BUFSIZ; | ||
928 | i = 0; | 933 | i = 0; |
929 | } | 934 | } |
930 | ch = __read_char(); | 935 | ch = __read_char(); |
@@ -933,14 +938,7 @@ static enum event_type __read_token(char **tok) | |||
933 | 938 | ||
934 | out: | 939 | out: |
935 | buf[i] = 0; | 940 | buf[i] = 0; |
936 | if (*tok) { | 941 | if (extend_token(tok, buf, tok_size + i + 1) < 0) |
937 | *tok = realloc(*tok, tok_size + i); | ||
938 | if (!*tok) | ||
939 | return EVENT_NONE; | ||
940 | strcat(*tok, buf); | ||
941 | } else | ||
942 | *tok = strdup(buf); | ||
943 | if (!*tok) | ||
944 | return EVENT_NONE; | 942 | return EVENT_NONE; |
945 | 943 | ||
946 | if (type == EVENT_ITEM) { | 944 | if (type == EVENT_ITEM) { |