Skip to content

Commit a610f5c

Browse files
committed
perf help: Use asprintf instead of adhoc equivalents
That doesn't chekcs malloc return and that, when using strbuf, if it can't grow, just explodes away via die(). Cc: Adrian Hunter <adrian.hunter@intel.com> Cc: Jiri Olsa <jolsa@kernel.org> Cc: Namhyung Kim <namhyung@kernel.org> Cc: Wang Nan <wangnan0@huawei.com> Link: http://lkml.kernel.org/n/tip-vr8qsjbwub7e892hpa9msz95@git.kernel.org Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
1 parent cf47a8a commit a610f5c

File tree

1 file changed

+31
-38
lines changed

1 file changed

+31
-38
lines changed

tools/perf/builtin-help.c

Lines changed: 31 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -106,12 +106,14 @@ static void exec_woman_emacs(const char *path, const char *page)
106106

107107
if (!check_emacsclient_version()) {
108108
/* This works only with emacsclient version >= 22. */
109-
struct strbuf man_page = STRBUF_INIT;
109+
char *man_page;
110110

111111
if (!path)
112112
path = "emacsclient";
113-
strbuf_addf(&man_page, "(woman \"%s\")", page);
114-
execlp(path, "emacsclient", "-e", man_page.buf, NULL);
113+
if (asprintf(&man_page, "(woman \"%s\")", page) > 0) {
114+
execlp(path, "emacsclient", "-e", man_page, NULL);
115+
free(man_page);
116+
}
115117
warning("failed to exec '%s': %s", path,
116118
strerror_r(errno, sbuf, sizeof(sbuf)));
117119
}
@@ -122,7 +124,7 @@ static void exec_man_konqueror(const char *path, const char *page)
122124
const char *display = getenv("DISPLAY");
123125

124126
if (display && *display) {
125-
struct strbuf man_page = STRBUF_INIT;
127+
char *man_page;
126128
const char *filename = "kfmclient";
127129
char sbuf[STRERR_BUFSIZE];
128130

@@ -141,8 +143,10 @@ static void exec_man_konqueror(const char *path, const char *page)
141143
filename = file;
142144
} else
143145
path = "kfmclient";
144-
strbuf_addf(&man_page, "man:%s(1)", page);
145-
execlp(path, filename, "newTab", man_page.buf, NULL);
146+
if (asprintf(&man_page, "man:%s(1)", page) > 0) {
147+
execlp(path, filename, "newTab", man_page, NULL);
148+
free(man_page);
149+
}
146150
warning("failed to exec '%s': %s", path,
147151
strerror_r(errno, sbuf, sizeof(sbuf)));
148152
}
@@ -161,11 +165,13 @@ static void exec_man_man(const char *path, const char *page)
161165

162166
static void exec_man_cmd(const char *cmd, const char *page)
163167
{
164-
struct strbuf shell_cmd = STRBUF_INIT;
165168
char sbuf[STRERR_BUFSIZE];
169+
char *shell_cmd;
166170

167-
strbuf_addf(&shell_cmd, "%s %s", cmd, page);
168-
execl("/bin/sh", "sh", "-c", shell_cmd.buf, NULL);
171+
if (asprintf(&shell_cmd, "%s %s", cmd, page) > 0) {
172+
execl("/bin/sh", "sh", "-c", shell_cmd, NULL);
173+
free(shell_cmd);
174+
}
169175
warning("failed to exec '%s': %s", cmd,
170176
strerror_r(errno, sbuf, sizeof(sbuf)));
171177
}
@@ -299,43 +305,33 @@ static int is_perf_command(const char *s)
299305
is_in_cmdlist(&other_cmds, s);
300306
}
301307

302-
static const char *prepend(const char *prefix, const char *cmd)
303-
{
304-
size_t pre_len = strlen(prefix);
305-
size_t cmd_len = strlen(cmd);
306-
char *p = malloc(pre_len + cmd_len + 1);
307-
memcpy(p, prefix, pre_len);
308-
strcpy(p + pre_len, cmd);
309-
return p;
310-
}
311-
312308
static const char *cmd_to_page(const char *perf_cmd)
313309
{
310+
char *s;
311+
314312
if (!perf_cmd)
315313
return "perf";
316314
else if (!prefixcmp(perf_cmd, "perf"))
317315
return perf_cmd;
318-
else
319-
return prepend("perf-", perf_cmd);
316+
317+
return asprintf(&s, "perf-%s", perf_cmd) < 0 ? NULL : s;
320318
}
321319

322320
static void setup_man_path(void)
323321
{
324-
struct strbuf new_path = STRBUF_INIT;
322+
char *new_path;
325323
const char *old_path = getenv("MANPATH");
326324

327325
/* We should always put ':' after our path. If there is no
328326
* old_path, the ':' at the end will let 'man' to try
329327
* system-wide paths after ours to find the manual page. If
330328
* there is old_path, we need ':' as delimiter. */
331-
strbuf_addstr(&new_path, system_path(PERF_MAN_PATH));
332-
strbuf_addch(&new_path, ':');
333-
if (old_path)
334-
strbuf_addstr(&new_path, old_path);
335-
336-
setenv("MANPATH", new_path.buf, 1);
337-
338-
strbuf_release(&new_path);
329+
if (asprintf(&new_path, "%s:%s", system_path(PERF_MAN_PATH), old_path ?: "") > 0) {
330+
setenv("MANPATH", new_path, 1);
331+
free(new_path);
332+
} else {
333+
error("Unable to setup man path");
334+
}
339335
}
340336

341337
static void exec_viewer(const char *name, const char *page)
@@ -380,7 +376,7 @@ static int show_info_page(const char *perf_cmd)
380376
return -1;
381377
}
382378

383-
static int get_html_page_path(struct strbuf *page_path, const char *page)
379+
static int get_html_page_path(char **page_path, const char *page)
384380
{
385381
struct stat st;
386382
const char *html_path = system_path(PERF_HTML_PATH);
@@ -392,10 +388,7 @@ static int get_html_page_path(struct strbuf *page_path, const char *page)
392388
return -1;
393389
}
394390

395-
strbuf_init(page_path, 0);
396-
strbuf_addf(page_path, "%s/%s.html", html_path, page);
397-
398-
return 0;
391+
return asprintf(page_path, "%s/%s.html", html_path, page);
399392
}
400393

401394
/*
@@ -413,12 +406,12 @@ static void open_html(const char *path)
413406
static int show_html_page(const char *perf_cmd)
414407
{
415408
const char *page = cmd_to_page(perf_cmd);
416-
struct strbuf page_path; /* it leaks but we exec bellow */
409+
char *page_path; /* it leaks but we exec bellow */
417410

418-
if (get_html_page_path(&page_path, page) != 0)
411+
if (get_html_page_path(&page_path, page) < 0)
419412
return -1;
420413

421-
open_html(page_path.buf);
414+
open_html(page_path);
422415

423416
return 0;
424417
}

0 commit comments

Comments
 (0)