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>
This commit is contained in:
Federico Vaga 2018-01-11 19:47:48 -05:00 committed by Arnaldo Carvalho de Melo
parent e877372880
commit 67dfc376f3
2 changed files with 13 additions and 22 deletions

View File

@ -120,12 +120,12 @@ char **traceevent_plugin_list_options(void)
for (op = reg->options; op->name; op++) { for (op = reg->options; op->name; op++) {
char *alias = op->plugin_alias ? op->plugin_alias : op->file; char *alias = op->plugin_alias ? op->plugin_alias : op->file;
char **temp = list; char **temp = list;
int ret;
name = malloc(strlen(op->name) + strlen(alias) + 2); ret = asprintf(&name, "%s:%s", alias, op->name);
if (!name) if (ret < 0)
goto err; goto err;
sprintf(name, "%s:%s", alias, op->name);
list = realloc(list, count + 2); list = realloc(list, count + 2);
if (!list) { if (!list) {
list = temp; list = temp;
@ -290,17 +290,14 @@ load_plugin(struct pevent *pevent, const char *path,
const char *alias; const char *alias;
char *plugin; char *plugin;
void *handle; void *handle;
int ret;
plugin = malloc(strlen(path) + strlen(file) + 2); ret = asprintf(&plugin, "%s/%s", path, file);
if (!plugin) { if (ret < 0) {
warning("could not allocate plugin memory\n"); warning("could not allocate plugin memory\n");
return; return;
} }
strcpy(plugin, path);
strcat(plugin, "/");
strcat(plugin, file);
handle = dlopen(plugin, RTLD_NOW | RTLD_GLOBAL); handle = dlopen(plugin, RTLD_NOW | RTLD_GLOBAL);
if (!handle) { if (!handle) {
warning("could not load plugin '%s'\n%s\n", warning("could not load plugin '%s'\n%s\n",
@ -391,6 +388,7 @@ load_plugins(struct pevent *pevent, const char *suffix,
char *home; char *home;
char *path; char *path;
char *envdir; char *envdir;
int ret;
if (pevent->flags & PEVENT_DISABLE_PLUGINS) if (pevent->flags & PEVENT_DISABLE_PLUGINS)
return; return;
@ -421,16 +419,12 @@ load_plugins(struct pevent *pevent, const char *suffix,
if (!home) if (!home)
return; return;
path = malloc(strlen(home) + strlen(LOCAL_PLUGIN_DIR) + 2); ret = asprintf(&path, "%s/%s", home, LOCAL_PLUGIN_DIR);
if (!path) { if (ret < 0) {
warning("could not allocate plugin memory\n"); warning("could not allocate plugin memory\n");
return; return;
} }
strcpy(path, home);
strcat(path, "/");
strcat(path, LOCAL_PLUGIN_DIR);
load_plugins_dir(pevent, suffix, path, load_plugin, data); load_plugins_dir(pevent, suffix, path, load_plugin, data);
free(path); free(path);

View File

@ -287,12 +287,10 @@ find_event(struct pevent *pevent, struct event_list **events,
sys_name = NULL; sys_name = NULL;
} }
reg = malloc(strlen(event_name) + 3); ret = asprintf(&reg, "^%s$", event_name);
if (reg == NULL) if (ret < 0)
return PEVENT_ERRNO__MEM_ALLOC_FAILED; return PEVENT_ERRNO__MEM_ALLOC_FAILED;
sprintf(reg, "^%s$", event_name);
ret = regcomp(&ereg, reg, REG_ICASE|REG_NOSUB); ret = regcomp(&ereg, reg, REG_ICASE|REG_NOSUB);
free(reg); free(reg);
@ -300,13 +298,12 @@ find_event(struct pevent *pevent, struct event_list **events,
return PEVENT_ERRNO__INVALID_EVENT_NAME; return PEVENT_ERRNO__INVALID_EVENT_NAME;
if (sys_name) { if (sys_name) {
reg = malloc(strlen(sys_name) + 3); ret = asprintf(&reg, "^%s$", sys_name);
if (reg == NULL) { if (ret < 0) {
regfree(&ereg); regfree(&ereg);
return PEVENT_ERRNO__MEM_ALLOC_FAILED; return PEVENT_ERRNO__MEM_ALLOC_FAILED;
} }
sprintf(reg, "^%s$", sys_name);
ret = regcomp(&sreg, reg, REG_ICASE|REG_NOSUB); ret = regcomp(&sreg, reg, REG_ICASE|REG_NOSUB);
free(reg); free(reg);
if (ret) { if (ret) {