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.orgSigned-off-by: default avatarArnaldo Carvalho de Melo <acme@redhat.com>
parent cf47a8ae
...@@ -106,12 +106,14 @@ static void exec_woman_emacs(const char *path, const char *page) ...@@ -106,12 +106,14 @@ static void exec_woman_emacs(const char *path, const char *page)
if (!check_emacsclient_version()) { if (!check_emacsclient_version()) {
/* This works only with emacsclient version >= 22. */ /* This works only with emacsclient version >= 22. */
struct strbuf man_page = STRBUF_INIT; char *man_page;
if (!path) if (!path)
path = "emacsclient"; path = "emacsclient";
strbuf_addf(&man_page, "(woman \"%s\")", page); if (asprintf(&man_page, "(woman \"%s\")", page) > 0) {
execlp(path, "emacsclient", "-e", man_page.buf, NULL); execlp(path, "emacsclient", "-e", man_page, NULL);
free(man_page);
}
warning("failed to exec '%s': %s", path, warning("failed to exec '%s': %s", path,
strerror_r(errno, sbuf, sizeof(sbuf))); strerror_r(errno, sbuf, sizeof(sbuf)));
} }
...@@ -122,7 +124,7 @@ static void exec_man_konqueror(const char *path, const char *page) ...@@ -122,7 +124,7 @@ static void exec_man_konqueror(const char *path, const char *page)
const char *display = getenv("DISPLAY"); const char *display = getenv("DISPLAY");
if (display && *display) { if (display && *display) {
struct strbuf man_page = STRBUF_INIT; char *man_page;
const char *filename = "kfmclient"; const char *filename = "kfmclient";
char sbuf[STRERR_BUFSIZE]; char sbuf[STRERR_BUFSIZE];
...@@ -141,8 +143,10 @@ static void exec_man_konqueror(const char *path, const char *page) ...@@ -141,8 +143,10 @@ static void exec_man_konqueror(const char *path, const char *page)
filename = file; filename = file;
} else } else
path = "kfmclient"; path = "kfmclient";
strbuf_addf(&man_page, "man:%s(1)", page); if (asprintf(&man_page, "man:%s(1)", page) > 0) {
execlp(path, filename, "newTab", man_page.buf, NULL); execlp(path, filename, "newTab", man_page, NULL);
free(man_page);
}
warning("failed to exec '%s': %s", path, warning("failed to exec '%s': %s", path,
strerror_r(errno, sbuf, sizeof(sbuf))); strerror_r(errno, sbuf, sizeof(sbuf)));
} }
...@@ -161,11 +165,13 @@ static void exec_man_man(const char *path, const char *page) ...@@ -161,11 +165,13 @@ static void exec_man_man(const char *path, const char *page)
static void exec_man_cmd(const char *cmd, const char *page) static void exec_man_cmd(const char *cmd, const char *page)
{ {
struct strbuf shell_cmd = STRBUF_INIT;
char sbuf[STRERR_BUFSIZE]; char sbuf[STRERR_BUFSIZE];
char *shell_cmd;
strbuf_addf(&shell_cmd, "%s %s", cmd, page); if (asprintf(&shell_cmd, "%s %s", cmd, page) > 0) {
execl("/bin/sh", "sh", "-c", shell_cmd.buf, NULL); execl("/bin/sh", "sh", "-c", shell_cmd, NULL);
free(shell_cmd);
}
warning("failed to exec '%s': %s", cmd, warning("failed to exec '%s': %s", cmd,
strerror_r(errno, sbuf, sizeof(sbuf))); strerror_r(errno, sbuf, sizeof(sbuf)));
} }
...@@ -299,43 +305,33 @@ static int is_perf_command(const char *s) ...@@ -299,43 +305,33 @@ static int is_perf_command(const char *s)
is_in_cmdlist(&other_cmds, s); is_in_cmdlist(&other_cmds, s);
} }
static const char *prepend(const char *prefix, const char *cmd)
{
size_t pre_len = strlen(prefix);
size_t cmd_len = strlen(cmd);
char *p = malloc(pre_len + cmd_len + 1);
memcpy(p, prefix, pre_len);
strcpy(p + pre_len, cmd);
return p;
}
static const char *cmd_to_page(const char *perf_cmd) static const char *cmd_to_page(const char *perf_cmd)
{ {
char *s;
if (!perf_cmd) if (!perf_cmd)
return "perf"; return "perf";
else if (!prefixcmp(perf_cmd, "perf")) else if (!prefixcmp(perf_cmd, "perf"))
return perf_cmd; return perf_cmd;
else
return prepend("perf-", perf_cmd); return asprintf(&s, "perf-%s", perf_cmd) < 0 ? NULL : s;
} }
static void setup_man_path(void) static void setup_man_path(void)
{ {
struct strbuf new_path = STRBUF_INIT; char *new_path;
const char *old_path = getenv("MANPATH"); const char *old_path = getenv("MANPATH");
/* We should always put ':' after our path. If there is no /* We should always put ':' after our path. If there is no
* old_path, the ':' at the end will let 'man' to try * old_path, the ':' at the end will let 'man' to try
* system-wide paths after ours to find the manual page. If * system-wide paths after ours to find the manual page. If
* there is old_path, we need ':' as delimiter. */ * there is old_path, we need ':' as delimiter. */
strbuf_addstr(&new_path, system_path(PERF_MAN_PATH)); if (asprintf(&new_path, "%s:%s", system_path(PERF_MAN_PATH), old_path ?: "") > 0) {
strbuf_addch(&new_path, ':'); setenv("MANPATH", new_path, 1);
if (old_path) free(new_path);
strbuf_addstr(&new_path, old_path); } else {
error("Unable to setup man path");
setenv("MANPATH", new_path.buf, 1); }
strbuf_release(&new_path);
} }
static void exec_viewer(const char *name, const char *page) static void exec_viewer(const char *name, const char *page)
...@@ -380,7 +376,7 @@ static int show_info_page(const char *perf_cmd) ...@@ -380,7 +376,7 @@ static int show_info_page(const char *perf_cmd)
return -1; return -1;
} }
static int get_html_page_path(struct strbuf *page_path, const char *page) static int get_html_page_path(char **page_path, const char *page)
{ {
struct stat st; struct stat st;
const char *html_path = system_path(PERF_HTML_PATH); 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) ...@@ -392,10 +388,7 @@ static int get_html_page_path(struct strbuf *page_path, const char *page)
return -1; return -1;
} }
strbuf_init(page_path, 0); return asprintf(page_path, "%s/%s.html", html_path, page);
strbuf_addf(page_path, "%s/%s.html", html_path, page);
return 0;
} }
/* /*
...@@ -413,12 +406,12 @@ static void open_html(const char *path) ...@@ -413,12 +406,12 @@ static void open_html(const char *path)
static int show_html_page(const char *perf_cmd) static int show_html_page(const char *perf_cmd)
{ {
const char *page = cmd_to_page(perf_cmd); const char *page = cmd_to_page(perf_cmd);
struct strbuf page_path; /* it leaks but we exec bellow */ char *page_path; /* it leaks but we exec bellow */
if (get_html_page_path(&page_path, page) != 0) if (get_html_page_path(&page_path, page) < 0)
return -1; return -1;
open_html(page_path.buf); open_html(page_path);
return 0; return 0;
} }
......
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