LKML Archive on lore.kernel.org
 help / color / Atom feed
From: Steven Rostedt <rostedt@goodmis.org>
To: linux-kernel@vger.kernel.org
Cc: Arnaldo Carvalho de Melo <acme@kernel.org>,
	Ingo Molnar <mingo@kernel.org>,
	Andrew Morton <akpm@linux-foundation.org>,
	Namhyung Kim <namhyung@kernel.org>,
	Federico Vaga <federico.vaga@vaga.pv.it>
Subject: [PATCH 07/10] lib traceevent: Use asprintf when possible
Date: Thu, 11 Jan 2018 19:47:48 -0500
Message-ID: <20180112004822.686281649@goodmis.org> (raw)
In-Reply-To: <20180112004741.378806573@goodmis.org>

[-- Attachment #0: 0007-lib-traceevent-Use-asprintf-when-possible.patch --]
[-- Type: text/plain, Size: 3884 bytes --]

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.

Link: http://lkml.kernel.org/r/20170802221558.9684-2-federico.vaga@vaga.pv.it

Signed-off-by: Federico Vaga <federico.vaga@vaga.pv.it>
Signed-off-by: Steven Rostedt (VMware) <rostedt@goodmis.org>
---
 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.13.2

  parent reply index

Thread overview: 51+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-01-12  0:47 [PATCH 00/10] tools lib traceveent: Pull updates from trace-cmd library Steven Rostedt
2018-01-12  0:47 ` [PATCH 01/10] lib, traceevent: Fix bad force_token escape sequence Steven Rostedt
2018-01-12  0:47 ` [PATCH 02/10] lib traceevent: Show value of flags that have not been parsed Steven Rostedt
2018-01-17 16:28   ` [tip:perf/core] tools " tip-bot for Steven Rostedt (VMware)
2018-01-12  0:47 ` [PATCH 03/10] lib traceevent: Print value of unknown symbolic fields Steven Rostedt
2018-01-17 16:28   ` [tip:perf/core] tools " tip-bot for Jan Kiszka
2018-01-12  0:47 ` [PATCH 04/10] lib traceevent: Simplify pointer print logic and fix %pF Steven Rostedt
2018-01-17 16:29   ` [tip:perf/core] tools " tip-bot for Steven Rostedt (VMware)
2018-01-12  0:47 ` [PATCH 05/10] lib traceevent: Handle new pointer processing of bprint strings Steven Rostedt
2018-01-17 16:29   ` [tip:perf/core] tools " tip-bot for Steven Rostedt (VMware)
2018-01-12  0:47 ` [PATCH 06/10] lib traceevent: Show contents (in hex) of data of unrecognized type records Steven Rostedt
2018-01-17 16:30   ` [tip:perf/core] tools " tip-bot for Steven Rostedt (VMware)
2018-01-12  0:47 ` Steven Rostedt [this message]
2018-01-17 16:30   ` [tip:perf/core] tools lib traceevent: Use asprintf when possible tip-bot for Federico Vaga
2018-01-12  0:47 ` [PATCH 08/10] lib traceevent: Add UL suffix to MISSING_EVENTS Steven Rostedt
2018-01-17 16:31   ` [tip:perf/core] tools " tip-bot for Michael Sartain
2018-01-12  0:47 ` [PATCH 09/10] lib traceeevent: Fix missing break in FALSE case of pevent_filter_clear_trivial() Steven Rostedt
2017-04-26 14:55   ` [PATCH] parse-events: Fix the FALSE case in pevent_filter_clear_trivial() Taeung Song
2018-01-17 16:31     ` [tip:perf/core] tools lib traceevent: Fix missing break in FALSE case of pevent_filter_clear_trivial() tip-bot for Taeung Song
2018-01-12  1:00   ` [PATCH 09/10] lib traceeevent: " Taeung Song
2018-01-12  1:14     ` Steven Rostedt
2018-01-12 16:02       ` Arnaldo Carvalho de Melo
2018-01-12  0:47 ` [PATCH 10/10] lib traceevent: Fix get_field_str() for dynamic strings Steven Rostedt
2018-01-17 16:31   ` [tip:perf/core] tools " tip-bot for Steven Rostedt (VMware)
2018-01-17  6:02 ` [PATCH 00/10] tools lib traceveent: Pull updates from trace-cmd library Namhyung Kim
  -- strict thread matches above, loose matches on Subject: below --
2017-10-16 16:55 [PATCH v2 0/4] trace-cmd: Fixes for four minor bugs Michael Sartain
2017-10-16 16:55 ` [PATCH v2 1/4] trace-cmd: Fix incorrect malloc size arg: *item instead of item Michael Sartain
2017-10-16 16:55 ` [PATCH v2 2/4] trace-cmd: Fix NULL pointer being passed to memcpy Michael Sartain
2017-10-16 16:55 ` [PATCH v2 3/4] trace-cmd: Add UL suffix to MISSING_EVENTS since ints shouldn't be left shifted by 31 Michael Sartain
2017-10-16 16:55 ` [PATCH v2 4/4] trace-cmd: Use unsigned values in Hsieh's trace_hash fast hash function Michael Sartain
2017-09-28 20:13 [PATCH][trace-cmd] Print value of unknown symbolic fields Jan Kiszka
2017-10-03 22:46 ` Steven Rostedt
2017-10-03 23:12 ` Steven Rostedt
2017-08-02 22:15 [PATCH V3 1/2] use direname instead of custom code Federico Vaga
2017-08-02 22:15 ` [PATCH V3 2/2] It makes the code clearer and less error prone Federico Vaga
2017-08-14 17:33   ` Steven Rostedt
2017-08-15  7:25     ` Federico Vaga
2017-11-08 18:50       ` Steven Rostedt
2017-08-02 23:48 ` [PATCH V3 1/2] trace-cmd record: use direname instead of custom code Steven Rostedt
2017-08-14 17:14 ` [PATCH V3 1/2] " Steven Rostedt
2017-06-15  0:27 [PATCH v3 0/6] trace-cmd: escape sequence, EINTR, error checking bug fixes Michael Sartain
2017-06-15  0:27 ` [PATCH v3 1/6] Fix bad force_token escape sequence Michael Sartain
2018-01-17 16:28   ` [tip:perf/core] tools lib traceevent: " tip-bot for Michael Sartain
2017-06-15  0:27 ` [PATCH v3 2/6] Fix unsigned return values being error checked as negative Michael Sartain
2017-06-15  0:27 ` [PATCH v3 3/6] Handle EINTR signal interrupts for read, write, open calls Michael Sartain
2017-06-21 13:26   ` Steven Rostedt
2017-06-15  0:27 ` [PATCH v3 4/6] Fix read / write data offsets in read / write loops Michael Sartain
2017-06-21 13:29   ` Steven Rostedt
2017-06-21 17:36     ` Michael Sartain
2017-06-15  0:28 ` [PATCH v3 5/6] Fix function prototypes for __vwarning, __vpr_stat, and __vdie Michael Sartain
2017-06-15  0:28 ` [PATCH v3 6/6] Fix cases where string literals were passed as string format args Michael Sartain

Reply instructions:

You may reply publically 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=20180112004822.686281649@goodmis.org \
    --to=rostedt@goodmis.org \
    --cc=acme@kernel.org \
    --cc=akpm@linux-foundation.org \
    --cc=federico.vaga@vaga.pv.it \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@kernel.org \
    --cc=namhyung@kernel.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

LKML Archive on lore.kernel.org

Archives are clonable:
	git clone --mirror https://lore.kernel.org/lkml/0 lkml/git/0.git
	git clone --mirror https://lore.kernel.org/lkml/1 lkml/git/1.git
	git clone --mirror https://lore.kernel.org/lkml/2 lkml/git/2.git
	git clone --mirror https://lore.kernel.org/lkml/3 lkml/git/3.git
	git clone --mirror https://lore.kernel.org/lkml/4 lkml/git/4.git
	git clone --mirror https://lore.kernel.org/lkml/5 lkml/git/5.git
	git clone --mirror https://lore.kernel.org/lkml/6 lkml/git/6.git
	git clone --mirror https://lore.kernel.org/lkml/7 lkml/git/7.git
	git clone --mirror https://lore.kernel.org/lkml/8 lkml/git/8.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 lkml lkml/ https://lore.kernel.org/lkml \
		linux-kernel@vger.kernel.org
	public-inbox-index lkml

Example config snippet for mirrors

Newsgroup available over NNTP:
	nntp://nntp.lore.kernel.org/org.kernel.vger.linux-kernel


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git