linux-perf-users.vger.kernel.org archive mirror
 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, linux-perf-users@vger.kernel.org,
	Federico Vaga <federico.vaga@vaga.pv.it>,
	Andrew Morton <akpm@linux-foundation.org>,
	Steven Rostedt <rostedt@goodmis.org>,
	Arnaldo Carvalho de Melo <acme@redhat.com>
Subject: [PATCH 14/32] tools lib traceevent: Use asprintf when possible
Date: Wed, 17 Jan 2018 13:12:04 -0300	[thread overview]
Message-ID: <20180117161222.15611-15-acme@kernel.org> (raw)
In-Reply-To: <20180117161222.15611-1-acme@kernel.org>

From: Federico Vaga <federico.vaga@vaga.pv.it>

It makes the code clearer and less error prone.

clearer:
- less code
- the code is now using the same format to create strings dynamically

less error prone:
- no magic number +2 +9 +5 to compute the size
- no copy&paste of the strings to compute the size and to concatenate

The function `asprintf` is not POSIX standard but the program
was already using it. Later it can be decided to use only POSIX
functions, then we can easly replace all the `asprintf(3)` with a local
implementation of that function.

Signed-off-by: Federico Vaga <federico.vaga@vaga.pv.it>
Acked-by: Namhyung Kim <namhyung@kernel.org>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Federico Vaga <federico.vaga@vaga.pv.it>
Link: http://lkml.kernel.org/r/20170802221558.9684-2-federico.vaga@vaga.pv.it
Link: http://lkml.kernel.org/r/20180112004822.686281649@goodmis.org
Signed-off-by: Steven Rostedt <rostedt@goodmis.org>
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
---
 tools/lib/traceevent/event-plugin.c | 24 +++++++++---------------
 tools/lib/traceevent/parse-filter.c | 11 ++++-------
 2 files changed, 13 insertions(+), 22 deletions(-)

diff --git a/tools/lib/traceevent/event-plugin.c b/tools/lib/traceevent/event-plugin.c
index a16756ae3526..d542cb60ca1a 100644
--- a/tools/lib/traceevent/event-plugin.c
+++ b/tools/lib/traceevent/event-plugin.c
@@ -120,12 +120,12 @@ char **traceevent_plugin_list_options(void)
 		for (op = reg->options; op->name; op++) {
 			char *alias = op->plugin_alias ? op->plugin_alias : op->file;
 			char **temp = list;
+			int ret;
 
-			name = malloc(strlen(op->name) + strlen(alias) + 2);
-			if (!name)
+			ret = asprintf(&name, "%s:%s", alias, op->name);
+			if (ret < 0)
 				goto err;
 
-			sprintf(name, "%s:%s", alias, op->name);
 			list = realloc(list, count + 2);
 			if (!list) {
 				list = temp;
@@ -290,17 +290,14 @@ load_plugin(struct pevent *pevent, const char *path,
 	const char *alias;
 	char *plugin;
 	void *handle;
+	int ret;
 
-	plugin = malloc(strlen(path) + strlen(file) + 2);
-	if (!plugin) {
+	ret = asprintf(&plugin, "%s/%s", path, file);
+	if (ret < 0) {
 		warning("could not allocate plugin memory\n");
 		return;
 	}
 
-	strcpy(plugin, path);
-	strcat(plugin, "/");
-	strcat(plugin, file);
-
 	handle = dlopen(plugin, RTLD_NOW | RTLD_GLOBAL);
 	if (!handle) {
 		warning("could not load plugin '%s'\n%s\n",
@@ -391,6 +388,7 @@ load_plugins(struct pevent *pevent, const char *suffix,
 	char *home;
 	char *path;
 	char *envdir;
+	int ret;
 
 	if (pevent->flags & PEVENT_DISABLE_PLUGINS)
 		return;
@@ -421,16 +419,12 @@ load_plugins(struct pevent *pevent, const char *suffix,
 	if (!home)
 		return;
 
-	path = malloc(strlen(home) + strlen(LOCAL_PLUGIN_DIR) + 2);
-	if (!path) {
+	ret = asprintf(&path, "%s/%s", home, LOCAL_PLUGIN_DIR);
+	if (ret < 0) {
 		warning("could not allocate plugin memory\n");
 		return;
 	}
 
-	strcpy(path, home);
-	strcat(path, "/");
-	strcat(path, LOCAL_PLUGIN_DIR);
-
 	load_plugins_dir(pevent, suffix, path, load_plugin, data);
 
 	free(path);
diff --git a/tools/lib/traceevent/parse-filter.c b/tools/lib/traceevent/parse-filter.c
index 315df0a70265..2410afdcbcfe 100644
--- a/tools/lib/traceevent/parse-filter.c
+++ b/tools/lib/traceevent/parse-filter.c
@@ -287,12 +287,10 @@ find_event(struct pevent *pevent, struct event_list **events,
 		sys_name = NULL;
 	}
 
-	reg = malloc(strlen(event_name) + 3);
-	if (reg == NULL)
+	ret = asprintf(&reg, "^%s$", event_name);
+	if (ret < 0)
 		return PEVENT_ERRNO__MEM_ALLOC_FAILED;
 
-	sprintf(reg, "^%s$", event_name);
-
 	ret = regcomp(&ereg, reg, REG_ICASE|REG_NOSUB);
 	free(reg);
 
@@ -300,13 +298,12 @@ find_event(struct pevent *pevent, struct event_list **events,
 		return PEVENT_ERRNO__INVALID_EVENT_NAME;
 
 	if (sys_name) {
-		reg = malloc(strlen(sys_name) + 3);
-		if (reg == NULL) {
+		ret = asprintf(&reg, "^%s$", sys_name);
+		if (ret < 0) {
 			regfree(&ereg);
 			return PEVENT_ERRNO__MEM_ALLOC_FAILED;
 		}
 
-		sprintf(reg, "^%s$", sys_name);
 		ret = regcomp(&sreg, reg, REG_ICASE|REG_NOSUB);
 		free(reg);
 		if (ret) {
-- 
2.14.3

  parent reply	other threads:[~2018-01-17 16:12 UTC|newest]

Thread overview: 34+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-01-17 16:11 [GIT PULL 00/32] perf/core improvements and fixes Arnaldo Carvalho de Melo
2018-01-17 16:11 ` [PATCH 01/32] perf evsel: Fix incorrect handling of type _TERM_DRV_CFG Arnaldo Carvalho de Melo
2018-01-17 16:11 ` [PATCH 02/32] perf evlist: Remove trailing semicolon Arnaldo Carvalho de Melo
2018-01-17 16:11 ` [PATCH 03/32] perf script python: Add script to profile and resolve physical mem type Arnaldo Carvalho de Melo
2018-01-17 16:11 ` [PATCH 04/32] perf trace: No need to set PERF_SAMPLE_IDENTIFIER explicitely Arnaldo Carvalho de Melo
2018-01-17 16:11 ` [PATCH 05/32] perf tools: Fix copyfile_offset update of output offset Arnaldo Carvalho de Melo
2018-01-17 16:11 ` [PATCH 06/32] perf evsel: Check if callchain is enabled before setting it up Arnaldo Carvalho de Melo
2018-01-17 16:11 ` [PATCH 07/32] perf trace: Fix setting of --call-graph/--max-stack for non-syscall events Arnaldo Carvalho de Melo
2018-01-17 16:11 ` [PATCH 08/32] tools lib traceevent: Fix bad force_token escape sequence Arnaldo Carvalho de Melo
2018-01-17 16:11 ` [PATCH 09/32] tools lib traceevent: Show value of flags that have not been parsed Arnaldo Carvalho de Melo
2018-01-17 16:12 ` [PATCH 10/32] tools lib traceevent: Print value of unknown symbolic fields Arnaldo Carvalho de Melo
2018-01-17 16:12 ` [PATCH 11/32] tools lib traceevent: Simplify pointer print logic and fix %pF Arnaldo Carvalho de Melo
2018-01-17 16:12 ` [PATCH 12/32] tools lib traceevent: Handle new pointer processing of bprint strings Arnaldo Carvalho de Melo
2018-01-17 16:12 ` [PATCH 13/32] tools lib traceevent: Show contents (in hex) of data of unrecognized type records Arnaldo Carvalho de Melo
2018-01-17 16:12 ` Arnaldo Carvalho de Melo [this message]
2018-01-17 16:12 ` [PATCH 15/32] tools lib traceevent: Add UL suffix to MISSING_EVENTS Arnaldo Carvalho de Melo
2018-01-17 16:12 ` [PATCH 16/32] tools lib traceevent: Fix missing break in FALSE case of pevent_filter_clear_trivial() Arnaldo Carvalho de Melo
2018-01-17 16:12 ` [PATCH 17/32] tools lib traceevent: Fix get_field_str() for dynamic strings Arnaldo Carvalho de Melo
2018-01-17 16:12 ` [PATCH 18/32] perf tools: Add ARM Statistical Profiling Extensions (SPE) support Arnaldo Carvalho de Melo
2018-01-17 16:12 ` [PATCH 19/32] perf callchain: Fix attr.sample_max_stack setting Arnaldo Carvalho de Melo
2018-01-17 16:12 ` [PATCH 20/32] perf unwind: Do not look just at the global callchain_param.record_mode Arnaldo Carvalho de Melo
2018-01-17 16:12 ` [PATCH 21/32] perf trace: Setup DWARF callchains for non-syscall events when --max-stack is used Arnaldo Carvalho de Melo
2018-01-17 16:12 ` [PATCH 22/32] perf trace: Allow overriding global --max-stack per event Arnaldo Carvalho de Melo
2018-01-17 16:12 ` [PATCH 23/32] perf callchains: Ask for PERF_RECORD_MMAP for data mmaps for DWARF unwinding Arnaldo Carvalho de Melo
2018-01-17 16:12 ` [PATCH 24/32] perf report: Improve error msg when no first/last sample time found Arnaldo Carvalho de Melo
2018-01-17 16:12 ` [PATCH 25/32] perf script: " Arnaldo Carvalho de Melo
2018-01-17 16:12 ` [PATCH 26/32] perf util: Improve error checking for time percent input Arnaldo Carvalho de Melo
2018-01-17 16:12 ` [PATCH 27/32] perf util: Support no index time percent slice Arnaldo Carvalho de Melo
2018-01-17 16:12 ` [PATCH 28/32] perf report: Add an indication of what time slices are used Arnaldo Carvalho de Melo
2018-01-17 16:12 ` [PATCH 29/32] perf util: Allocate time slices buffer according to number of comma Arnaldo Carvalho de Melo
2018-01-17 16:12 ` [PATCH 30/32] perf report: Remove the time slices number limitation Arnaldo Carvalho de Melo
2018-01-17 16:12 ` [PATCH 31/32] perf script: " Arnaldo Carvalho de Melo
2018-01-17 16:12 ` [PATCH 32/32] perf record: Fix failed memory allocation for get_cpuid_str Arnaldo Carvalho de Melo
2018-01-17 16:22 ` [GIT PULL 00/32] 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=20180117161222.15611-15-acme@kernel.org \
    --to=acme@kernel.org \
    --cc=acme@redhat.com \
    --cc=akpm@linux-foundation.org \
    --cc=federico.vaga@vaga.pv.it \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-perf-users@vger.kernel.org \
    --cc=mingo@kernel.org \
    --cc=rostedt@goodmis.org \
    /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;
as well as URLs for NNTP newsgroup(s).