From 3b88065266136f13f1b927c972e9f71eaea6f558 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Helleu?= Date: Tue, 17 Dec 2024 21:06:05 +0100 Subject: [PATCH] trigger: replace calls to malloc by weechat_asprintf --- src/plugins/trigger/trigger-callback.c | 23 +++++++---------------- src/plugins/trigger/trigger-completion.c | 15 +++++---------- src/plugins/trigger/trigger-config.c | 14 ++++++-------- 3 files changed, 18 insertions(+), 34 deletions(-) diff --git a/src/plugins/trigger/trigger-callback.c b/src/plugins/trigger/trigger-callback.c index a6c52628f..fac863232 100644 --- a/src/plugins/trigger/trigger-callback.c +++ b/src/plugins/trigger/trigger-callback.c @@ -811,7 +811,7 @@ trigger_callback_modifier_cb (const void *pointer, void *data, const char *ptr_string; char *string_modified, *pos, *buffer_pointer; char *str_tags, **tags, *prefix, *string_no_color; - int length, num_tags, rc; + int num_tags, rc; void *ptr_irc_server, *ptr_irc_channel; struct t_gui_buffer *ptr_buffer; @@ -950,11 +950,8 @@ trigger_callback_modifier_cb (const void *pointer, void *data, | WEECHAT_STRING_SPLIT_COLLAPSE_SEPS, 0, &num_tags); - length = 1 + strlen (pos) + 1 + 1; - str_tags = malloc (length); - if (str_tags) + if (weechat_asprintf (&str_tags, ",%s,", pos) >= 0) { - snprintf (str_tags, length, ",%s,", pos); weechat_hashtable_set (ctx.extra_vars, "tg_tags", str_tags); free (str_tags); @@ -1003,7 +1000,7 @@ trigger_callback_line_cb (const void *pointer, void *data, void *ptr; const char *ptr_key, *ptr_value; char **tags, *str_tags, *string_no_color; - int rc, num_tags, length; + int rc, num_tags; TRIGGER_CALLBACK_CB_INIT(NULL); @@ -1041,12 +1038,10 @@ trigger_callback_line_cb (const void *pointer, void *data, &num_tags); /* build string with tags and commas around: ",tag1,tag2,tag3," */ - length = 1 + strlen ((ptr_value) ? ptr_value : "") + 1 + 1; - str_tags = malloc (length); - if (str_tags) + if (weechat_asprintf (&str_tags, + ",%s,", + (ptr_value) ? ptr_value : "") >= 0) { - snprintf (str_tags, length, ",%s,", - (ptr_value) ? ptr_value : ""); weechat_hashtable_set (ctx.extra_vars, "tags", str_tags); free (str_tags); } @@ -1140,7 +1135,6 @@ trigger_callback_print_cb (const void *pointer, void *data, const char *message) { char *str_tags, *str_tags2, str_temp[128], *str_no_color; - int length; struct timeval tv; TRIGGER_CALLBACK_CB_INIT(WEECHAT_RC_OK); @@ -1185,11 +1179,8 @@ trigger_callback_print_cb (const void *pointer, void *data, if (str_tags) { /* build string with tags and commas around: ",tag1,tag2,tag3," */ - length = 1 + strlen (str_tags) + 1 + 1; - str_tags2 = malloc (length); - if (str_tags2) + if (weechat_asprintf (&str_tags2, ",%s,", str_tags) >= 0) { - snprintf (str_tags2, length, ",%s,", str_tags); weechat_hashtable_set (ctx.extra_vars, "tg_tags", str_tags2); free (str_tags2); } diff --git a/src/plugins/trigger/trigger-completion.c b/src/plugins/trigger/trigger-completion.c index 30550f156..22c2f0134 100644 --- a/src/plugins/trigger/trigger-completion.c +++ b/src/plugins/trigger/trigger-completion.c @@ -307,17 +307,12 @@ trigger_completion_add_quoted_word (struct t_gui_completion *completion, const char *word) { char *temp; - int length; - length = 1 + strlen (word) + 1 + 1; - temp = malloc (length); - if (!temp) - return; - - snprintf (temp, length, "\"%s\"", word); - weechat_completion_list_add (completion, temp, 0, WEECHAT_LIST_POS_END); - - free (temp); + if (weechat_asprintf (&temp, "\"%s\"", word) >= 0) + { + weechat_completion_list_add (completion, temp, 0, WEECHAT_LIST_POS_END); + free (temp); + } } /* diff --git a/src/plugins/trigger/trigger-config.c b/src/plugins/trigger/trigger-config.c index 7d2313bad..dbe30e0c1 100644 --- a/src/plugins/trigger/trigger-config.c +++ b/src/plugins/trigger/trigger-config.c @@ -345,19 +345,17 @@ trigger_config_create_trigger_option (const char *trigger_name, int index_option const char *value) { struct t_config_option *ptr_option; - int length; char *option_name; ptr_option = NULL; - length = strlen (trigger_name) + 1 + - strlen (trigger_option_string[index_option]) + 1; - option_name = malloc (length); - if (!option_name) + if (weechat_asprintf (&option_name, + "%s.%s", + trigger_name, + trigger_option_string[index_option]) < 0) + { return NULL; - - snprintf (option_name, length, "%s.%s", - trigger_name, trigger_option_string[index_option]); + } switch (index_option) {