Skip to content

Commit

Permalink
perf tools: Convert open coded equivalents to asprintf()
Browse files Browse the repository at this point in the history
The following snippet
	V = malloc(S);
	if (!V) { }
	sprintf(V, ...)

Can be easily changed to a one line:

	if (asprintf(&V, ...) < 0) { }

Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Link: http://lkml.kernel.org/r/1404474229-15272-1-git-send-email-andriy.shevchenko@linux.intel.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
  • Loading branch information
Andy Shevchenko authored and Arnaldo Carvalho de Melo committed Jul 7, 2014
1 parent f8d9ccd commit d400a68
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 16 deletions.
12 changes: 3 additions & 9 deletions tools/perf/util/trace-event-info.c
Original file line number Diff line number Diff line change
Expand Up @@ -191,12 +191,10 @@ static int copy_event_system(const char *sys, struct tracepoint_path *tps)
strcmp(dent->d_name, "..") == 0 ||
!name_in_tp_list(dent->d_name, tps))
continue;
format = malloc(strlen(sys) + strlen(dent->d_name) + 10);
if (!format) {
if (asprintf(&format, "%s/%s/format", sys, dent->d_name) < 0) {
err = -ENOMEM;
goto out;
}
sprintf(format, "%s/%s/format", sys, dent->d_name);
ret = stat(format, &st);
free(format);
if (ret < 0)
Expand All @@ -217,12 +215,10 @@ static int copy_event_system(const char *sys, struct tracepoint_path *tps)
strcmp(dent->d_name, "..") == 0 ||
!name_in_tp_list(dent->d_name, tps))
continue;
format = malloc(strlen(sys) + strlen(dent->d_name) + 10);
if (!format) {
if (asprintf(&format, "%s/%s/format", sys, dent->d_name) < 0) {
err = -ENOMEM;
goto out;
}
sprintf(format, "%s/%s/format", sys, dent->d_name);
ret = stat(format, &st);

if (ret >= 0) {
Expand Down Expand Up @@ -317,12 +313,10 @@ static int record_event_files(struct tracepoint_path *tps)
strcmp(dent->d_name, "ftrace") == 0 ||
!system_in_tp_list(dent->d_name, tps))
continue;
sys = malloc(strlen(path) + strlen(dent->d_name) + 2);
if (!sys) {
if (asprintf(&sys, "%s/%s", path, dent->d_name) < 0) {
err = -ENOMEM;
goto out;
}
sprintf(sys, "%s/%s", path, dent->d_name);
ret = stat(sys, &st);
if (ret >= 0) {
ssize_t size = strlen(dent->d_name) + 1;
Expand Down
9 changes: 2 additions & 7 deletions tools/perf/util/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -333,12 +333,9 @@ const char *find_tracing_dir(void)
if (!debugfs)
return NULL;

tracing = malloc(strlen(debugfs) + 9);
if (!tracing)
if (asprintf(&tracing, "%s/tracing", debugfs) < 0)
return NULL;

sprintf(tracing, "%s/tracing", debugfs);

tracing_found = 1;
return tracing;
}
Expand All @@ -352,11 +349,9 @@ char *get_tracing_file(const char *name)
if (!tracing)
return NULL;

file = malloc(strlen(tracing) + strlen(name) + 2);
if (!file)
if (asprintf(&file, "%s/%s", tracing, name) < 0)
return NULL;

sprintf(file, "%s/%s", tracing, name);
return file;
}

Expand Down

0 comments on commit d400a68

Please sign in to comment.