mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Arnaldo Carvalho de Melo <acme@kernel.org>
To: Ingo Molnar <mingo@kernel.org>
Cc: linux-kernel@vger.kernel.org,
	Arnaldo Carvalho de Melo <acme@redhat.com>,
	Adrian Hunter <adrian.hunter@intel.com>,
	Jiri Olsa <jolsa@kernel.org>, Namhyung Kim <namhyung@kernel.org>,
	Wang Nan <wangnan0@huawei.com>
Subject: [PATCH 19/23] perf help: Use asprintf instead of adhoc equivalents
Date: Wed, 23 Mar 2016 18:17:56 -0300	[thread overview]
Message-ID: <1458767880-3288-20-git-send-email-acme@kernel.org> (raw)
In-Reply-To: <1458767880-3288-1-git-send-email-acme@kernel.org>

From: Arnaldo Carvalho de Melo <acme@redhat.com>

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>
---
 tools/perf/builtin-help.c | 69 +++++++++++++++++++++--------------------------
 1 file 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)
 
 	if (!check_emacsclient_version()) {
 		/* This works only with emacsclient version >= 22. */
-		struct strbuf man_page = STRBUF_INIT;
+		char *man_page;
 
 		if (!path)
 			path = "emacsclient";
-		strbuf_addf(&man_page, "(woman \"%s\")", page);
-		execlp(path, "emacsclient", "-e", man_page.buf, NULL);
+		if (asprintf(&man_page, "(woman \"%s\")", page) > 0) {
+			execlp(path, "emacsclient", "-e", man_page, NULL);
+			free(man_page);
+		}
 		warning("failed to exec '%s': %s", path,
 			strerror_r(errno, sbuf, sizeof(sbuf)));
 	}
@@ -122,7 +124,7 @@ static void exec_man_konqueror(const char *path, const char *page)
 	const char *display = getenv("DISPLAY");
 
 	if (display && *display) {
-		struct strbuf man_page = STRBUF_INIT;
+		char *man_page;
 		const char *filename = "kfmclient";
 		char sbuf[STRERR_BUFSIZE];
 
@@ -141,8 +143,10 @@ static void exec_man_konqueror(const char *path, const char *page)
 				filename = file;
 		} else
 			path = "kfmclient";
-		strbuf_addf(&man_page, "man:%s(1)", page);
-		execlp(path, filename, "newTab", man_page.buf, NULL);
+		if (asprintf(&man_page, "man:%s(1)", page) > 0) {
+			execlp(path, filename, "newTab", man_page, NULL);
+			free(man_page);
+		}
 		warning("failed to exec '%s': %s", path,
 			strerror_r(errno, sbuf, sizeof(sbuf)));
 	}
@@ -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)
 {
-	struct strbuf shell_cmd = STRBUF_INIT;
 	char sbuf[STRERR_BUFSIZE];
+	char *shell_cmd;
 
-	strbuf_addf(&shell_cmd, "%s %s", cmd, page);
-	execl("/bin/sh", "sh", "-c", shell_cmd.buf, NULL);
+	if (asprintf(&shell_cmd, "%s %s", cmd, page) > 0) {
+		execl("/bin/sh", "sh", "-c", shell_cmd, NULL);
+		free(shell_cmd);
+	}
 	warning("failed to exec '%s': %s", cmd,
 		strerror_r(errno, sbuf, sizeof(sbuf)));
 }
@@ -299,43 +305,33 @@ static int is_perf_command(const char *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)
 {
+	char *s;
+
 	if (!perf_cmd)
 		return "perf";
 	else if (!prefixcmp(perf_cmd, "perf"))
 		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)
 {
-	struct strbuf new_path = STRBUF_INIT;
+	char *new_path;
 	const char *old_path = getenv("MANPATH");
 
 	/* We should always put ':' after our path. If there is no
 	 * old_path, the ':' at the end will let 'man' to try
 	 * system-wide paths after ours to find the manual page. If
 	 * there is old_path, we need ':' as delimiter. */
-	strbuf_addstr(&new_path, system_path(PERF_MAN_PATH));
-	strbuf_addch(&new_path, ':');
-	if (old_path)
-		strbuf_addstr(&new_path, old_path);
-
-	setenv("MANPATH", new_path.buf, 1);
-
-	strbuf_release(&new_path);
+	if (asprintf(&new_path, "%s:%s", system_path(PERF_MAN_PATH), old_path ?: "") > 0) {
+		setenv("MANPATH", new_path, 1);
+		free(new_path);
+	} else {
+		error("Unable to setup man path");
+	}
 }
 
 static void exec_viewer(const char *name, const char *page)
@@ -380,7 +376,7 @@ static int show_info_page(const char *perf_cmd)
 	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;
 	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)
 		return -1;
 	}
 
-	strbuf_init(page_path, 0);
-	strbuf_addf(page_path, "%s/%s.html", html_path, page);
-
-	return 0;
+	return asprintf(page_path, "%s/%s.html", html_path, page);
 }
 
 /*
@@ -413,12 +406,12 @@ static void open_html(const char *path)
 static int show_html_page(const char *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;
 
-	open_html(page_path.buf);
+	open_html(page_path);
 
 	return 0;
 }
-- 
2.5.5

  parent reply	other threads:[~2016-03-23 21:21 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-03-23 21:17 [GIT PULL 00/23] perf/core improvements and fixes Arnaldo Carvalho de Melo
2016-03-23 21:17 ` [PATCH 01/23] perf test: Remove 'core_id' check in topo test Arnaldo Carvalho de Melo
2016-03-23 21:17 ` [PATCH 02/23] tools: Move utilities.mak from perf to tools/scripts/ Arnaldo Carvalho de Melo
2016-03-23 21:17 ` [PATCH 03/23] perf symbols: Record text offset in dso to calculate objdump address Arnaldo Carvalho de Melo
2016-03-23 21:17 ` [PATCH 04/23] perf bench numa: Fix assertion for nodes bitfield Arnaldo Carvalho de Melo
2016-03-23 21:17 ` [PATCH 05/23] perf list: Fix documentation of :ppp Arnaldo Carvalho de Melo
2016-03-23 21:17 ` [PATCH 06/23] perf tools: Remove misplaced __maybe_unused Arnaldo Carvalho de Melo
2016-03-23 21:17 ` [PATCH 07/23] perf tests: Forward the perf_sample in the dwarf unwind test Arnaldo Carvalho de Melo
2016-03-23 21:17 ` [PATCH 08/23] perf tools: Add cpumode to struct perf_sample Arnaldo Carvalho de Melo
2016-03-23 21:17 ` [PATCH 09/23] perf machine: Rename perf_event__preprocess_sample to machine__resolve Arnaldo Carvalho de Melo
2016-03-23 21:17 ` [PATCH 10/23] perf thread: Rename perf_event__preprocess_sample_addr to thread__resolve Arnaldo Carvalho de Melo
2016-03-23 21:17 ` [PATCH 11/23] perf script: Remove lots of unused arguments Arnaldo Carvalho de Melo
2016-03-23 21:17 ` [PATCH 12/23] perf tools: Remove unused DIE_IF macro Arnaldo Carvalho de Melo
2016-03-23 21:17 ` [PATCH 13/23] perf tools: Simplify die() mechanism Arnaldo Carvalho de Melo
2016-03-23 21:17 ` [PATCH 14/23] perf tools: Remove needless 'extern' from function prototypes Arnaldo Carvalho de Melo
2016-03-23 21:17 ` [PATCH 15/23] tools lib traceevent: Remove redundant CPU output Arnaldo Carvalho de Melo
2016-03-23 21:17 ` [PATCH 16/23] tools include: Copy linux/stringify.h from the kernel Arnaldo Carvalho de Melo
2016-03-23 21:17 ` [PATCH 17/23] perf tools: Do not include stringify.h from the kernel sources Arnaldo Carvalho de Melo
2016-03-23 21:17 ` [PATCH 18/23] perf tools: Remove unused perf_pathdup, xstrdup functions Arnaldo Carvalho de Melo
2016-03-23 21:17 ` Arnaldo Carvalho de Melo [this message]
2016-03-23 21:17 ` [PATCH 20/23] perf probe: No need to use formatting strbuf method Arnaldo Carvalho de Melo
2016-03-23 21:17 ` [PATCH 21/23] perf tools: Unexport some methods unused outside strbuf.c Arnaldo Carvalho de Melo
2016-03-23 21:17 ` [PATCH 22/23] perf llvm: Use realpath to canonicalize paths Arnaldo Carvalho de Melo
2016-03-23 21:18 ` [PATCH 23/23] perf llvm: Use strerror_r instead of the thread unsafe strerror one Arnaldo Carvalho de Melo
2016-03-24  7:32 ` [GIT PULL 00/23] perf/core improvements and fixes Ingo Molnar

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1458767880-3288-20-git-send-email-acme@kernel.org \
    --to=acme@kernel.org \
    --cc=acme@redhat.com \
    --cc=adrian.hunter@intel.com \
    --cc=jolsa@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@kernel.org \
    --cc=namhyung@kernel.org \
    --cc=wangnan0@huawei.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

Powered by JetHome