Commit 27d4d336 authored by Steven Rostedt (VMware)'s avatar Steven Rostedt (VMware) Committed by Arnaldo Carvalho de Melo

tools lib traceevent: Add append() function helper for appending strings

There's several locations that open code realloc and strcat() to append
text to strings. Add an append() function that takes a delimiter and a
string to append to another string.
Signed-off-by: default avatarSteven Rostedt (VMware) <rostedt@goodmis.org>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Jaewon Lim <jaewon31.kim@samsung.com>
Cc: Jiri Olsa <jolsa@redhat.com>
Cc: Kees Kook <keescook@chromium.org>
Cc: linux-mm@kvack.org
Cc: linux-trace-devel@vger.kernel.org
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Vlastimil Babka <vbabka@suse.cz>
Link: http://lore.kernel.org/lkml/20200324200956.515118403@goodmis.orgSigned-off-by: default avatarArnaldo Carvalho de Melo <acme@redhat.com>
parent 0e093c77
...@@ -1425,6 +1425,19 @@ static unsigned int type_size(const char *name) ...@@ -1425,6 +1425,19 @@ static unsigned int type_size(const char *name)
return 0; return 0;
} }
static int append(char **buf, const char *delim, const char *str)
{
char *new_buf;
new_buf = realloc(*buf, strlen(*buf) + strlen(delim) + strlen(str) + 1);
if (!new_buf)
return -1;
strcat(new_buf, delim);
strcat(new_buf, str);
*buf = new_buf;
return 0;
}
static int event_read_fields(struct tep_event *event, struct tep_format_field **fields) static int event_read_fields(struct tep_event *event, struct tep_format_field **fields)
{ {
struct tep_format_field *field = NULL; struct tep_format_field *field = NULL;
...@@ -1432,6 +1445,7 @@ static int event_read_fields(struct tep_event *event, struct tep_format_field ** ...@@ -1432,6 +1445,7 @@ static int event_read_fields(struct tep_event *event, struct tep_format_field **
char *token; char *token;
char *last_token; char *last_token;
int count = 0; int count = 0;
int ret;
do { do {
unsigned int size_dynamic = 0; unsigned int size_dynamic = 0;
...@@ -1490,24 +1504,15 @@ static int event_read_fields(struct tep_event *event, struct tep_format_field ** ...@@ -1490,24 +1504,15 @@ static int event_read_fields(struct tep_event *event, struct tep_format_field **
field->flags |= TEP_FIELD_IS_POINTER; field->flags |= TEP_FIELD_IS_POINTER;
if (field->type) { if (field->type) {
char *new_type; ret = append(&field->type, " ", last_token);
new_type = realloc(field->type,
strlen(field->type) +
strlen(last_token) + 2);
if (!new_type) {
free(last_token); free(last_token);
if (ret < 0)
goto fail; goto fail;
}
field->type = new_type;
strcat(field->type, " ");
strcat(field->type, last_token);
free(last_token);
} else } else
field->type = last_token; field->type = last_token;
last_token = token; last_token = token;
continue; continue;
} }
break; break;
} }
...@@ -1523,8 +1528,6 @@ static int event_read_fields(struct tep_event *event, struct tep_format_field ** ...@@ -1523,8 +1528,6 @@ static int event_read_fields(struct tep_event *event, struct tep_format_field **
if (strcmp(token, "[") == 0) { if (strcmp(token, "[") == 0) {
enum tep_event_type last_type = type; enum tep_event_type last_type = type;
char *brackets = token; char *brackets = token;
char *new_brackets;
int len;
field->flags |= TEP_FIELD_IS_ARRAY; field->flags |= TEP_FIELD_IS_ARRAY;
...@@ -1536,29 +1539,27 @@ static int event_read_fields(struct tep_event *event, struct tep_format_field ** ...@@ -1536,29 +1539,27 @@ static int event_read_fields(struct tep_event *event, struct tep_format_field **
field->arraylen = 0; field->arraylen = 0;
while (strcmp(token, "]") != 0) { while (strcmp(token, "]") != 0) {
const char *delim;
if (last_type == TEP_EVENT_ITEM && if (last_type == TEP_EVENT_ITEM &&
type == TEP_EVENT_ITEM) type == TEP_EVENT_ITEM)
len = 2; delim = " ";
else else
len = 1; delim = "";
last_type = type; last_type = type;
new_brackets = realloc(brackets, ret = append(&brackets, delim, token);
strlen(brackets) + if (ret < 0) {
strlen(token) + len);
if (!new_brackets) {
free(brackets); free(brackets);
goto fail; goto fail;
} }
brackets = new_brackets;
if (len == 2)
strcat(brackets, " ");
strcat(brackets, token);
/* We only care about the last token */ /* We only care about the last token */
field->arraylen = strtoul(token, NULL, 0); field->arraylen = strtoul(token, NULL, 0);
free_token(token); free_token(token);
type = read_token(&token); type = read_token(&token);
if (type == TEP_EVENT_NONE) { if (type == TEP_EVENT_NONE) {
free(brackets);
do_warning_event(event, "failed to find token"); do_warning_event(event, "failed to find token");
goto fail; goto fail;
} }
...@@ -1566,13 +1567,11 @@ static int event_read_fields(struct tep_event *event, struct tep_format_field ** ...@@ -1566,13 +1567,11 @@ static int event_read_fields(struct tep_event *event, struct tep_format_field **
free_token(token); free_token(token);
new_brackets = realloc(brackets, strlen(brackets) + 2); ret = append(&brackets, "", "]");
if (!new_brackets) { if (ret < 0) {
free(brackets); free(brackets);
goto fail; goto fail;
} }
brackets = new_brackets;
strcat(brackets, "]");
/* add brackets to type */ /* add brackets to type */
...@@ -1582,34 +1581,23 @@ static int event_read_fields(struct tep_event *event, struct tep_format_field ** ...@@ -1582,34 +1581,23 @@ static int event_read_fields(struct tep_event *event, struct tep_format_field **
* the format: type [] item; * the format: type [] item;
*/ */
if (type == TEP_EVENT_ITEM) { if (type == TEP_EVENT_ITEM) {
char *new_type; ret = append(&field->type, " ", field->name);
new_type = realloc(field->type, if (ret < 0) {
strlen(field->type) +
strlen(field->name) +
strlen(brackets) + 2);
if (!new_type) {
free(brackets); free(brackets);
goto fail; goto fail;
} }
field->type = new_type; ret = append(&field->type, "", brackets);
strcat(field->type, " ");
strcat(field->type, field->name);
size_dynamic = type_size(field->name); size_dynamic = type_size(field->name);
free_token(field->name); free_token(field->name);
strcat(field->type, brackets);
field->name = field->alias = token; field->name = field->alias = token;
type = read_token(&token); type = read_token(&token);
} else { } else {
char *new_type; ret = append(&field->type, "", brackets);
new_type = realloc(field->type, if (ret < 0) {
strlen(field->type) +
strlen(brackets) + 1);
if (!new_type) {
free(brackets); free(brackets);
goto fail; goto fail;
} }
field->type = new_type;
strcat(field->type, brackets);
} }
free(brackets); free(brackets);
} }
...@@ -2046,19 +2034,16 @@ process_op(struct tep_event *event, struct tep_print_arg *arg, char **tok) ...@@ -2046,19 +2034,16 @@ process_op(struct tep_event *event, struct tep_print_arg *arg, char **tok)
/* could just be a type pointer */ /* could just be a type pointer */
if ((strcmp(arg->op.op, "*") == 0) && if ((strcmp(arg->op.op, "*") == 0) &&
type == TEP_EVENT_DELIM && (strcmp(token, ")") == 0)) { type == TEP_EVENT_DELIM && (strcmp(token, ")") == 0)) {
char *new_atom; int ret;
if (left->type != TEP_PRINT_ATOM) { if (left->type != TEP_PRINT_ATOM) {
do_warning_event(event, "bad pointer type"); do_warning_event(event, "bad pointer type");
goto out_free; goto out_free;
} }
new_atom = realloc(left->atom.atom, ret = append(&left->atom.atom, " ", "*");
strlen(left->atom.atom) + 3); if (ret < 0)
if (!new_atom)
goto out_warn_free; goto out_warn_free;
left->atom.atom = new_atom;
strcat(left->atom.atom, " *");
free(arg->op.op); free(arg->op.op);
*arg = *left; *arg = *left;
free(left); free(left);
...@@ -3151,18 +3136,15 @@ process_arg_token(struct tep_event *event, struct tep_print_arg *arg, ...@@ -3151,18 +3136,15 @@ process_arg_token(struct tep_event *event, struct tep_print_arg *arg,
} }
/* atoms can be more than one token long */ /* atoms can be more than one token long */
while (type == TEP_EVENT_ITEM) { while (type == TEP_EVENT_ITEM) {
char *new_atom; int ret;
new_atom = realloc(atom,
strlen(atom) + strlen(token) + 2); ret = append(&atom, " ", token);
if (!new_atom) { if (ret < 0) {
free(atom); free(atom);
*tok = NULL; *tok = NULL;
free_token(token); free_token(token);
return TEP_EVENT_ERROR; return TEP_EVENT_ERROR;
} }
atom = new_atom;
strcat(atom, " ");
strcat(atom, token);
free_token(token); free_token(token);
type = read_token_item(&token); type = read_token_item(&token);
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment