diff options
author | Federico Vaga <federico.vaga@vaga.pv.it> | 2018-01-11 19:47:48 -0500 |
---|---|---|
committer | Arnaldo Carvalho de Melo <acme@redhat.com> | 2018-01-17 08:22:19 -0500 |
commit | 67dfc376f3dfdc39b9125f32d5b24053a4da264f (patch) | |
tree | b8025b85da4c1a82de5f0b9fe6566d654d1be469 /tools/lib/traceevent | |
parent | e877372880f72399323e433187cce2bfbea40263 (diff) |
tools lib traceevent: Use asprintf when possible
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>
Diffstat (limited to 'tools/lib/traceevent')
-rw-r--r-- | tools/lib/traceevent/event-plugin.c | 24 | ||||
-rw-r--r-- | 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) | |||
120 | for (op = reg->options; op->name; op++) { | 120 | for (op = reg->options; op->name; op++) { |
121 | char *alias = op->plugin_alias ? op->plugin_alias : op->file; | 121 | char *alias = op->plugin_alias ? op->plugin_alias : op->file; |
122 | char **temp = list; | 122 | char **temp = list; |
123 | int ret; | ||
123 | 124 | ||
124 | name = malloc(strlen(op->name) + strlen(alias) + 2); | 125 | ret = asprintf(&name, "%s:%s", alias, op->name); |
125 | if (!name) | 126 | if (ret < 0) |
126 | goto err; | 127 | goto err; |
127 | 128 | ||
128 | sprintf(name, "%s:%s", alias, op->name); | ||
129 | list = realloc(list, count + 2); | 129 | list = realloc(list, count + 2); |
130 | if (!list) { | 130 | if (!list) { |
131 | list = temp; | 131 | list = temp; |
@@ -290,17 +290,14 @@ load_plugin(struct pevent *pevent, const char *path, | |||
290 | const char *alias; | 290 | const char *alias; |
291 | char *plugin; | 291 | char *plugin; |
292 | void *handle; | 292 | void *handle; |
293 | int ret; | ||
293 | 294 | ||
294 | plugin = malloc(strlen(path) + strlen(file) + 2); | 295 | ret = asprintf(&plugin, "%s/%s", path, file); |
295 | if (!plugin) { | 296 | if (ret < 0) { |
296 | warning("could not allocate plugin memory\n"); | 297 | warning("could not allocate plugin memory\n"); |
297 | return; | 298 | return; |
298 | } | 299 | } |
299 | 300 | ||
300 | strcpy(plugin, path); | ||
301 | strcat(plugin, "/"); | ||
302 | strcat(plugin, file); | ||
303 | |||
304 | handle = dlopen(plugin, RTLD_NOW | RTLD_GLOBAL); | 301 | handle = dlopen(plugin, RTLD_NOW | RTLD_GLOBAL); |
305 | if (!handle) { | 302 | if (!handle) { |
306 | warning("could not load plugin '%s'\n%s\n", | 303 | warning("could not load plugin '%s'\n%s\n", |
@@ -391,6 +388,7 @@ load_plugins(struct pevent *pevent, const char *suffix, | |||
391 | char *home; | 388 | char *home; |
392 | char *path; | 389 | char *path; |
393 | char *envdir; | 390 | char *envdir; |
391 | int ret; | ||
394 | 392 | ||
395 | if (pevent->flags & PEVENT_DISABLE_PLUGINS) | 393 | if (pevent->flags & PEVENT_DISABLE_PLUGINS) |
396 | return; | 394 | return; |
@@ -421,16 +419,12 @@ load_plugins(struct pevent *pevent, const char *suffix, | |||
421 | if (!home) | 419 | if (!home) |
422 | return; | 420 | return; |
423 | 421 | ||
424 | path = malloc(strlen(home) + strlen(LOCAL_PLUGIN_DIR) + 2); | 422 | ret = asprintf(&path, "%s/%s", home, LOCAL_PLUGIN_DIR); |
425 | if (!path) { | 423 | if (ret < 0) { |
426 | warning("could not allocate plugin memory\n"); | 424 | warning("could not allocate plugin memory\n"); |
427 | return; | 425 | return; |
428 | } | 426 | } |
429 | 427 | ||
430 | strcpy(path, home); | ||
431 | strcat(path, "/"); | ||
432 | strcat(path, LOCAL_PLUGIN_DIR); | ||
433 | |||
434 | load_plugins_dir(pevent, suffix, path, load_plugin, data); | 428 | load_plugins_dir(pevent, suffix, path, load_plugin, data); |
435 | 429 | ||
436 | free(path); | 430 | 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, | |||
287 | sys_name = NULL; | 287 | sys_name = NULL; |
288 | } | 288 | } |
289 | 289 | ||
290 | reg = malloc(strlen(event_name) + 3); | 290 | ret = asprintf(®, "^%s$", event_name); |
291 | if (reg == NULL) | 291 | if (ret < 0) |
292 | return PEVENT_ERRNO__MEM_ALLOC_FAILED; | 292 | return PEVENT_ERRNO__MEM_ALLOC_FAILED; |
293 | 293 | ||
294 | sprintf(reg, "^%s$", event_name); | ||
295 | |||
296 | ret = regcomp(&ereg, reg, REG_ICASE|REG_NOSUB); | 294 | ret = regcomp(&ereg, reg, REG_ICASE|REG_NOSUB); |
297 | free(reg); | 295 | free(reg); |
298 | 296 | ||
@@ -300,13 +298,12 @@ find_event(struct pevent *pevent, struct event_list **events, | |||
300 | return PEVENT_ERRNO__INVALID_EVENT_NAME; | 298 | return PEVENT_ERRNO__INVALID_EVENT_NAME; |
301 | 299 | ||
302 | if (sys_name) { | 300 | if (sys_name) { |
303 | reg = malloc(strlen(sys_name) + 3); | 301 | ret = asprintf(®, "^%s$", sys_name); |
304 | if (reg == NULL) { | 302 | if (ret < 0) { |
305 | regfree(&ereg); | 303 | regfree(&ereg); |
306 | return PEVENT_ERRNO__MEM_ALLOC_FAILED; | 304 | return PEVENT_ERRNO__MEM_ALLOC_FAILED; |
307 | } | 305 | } |
308 | 306 | ||
309 | sprintf(reg, "^%s$", sys_name); | ||
310 | ret = regcomp(&sreg, reg, REG_ICASE|REG_NOSUB); | 307 | ret = regcomp(&sreg, reg, REG_ICASE|REG_NOSUB); |
311 | free(reg); | 308 | free(reg); |
312 | if (ret) { | 309 | if (ret) { |