aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorArnaldo Carvalho de Melo <acme@redhat.com>2016-03-23 14:44:09 -0400
committerArnaldo Carvalho de Melo <acme@redhat.com>2016-03-23 15:36:07 -0400
commita610f5cbb2a7e26b7f2df2f5df255e34007d6bfd (patch)
treed69e2f9289c94abc500de45d3c178b4fb0f6d9ee
parentcf47a8aede9febee21cbd21d40a9c47244773a6c (diff)
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>
-rw-r--r--tools/perf/builtin-help.c69
1 files changed, 31 insertions, 38 deletions
diff --git a/tools/perf/builtin-help.c b/tools/perf/builtin-help.c
index 49d55e21b1b0..bc1de9b8fd67 100644
--- a/tools/perf/builtin-help.c
+++ b/tools/perf/builtin-help.c
@@ -106,12 +106,14 @@ static void exec_woman_emacs(const char *path, const char *page)
106 106
107 if (!check_emacsclient_version()) { 107 if (!check_emacsclient_version()) {
108 /* This works only with emacsclient version >= 22. */ 108 /* This works only with emacsclient version >= 22. */
109 struct strbuf man_page = STRBUF_INIT; 109 char *man_page;
110 110
111 if (!path) 111 if (!path)
112 path = "emacsclient"; 112 path = "emacsclient";
113 strbuf_addf(&man_page, "(woman \"%s\")", page); 113 if (asprintf(&man_page, "(woman \"%s\")", page) > 0) {
114 execlp(path, "emacsclient", "-e", man_page.buf, NULL); 114 execlp(path, "emacsclient", "-e", man_page, NULL);
115 free(man_page);
116 }
115 warning("failed to exec '%s': %s", path, 117 warning("failed to exec '%s': %s", path,
116 strerror_r(errno, sbuf, sizeof(sbuf))); 118 strerror_r(errno, sbuf, sizeof(sbuf)));
117 } 119 }
@@ -122,7 +124,7 @@ static void exec_man_konqueror(const char *path, const char *page)
122 const char *display = getenv("DISPLAY"); 124 const char *display = getenv("DISPLAY");
123 125
124 if (display && *display) { 126 if (display && *display) {
125 struct strbuf man_page = STRBUF_INIT; 127 char *man_page;
126 const char *filename = "kfmclient"; 128 const char *filename = "kfmclient";
127 char sbuf[STRERR_BUFSIZE]; 129 char sbuf[STRERR_BUFSIZE];
128 130
@@ -141,8 +143,10 @@ static void exec_man_konqueror(const char *path, const char *page)
141 filename = file; 143 filename = file;
142 } else 144 } else
143 path = "kfmclient"; 145 path = "kfmclient";
144 strbuf_addf(&man_page, "man:%s(1)", page); 146 if (asprintf(&man_page, "man:%s(1)", page) > 0) {
145 execlp(path, filename, "newTab", man_page.buf, NULL); 147 execlp(path, filename, "newTab", man_page, NULL);
148 free(man_page);
149 }
146 warning("failed to exec '%s': %s", path, 150 warning("failed to exec '%s': %s", path,
147 strerror_r(errno, sbuf, sizeof(sbuf))); 151 strerror_r(errno, sbuf, sizeof(sbuf)));
148 } 152 }
@@ -161,11 +165,13 @@ static void exec_man_man(const char *path, const char *page)
161 165
162static void exec_man_cmd(const char *cmd, const char *page) 166static void exec_man_cmd(const char *cmd, const char *page)
163{ 167{
164 struct strbuf shell_cmd = STRBUF_INIT;
165 char sbuf[STRERR_BUFSIZE]; 168 char sbuf[STRERR_BUFSIZE];
169 char *shell_cmd;
166 170
167 strbuf_addf(&shell_cmd, "%s %s", cmd, page); 171 if (asprintf(&shell_cmd, "%s %s", cmd, page) > 0) {
168 execl("/bin/sh", "sh", "-c", shell_cmd.buf, NULL); 172 execl("/bin/sh", "sh", "-c", shell_cmd, NULL);
173 free(shell_cmd);
174 }
169 warning("failed to exec '%s': %s", cmd, 175 warning("failed to exec '%s': %s", cmd,
170 strerror_r(errno, sbuf, sizeof(sbuf))); 176 strerror_r(errno, sbuf, sizeof(sbuf)));
171} 177}
@@ -299,43 +305,33 @@ static int is_perf_command(const char *s)
299 is_in_cmdlist(&other_cmds, s); 305 is_in_cmdlist(&other_cmds, s);
300} 306}
301 307
302static 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
312static const char *cmd_to_page(const char *perf_cmd) 308static const char *cmd_to_page(const char *perf_cmd)
313{ 309{
310 char *s;
311
314 if (!perf_cmd) 312 if (!perf_cmd)
315 return "perf"; 313 return "perf";
316 else if (!prefixcmp(perf_cmd, "perf")) 314 else if (!prefixcmp(perf_cmd, "perf"))
317 return perf_cmd; 315 return perf_cmd;
318 else 316
319 return prepend("perf-", perf_cmd); 317 return asprintf(&s, "perf-%s", perf_cmd) < 0 ? NULL : s;
320} 318}
321 319
322static void setup_man_path(void) 320static void setup_man_path(void)
323{ 321{
324 struct strbuf new_path = STRBUF_INIT; 322 char *new_path;
325 const char *old_path = getenv("MANPATH"); 323 const char *old_path = getenv("MANPATH");
326 324
327 /* We should always put ':' after our path. If there is no 325 /* We should always put ':' after our path. If there is no
328 * old_path, the ':' at the end will let 'man' to try 326 * old_path, the ':' at the end will let 'man' to try
329 * system-wide paths after ours to find the manual page. If 327 * system-wide paths after ours to find the manual page. If
330 * there is old_path, we need ':' as delimiter. */ 328 * there is old_path, we need ':' as delimiter. */
331 strbuf_addstr(&new_path, system_path(PERF_MAN_PATH)); 329 if (asprintf(&new_path, "%s:%s", system_path(PERF_MAN_PATH), old_path ?: "") > 0) {
332 strbuf_addch(&new_path, ':'); 330 setenv("MANPATH", new_path, 1);
333 if (old_path) 331 free(new_path);
334 strbuf_addstr(&new_path, old_path); 332 } else {
335 333 error("Unable to setup man path");
336 setenv("MANPATH", new_path.buf, 1); 334 }
337
338 strbuf_release(&new_path);
339} 335}
340 336
341static void exec_viewer(const char *name, const char *page) 337static void exec_viewer(const char *name, const char *page)
@@ -380,7 +376,7 @@ static int show_info_page(const char *perf_cmd)
380 return -1; 376 return -1;
381} 377}
382 378
383static int get_html_page_path(struct strbuf *page_path, const char *page) 379static int get_html_page_path(char **page_path, const char *page)
384{ 380{
385 struct stat st; 381 struct stat st;
386 const char *html_path = system_path(PERF_HTML_PATH); 382 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 return -1; 388 return -1;
393 } 389 }
394 390
395 strbuf_init(page_path, 0); 391 return asprintf(page_path, "%s/%s.html", html_path, page);
396 strbuf_addf(page_path, "%s/%s.html", html_path, page);
397
398 return 0;
399} 392}
400 393
401/* 394/*
@@ -413,12 +406,12 @@ static void open_html(const char *path)
413static int show_html_page(const char *perf_cmd) 406static int show_html_page(const char *perf_cmd)
414{ 407{
415 const char *page = cmd_to_page(perf_cmd); 408 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 */
417 410
418 if (get_html_page_path(&page_path, page) != 0) 411 if (get_html_page_path(&page_path, page) < 0)
419 return -1; 412 return -1;
420 413
421 open_html(page_path.buf); 414 open_html(page_path);
422 415
423 return 0; 416 return 0;
424} 417}