From 818a4c95a926bb4863b55d74cf818a40bcddce69 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Helleu?= Date: Tue, 17 Dec 2024 19:11:02 +0100 Subject: [PATCH] core: replace calls to malloc by string_asprintf --- src/core/core-command.c | 131 +++++++++------------ src/core/core-completion.c | 66 ++++------- src/core/core-config-file.c | 156 ++++++++++-------------- src/core/core-dir.c | 26 ++-- src/core/core-eval.c | 4 +- src/core/core-log.c | 61 +++++----- src/core/core-network.c | 40 +++---- src/core/core-proxy.c | 14 +-- src/core/core-string.c | 22 +--- src/core/core-upgrade-file.c | 9 +- src/core/core-upgrade.c | 12 +- src/core/hook/hook-command-run.c | 7 +- src/core/hook/hook-focus.c | 19 ++- src/gui/curses/gui-curses-chat.c | 47 +++----- src/gui/gui-bar-item-custom.c | 14 +-- src/gui/gui-bar-item.c | 50 +++----- src/gui/gui-bar.c | 18 +-- src/gui/gui-buffer.c | 14 +-- src/gui/gui-chat.c | 196 ++++++++++++++----------------- src/gui/gui-nicklist.c | 12 +- src/plugins/plugin-config.c | 28 ++--- src/plugins/plugin.c | 86 ++++++-------- 22 files changed, 409 insertions(+), 623 deletions(-) diff --git a/src/core/core-command.c b/src/core/core-command.c index 8b7fb2553..2557ab422 100644 --- a/src/core/core-command.c +++ b/src/core/core-command.c @@ -1841,7 +1841,7 @@ COMMAND_CALLBACK(color) COMMAND_CALLBACK(command) { - int length, index_args, any_plugin; + int index_args, any_plugin; char *command, **commands, **ptr_command; struct t_weechat_plugin *ptr_plugin; struct t_gui_buffer *ptr_buffer; @@ -1912,11 +1912,8 @@ COMMAND_CALLBACK(command) } else { - length = strlen (argv_eol[index_args + 1]) + 2; - command = malloc (length); - if (command) + if (string_asprintf (&command, "/%s", argv_eol[index_args + 1]) >= 0) { - snprintf (command, length, "/%s", argv_eol[index_args + 1]); (void) input_exec_command (ptr_buffer, any_plugin, ptr_plugin, command, NULL); free (command); @@ -3079,8 +3076,8 @@ COMMAND_CALLBACK(help) struct t_weechat_plugin *ptr_plugin; struct t_config_option *ptr_option; int i, length, command_found, first_line_displayed, verbose; - char *string, *ptr_string, *pos_double_pipe, *pos_end, *args_desc; - char empty_string[1] = { '\0' }, str_format[64]; + char *string, *ptr_string, **string_values, *pos_double_pipe, *pos_end; + char *args_desc, empty_string[1] = { '\0' }, str_format[64]; /* make C compiler happy */ (void) pointer; @@ -3370,63 +3367,53 @@ COMMAND_CALLBACK(help) } break; case CONFIG_OPTION_TYPE_ENUM: - length = 0; - i = 0; - while (ptr_option->string_values[i]) + string_values = string_dyn_alloc (256); + if (string_values) { - length += strlen (ptr_option->string_values[i]) + 5; - i++; - } - if (length > 0) - { - string = malloc (length); - if (string) + i = 0; + while (ptr_option->string_values[i]) { - string[0] = '\0'; - i = 0; - while (ptr_option->string_values[i]) - { - strcat (string, "\""); - strcat (string, ptr_option->string_values[i]); - strcat (string, "\""); - if (ptr_option->string_values[i + 1]) - strcat (string, ", "); - i++; - } - gui_chat_printf (NULL, " %s: %s", - _("type"), _("enum")); - gui_chat_printf (NULL, " %s: %s", - _("values"), string); - if (ptr_option->default_value) - { - gui_chat_printf (NULL, " %s: \"%s\"", - _("default value"), - ptr_option->string_values[CONFIG_ENUM_DEFAULT(ptr_option)]); - } - else - { - gui_chat_printf (NULL, " %s: %s", - _("default value"), - _("(undefined)")); - } - if (ptr_option->value) - { - gui_chat_printf (NULL, - " %s: \"%s%s%s\"", - _("current value"), - GUI_COLOR(GUI_COLOR_CHAT_VALUE), - ptr_option->string_values[CONFIG_ENUM(ptr_option)], - GUI_COLOR(GUI_COLOR_CHAT)); - } - else - { - gui_chat_printf (NULL, - " %s: %s", - _("current value"), - _("(undefined)")); - } - free (string); + string_dyn_concat (string_values, "\"", -1); + string_dyn_concat (string_values, + ptr_option->string_values[i], -1); + string_dyn_concat (string_values, "\"", -1); + if (ptr_option->string_values[i + 1]) + string_dyn_concat (string_values, ", ", -1); + i++; } + gui_chat_printf (NULL, " %s: %s", _("type"), _("enum")); + gui_chat_printf (NULL, " %s: %s", _("values"), *string_values); + if (ptr_option->default_value) + { + gui_chat_printf ( + NULL, " %s: \"%s\"", + _("default value"), + ptr_option->string_values[CONFIG_ENUM_DEFAULT(ptr_option)]); + } + else + { + gui_chat_printf (NULL, " %s: %s", + _("default value"), + _("(undefined)")); + } + if (ptr_option->value) + { + gui_chat_printf ( + NULL, + " %s: \"%s%s%s\"", + _("current value"), + GUI_COLOR(GUI_COLOR_CHAT_VALUE), + ptr_option->string_values[CONFIG_ENUM(ptr_option)], + GUI_COLOR(GUI_COLOR_CHAT)); + } + else + { + gui_chat_printf (NULL, + " %s: %s", + _("current value"), + _("(undefined)")); + } + string_dyn_free (string_values, 1); } break; case CONFIG_NUM_OPTION_TYPES: @@ -4874,7 +4861,7 @@ COMMAND_CALLBACK(mouse) COMMAND_CALLBACK(mute) { - int length, mute_mode, gui_chat_mute_old; + int mute_mode, gui_chat_mute_old; char *command, *ptr_command; struct t_gui_buffer *mute_buffer, *ptr_buffer, *gui_chat_mute_buffer_old; @@ -4939,11 +4926,8 @@ COMMAND_CALLBACK(mute) } else { - length = strlen (ptr_command) + 2; - command = malloc (length); - if (command) + if (string_asprintf (&command, "/%s", ptr_command) >= 0) { - snprintf (command, length, "/%s", ptr_command); (void) input_exec_command (buffer, 1, NULL, command, NULL); free (command); } @@ -6722,7 +6706,7 @@ int command_set_display_option_list (const char *message, const char *search, int display_only_changed) { - int number_found, section_displayed, length; + int number_found, section_displayed; struct t_config_file *ptr_config; struct t_config_section *ptr_section; struct t_config_option *ptr_option; @@ -6755,15 +6739,12 @@ command_set_display_option_list (const char *message, const char *search, !config_file_option_has_changed (ptr_option)) continue; - length = strlen (ptr_config->name) + 1 - + strlen (ptr_section->name) + 1 - + strlen (ptr_option->name) + 1; - option_full_name = malloc (length); - if (option_full_name) + if (string_asprintf (&option_full_name, + "%s.%s.%s", + ptr_config->name, + ptr_section->name, + ptr_option->name) >= 0) { - snprintf (option_full_name, length, "%s.%s.%s", - ptr_config->name, ptr_section->name, - ptr_option->name); if ((!search) || (search && search[0] && (string_match (option_full_name, search, 1)))) diff --git a/src/core/core-completion.c b/src/core/core-completion.c index fd5bd05c7..98ef67042 100644 --- a/src/core/core-completion.c +++ b/src/core/core-completion.c @@ -71,17 +71,12 @@ completion_list_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); - gui_completion_list_add (completion, temp, 0, WEECHAT_LIST_POS_END); - - free (temp); + if (string_asprintf (&temp, "\"%s\"", word) >= 0) + { + gui_completion_list_add (completion, temp, 0, WEECHAT_LIST_POS_END); + free (temp); + } } /* @@ -1221,7 +1216,6 @@ completion_list_add_config_options_cb (const void *pointer, void *data, struct t_config_file *ptr_config; struct t_config_section *ptr_section; struct t_config_option *ptr_option; - int length; char *option_full_name; /* make C compiler happy */ @@ -1239,15 +1233,12 @@ completion_list_add_config_options_cb (const void *pointer, void *data, for (ptr_option = ptr_section->options; ptr_option; ptr_option = ptr_option->next_option) { - length = strlen (ptr_config->name) + 1 - + strlen (ptr_section->name) + 1 - + strlen (ptr_option->name) + 1; - option_full_name = malloc (length); - if (option_full_name) + if (string_asprintf (&option_full_name, + "%s.%s.%s", + ptr_config->name, + ptr_section->name, + ptr_option->name) >= 0) { - snprintf (option_full_name, length, "%s.%s.%s", - ptr_config->name, ptr_section->name, - ptr_option->name); gui_completion_list_add (completion, option_full_name, 0, WEECHAT_LIST_POS_SORT); @@ -1334,7 +1325,6 @@ completion_list_add_plugins_installed_cb (const void *pointer, void *data, struct t_gui_completion *completion) { char *plugin_path, *dir_name, *extra_libdir; - int length; struct t_hashtable *options; /* make C compiler happy */ @@ -1347,11 +1337,8 @@ completion_list_add_plugins_installed_cb (const void *pointer, void *data, extra_libdir = getenv (WEECHAT_EXTRA_LIBDIR); if (extra_libdir && extra_libdir[0]) { - length = strlen (extra_libdir) + 16 + 1; - dir_name = malloc (length); - if (dir_name) + if (string_asprintf (&dir_name, "%s/plugins", extra_libdir) >= 0) { - snprintf (dir_name, length, "%s/plugins", extra_libdir); dir_exec_on_files (dir_name, 1, 0, &completion_list_add_plugins_installed_exec_cb, completion); @@ -1383,11 +1370,8 @@ completion_list_add_plugins_installed_cb (const void *pointer, void *data, } /* plugins in WeeChat global lib dir */ - length = strlen (WEECHAT_LIBDIR) + 16 + 1; - dir_name = malloc (length); - if (dir_name) + if (string_asprintf (&dir_name, "%s/plugins", WEECHAT_LIBDIR) >= 0) { - snprintf (dir_name, length, "%s/plugins", WEECHAT_LIBDIR); dir_exec_on_files (dir_name, 1, 0, &completion_list_add_plugins_installed_exec_cb, completion); @@ -1500,7 +1484,6 @@ completion_list_add_config_option_values_cb (const void *pointer, void *data, char *pos_space, *option_full_name, *pos_section, *pos_option; char *file, *section, *value_string, **ptr_value; const char *color_name; - int length; struct t_config_file *ptr_config; struct t_config_section *ptr_section, *section_found; struct t_config_option *option_found; @@ -1587,12 +1570,11 @@ completion_list_add_config_option_values_cb (const void *pointer, void *data, 0, WEECHAT_LIST_POS_BEGINNING); if (option_found->value) { - length = 64; - value_string = malloc (length); - if (value_string) + if (string_asprintf ( + &value_string, + "%d", + CONFIG_INTEGER(option_found)) >= 0) { - snprintf (value_string, length, - "%d", CONFIG_INTEGER(option_found)); gui_completion_list_add (completion, value_string, 0, WEECHAT_LIST_POS_BEGINNING); @@ -1612,13 +1594,11 @@ completion_list_add_config_option_values_cb (const void *pointer, void *data, 0, WEECHAT_LIST_POS_BEGINNING); if (option_found->value) { - length = strlen (CONFIG_STRING(option_found)) + 2 + 1; - value_string = malloc (length); - if (value_string) + if (string_asprintf ( + &value_string, + "\"%s\"", + CONFIG_STRING(option_found)) >= 0) { - snprintf (value_string, length, - "\"%s\"", - CONFIG_STRING(option_found)); gui_completion_list_add (completion, value_string, 0, WEECHAT_LIST_POS_BEGINNING); @@ -2126,17 +2106,13 @@ completion_list_map_eval_buffer_local_variable_cb (void *data, const void *key, const void *value) { char *name; - int length; /* make C compiler happy */ (void) hashtable; (void) value; - length = strlen (key) + 3 + 1; - name = malloc (length); - if (name) + if (string_asprintf (&name, "${%s}", (const char *)key) >= 0) { - snprintf (name, length, "${%s}", (const char *)key); gui_completion_list_add ((struct t_gui_completion *)data, name, 0, WEECHAT_LIST_POS_SORT); free (name); diff --git a/src/core/core-config-file.c b/src/core/core-config-file.c index 6e43837df..27d5ad5d9 100644 --- a/src/core/core-config-file.c +++ b/src/core/core-config-file.c @@ -201,8 +201,7 @@ config_file_new (struct t_weechat_plugin *plugin, const char *name, { struct t_config_file *new_config_file; const char *ptr_name; - char *filename; - int priority, length; + int priority; string_get_priority_and_name (name, &priority, &ptr_name, CONFIG_PRIORITY_DEFAULT); @@ -225,16 +224,7 @@ config_file_new (struct t_weechat_plugin *plugin, const char *name, free (new_config_file); return NULL; } - new_config_file->filename = NULL; - length = strlen (ptr_name) + 8 + 1; - filename = malloc (length); - if (filename) - { - snprintf (filename, length, "%s.conf", ptr_name); - new_config_file->filename = strdup (filename); - free (filename); - } - if (!new_config_file->filename) + if (string_asprintf (&new_config_file->filename, "%s.conf", ptr_name) < 0) { free (new_config_file->name); free (new_config_file); @@ -501,23 +491,16 @@ config_file_search_section (struct t_config_file *config_file, char * config_file_option_full_name (struct t_config_option *option) { - int length_option; char *option_full_name; if (!option) return NULL; - length_option = strlen (option->config_file->name) + 1 + - strlen (option->section->name) + 1 + strlen (option->name) + 1; - option_full_name = malloc (length_option); - if (option_full_name) - { - snprintf (option_full_name, length_option, - "%s.%s.%s", - option->config_file->name, - option->section->name, - option->name); - } + string_asprintf (&option_full_name, + "%s.%s.%s", + option->config_file->name, + option->section->name, + option->name); return option_full_name; } @@ -2373,7 +2356,7 @@ config_file_option_value_to_string (struct t_config_option *option, { char *value; const char *ptr_value; - int enabled, length; + int enabled; if (!option) return NULL; @@ -2381,14 +2364,11 @@ config_file_option_value_to_string (struct t_config_option *option, if ((default_value && !option->default_value) || (!default_value && !option->value)) { - length = 7 + ((use_colors) ? 64 : 0) + 1; - value = malloc (length); - if (!value) - return NULL; - snprintf (value, length, - "%s%s", - (use_colors) ? GUI_COLOR(GUI_COLOR_CHAT_VALUE_NULL) : "", - "null"); + string_asprintf ( + &value, + "%s%s", + (use_colors) ? GUI_COLOR(GUI_COLOR_CHAT_VALUE_NULL) : "", + "null"); return value; } @@ -2397,66 +2377,51 @@ config_file_option_value_to_string (struct t_config_option *option, case CONFIG_OPTION_TYPE_BOOLEAN: enabled = (default_value) ? CONFIG_BOOLEAN_DEFAULT(option) : CONFIG_BOOLEAN(option); - length = 7 + ((use_colors) ? 64 : 0) + 1; - value = malloc (length); - if (!value) - return NULL; - snprintf (value, length, - "%s%s", - (use_colors) ? GUI_COLOR(GUI_COLOR_CHAT_VALUE) : "", - (enabled) ? "on" : "off"); + string_asprintf ( + &value, + "%s%s", + (use_colors) ? GUI_COLOR(GUI_COLOR_CHAT_VALUE) : "", + (enabled) ? "on" : "off"); return value; case CONFIG_OPTION_TYPE_INTEGER: - length = 31 + ((use_colors) ? 64 : 0) + 1; - value = malloc (length); - if (!value) - return NULL; - snprintf (value, length, - "%s%d", - (use_colors) ? GUI_COLOR(GUI_COLOR_CHAT_VALUE) : "", - (default_value) ? CONFIG_INTEGER_DEFAULT(option) : CONFIG_INTEGER(option)); + string_asprintf ( + &value, + "%s%d", + (use_colors) ? GUI_COLOR(GUI_COLOR_CHAT_VALUE) : "", + (default_value) ? CONFIG_INTEGER_DEFAULT(option) : CONFIG_INTEGER(option)); return value; case CONFIG_OPTION_TYPE_STRING: ptr_value = (default_value) ? CONFIG_STRING_DEFAULT(option) : CONFIG_STRING(option); - length = strlen (ptr_value) + ((use_colors) ? 64 : 0) + 1; - value = malloc (length); - if (!value) - return NULL; - snprintf (value, length, - "%s%s%s%s%s%s", - (use_colors && use_delimiters) ? GUI_COLOR(GUI_COLOR_CHAT_DELIMITERS) : "", - (use_delimiters) ? "\"" : "", - (use_colors) ? GUI_COLOR(GUI_COLOR_CHAT_VALUE) : "", - ptr_value, - (use_colors && use_delimiters) ? GUI_COLOR(GUI_COLOR_CHAT_DELIMITERS) : "", - (use_delimiters) ? "\"" : ""); + string_asprintf ( + &value, + "%s%s%s%s%s%s", + (use_colors && use_delimiters) ? GUI_COLOR(GUI_COLOR_CHAT_DELIMITERS) : "", + (use_delimiters) ? "\"" : "", + (use_colors) ? GUI_COLOR(GUI_COLOR_CHAT_VALUE) : "", + ptr_value, + (use_colors && use_delimiters) ? GUI_COLOR(GUI_COLOR_CHAT_DELIMITERS) : "", + (use_delimiters) ? "\"" : ""); return value; case CONFIG_OPTION_TYPE_COLOR: ptr_value = gui_color_get_name ( (default_value) ? CONFIG_COLOR_DEFAULT(option) : CONFIG_COLOR(option)); if (!ptr_value) return NULL; - length = strlen (ptr_value) + ((use_colors) ? 64 : 0) + 1; - value = malloc (length); - if (!value) - return NULL; - snprintf (value, length, - "%s%s", - (use_colors) ? GUI_COLOR(GUI_COLOR_CHAT_VALUE) : "", - ptr_value); + string_asprintf ( + &value, + "%s%s", + (use_colors) ? GUI_COLOR(GUI_COLOR_CHAT_VALUE) : "", + ptr_value); return value; case CONFIG_OPTION_TYPE_ENUM: ptr_value = (default_value) ? option->string_values[CONFIG_ENUM_DEFAULT(option)] : - option->string_values[CONFIG_ENUM(option)]; - length = strlen (ptr_value) + ((use_colors) ? 64 : 0) + 1; - value = malloc (length); - if (!value) - return NULL; - snprintf (value, length, - "%s%s", - (use_colors) ? GUI_COLOR(GUI_COLOR_CHAT_VALUE) : "", - ptr_value); + option->string_values[CONFIG_ENUM(option)]; + string_asprintf ( + &value, + "%s%s", + (use_colors) ? GUI_COLOR(GUI_COLOR_CHAT_VALUE) : "", + ptr_value); return value; case CONFIG_NUM_OPTION_TYPES: /* make C compiler happy */ @@ -3187,7 +3152,7 @@ int config_file_write_internal (struct t_config_file *config_file, int default_options) { - int filename_length, rc; + int rc; long file_perms; char *filename, *filename2, resolved_path[PATH_MAX], *error; struct t_config_section *ptr_section; @@ -3197,25 +3162,24 @@ config_file_write_internal (struct t_config_file *config_file, return WEECHAT_CONFIG_WRITE_ERROR; /* build filename */ - filename_length = strlen (weechat_config_dir) + strlen (DIR_SEPARATOR) + - strlen (config_file->filename) + 1; - filename = malloc (filename_length); - if (!filename) + if (string_asprintf (&filename, + "%s%s%s", + weechat_config_dir, + DIR_SEPARATOR, + config_file->filename) < 0) + { return WEECHAT_CONFIG_WRITE_MEMORY_ERROR; - snprintf (filename, filename_length, "%s%s%s", - weechat_config_dir, DIR_SEPARATOR, config_file->filename); + } /* * build temporary filename, this temp file will be renamed to filename * after write */ - filename2 = malloc (filename_length + 32); - if (!filename2) + if (string_asprintf (&filename2, "%s.weechattmp", filename) < 0) { free (filename); return WEECHAT_CONFIG_WRITE_MEMORY_ERROR; } - snprintf (filename2, filename_length + 32, "%s.weechattmp", filename); /* if filename is a symbolic link, use target as filename */ if (realpath (filename, resolved_path)) @@ -3624,8 +3588,7 @@ config_file_update_data_read (struct t_config_file *config_file, int config_file_read_internal (struct t_config_file *config_file, int reload) { - int filename_length, line_number, rc, length, version; - int warning_update_displayed; + int line_number, rc, length, version, warning_update_displayed; char *filename, *section, *option, *value; struct t_config_section *ptr_section; struct t_config_option *ptr_option; @@ -3638,13 +3601,14 @@ config_file_read_internal (struct t_config_file *config_file, int reload) warning_update_displayed = 0; /* build filename */ - filename_length = strlen (weechat_config_dir) + strlen (DIR_SEPARATOR) + - strlen (config_file->filename) + 1; - filename = malloc (filename_length); - if (!filename) + if (string_asprintf (&filename, + "%s%s%s", + weechat_config_dir, + DIR_SEPARATOR, + config_file->filename) < 0) + { return WEECHAT_CONFIG_READ_MEMORY_ERROR; - snprintf (filename, filename_length, "%s%s%s", - weechat_config_dir, DIR_SEPARATOR, config_file->filename); + } /* create file with default options if it does not exist */ if (access (filename, F_OK) != 0) diff --git a/src/core/core-dir.c b/src/core/core-dir.c index 4dfb2ec42..c07fd9417 100644 --- a/src/core/core-dir.c +++ b/src/core/core-dir.c @@ -109,7 +109,7 @@ int dir_mkdir_home (const char *directory, int mode) { char *dir, *dir1, *dir2, *dir3, *dir4, *dir5; - int rc, dir_length; + int rc; rc = 0; dir = NULL; @@ -129,11 +129,8 @@ dir_mkdir_home (const char *directory, int mode) else { /* build directory in data dir by default */ - dir_length = strlen (weechat_data_dir) + strlen (directory) + 2; - dir = malloc (dir_length); - if (!dir) + if (string_asprintf (&dir, "%s/%s", weechat_data_dir, directory) < 0) goto end; - snprintf (dir, dir_length, "%s/%s", weechat_data_dir, directory); } dir1 = string_replace (dir, "${weechat_config_dir}", weechat_config_dir); @@ -386,7 +383,7 @@ int dir_create_home_temp_dir () { char *temp_dir, *temp_home_template, *ptr_weechat_home; - int rc, length, add_separator; + int rc, add_separator; rc = 0; temp_dir = NULL; @@ -396,16 +393,15 @@ dir_create_home_temp_dir () if (!temp_dir || !temp_dir[0]) goto memory_error; - length = strlen (temp_dir) + 32 + 1; - temp_home_template = malloc (length); - if (!temp_home_template) - goto memory_error; - add_separator = (temp_dir[strlen (temp_dir) - 1] != DIR_SEPARATOR_CHAR); - snprintf (temp_home_template, length, - "%s%sweechat_temp_XXXXXX", - temp_dir, - add_separator ? DIR_SEPARATOR : ""); + + if (string_asprintf (&temp_home_template, + "%s%sweechat_temp_XXXXXX", + temp_dir, + (add_separator) ? DIR_SEPARATOR : "") < 0) + { + goto memory_error; + } ptr_weechat_home = mkdtemp (temp_home_template); if (!ptr_weechat_home) { diff --git a/src/core/core-eval.c b/src/core/core-eval.c index 119f439b4..0e415f828 100644 --- a/src/core/core-eval.c +++ b/src/core/core-eval.c @@ -2524,11 +2524,9 @@ eval_replace_regex (const char *string, regex_t *regex, const char *replace, if (!string || !regex || !replace) goto end; - length = strlen (string) + 1; - result = malloc (length); + result = strdup (string); if (!result) goto end; - snprintf (result, length, "%s", string); eval_context->regex = &eval_regex; eval_context->regex_replacement_index = 1; diff --git a/src/core/core-log.c b/src/core/core-log.c index 2ad0b3213..af3678403 100644 --- a/src/core/core-log.c +++ b/src/core/core-log.c @@ -62,8 +62,6 @@ int weechat_log_use_time = 1; /* 0 to temporary disable time in log, */ int log_open (const char *filename, const char *mode) { - int filename_length; - /* exit if log already opened */ if (weechat_log_file) return 0; @@ -75,17 +73,18 @@ log_open (const char *filename, const char *mode) else if (filename) { weechat_log_filename = strdup (filename); - weechat_log_file = fopen (weechat_log_filename, mode); } else { - filename_length = strlen (weechat_state_dir) + 64; - weechat_log_filename = malloc (filename_length); - snprintf (weechat_log_filename, filename_length, - "%s/%s", weechat_state_dir, WEECHAT_LOG_NAME); - weechat_log_file = fopen (weechat_log_filename, mode); + string_asprintf (&weechat_log_filename, + "%s/%s", weechat_state_dir, WEECHAT_LOG_NAME); } + if (!weechat_log_filename) + return 0; + + weechat_log_file = fopen (weechat_log_filename, mode); + if (!weechat_log_file) { if (weechat_log_filename) @@ -260,7 +259,6 @@ int log_crash_rename () { char *old_name, *new_name; - int length; time_t time_now; struct tm *local_time; @@ -273,32 +271,33 @@ log_crash_rename () log_close (); - length = strlen (weechat_state_dir) + 128; - new_name = malloc (length); - if (new_name) + time_now = time (NULL); + local_time = localtime (&time_now); + if (string_asprintf (&new_name, + "%s/weechat_crash_%04d%02d%02d_%d.log", + weechat_state_dir, + local_time->tm_year + 1900, + local_time->tm_mon + 1, + local_time->tm_mday, + getpid ()) < 0) { - time_now = time (NULL); - local_time = localtime (&time_now); - snprintf (new_name, length, - "%s/weechat_crash_%04d%02d%02d_%d.log", - weechat_state_dir, - local_time->tm_year + 1900, - local_time->tm_mon + 1, - local_time->tm_mday, - getpid ()); - if (rename (old_name, new_name) == 0) - { - string_fprintf (stderr, "*** Full crash dump was saved to %s file.\n", - new_name); - log_open (new_name, "a"); - free (old_name); - free (new_name); - return 1; - } - free (new_name); + return 0; } + if (rename (old_name, new_name) == 0) + { + string_fprintf (stderr, "*** Full crash dump was saved to %s file.\n", + new_name); + log_open (new_name, "a"); + free (old_name); + free (new_name); + return 1; + } + + free (new_name); free (old_name); + log_open (NULL, "a"); + return 0; } diff --git a/src/core/core-network.c b/src/core/core-network.c index f40ca9b8c..5a538e372 100644 --- a/src/core/core-network.c +++ b/src/core/core-network.c @@ -930,7 +930,7 @@ network_connect_child (struct t_hook *hook_connect) char remote_address[NI_MAXHOST + 1]; char status_without_string[1 + 5 + 1]; const char *error; - int rc, length, num_written; + int rc, num_written; int sock, set, flags, j; struct msghdr msg; struct cmsghdr *cmsg; @@ -1042,14 +1042,11 @@ network_connect_child (struct t_hook *hook_connect) error = gai_strerror (rc); if (error) { - length = 1 + 5 + strlen (error) + 1; - status_with_string = malloc (length); - if (status_with_string) - { - snprintf (status_with_string, length, "%c%05d%s", - '0' + WEECHAT_HOOK_CONNECT_ADDRESS_NOT_FOUND, - (int)strlen (error), error); - } + string_asprintf (&status_with_string, + "%c%05d%s", + '0' + WEECHAT_HOOK_CONNECT_ADDRESS_NOT_FOUND, + (int)strlen (error), + error); } if (status_with_string) { @@ -1097,14 +1094,11 @@ network_connect_child (struct t_hook *hook_connect) error = gai_strerror (rc); if (error) { - length = 1 + 5 + strlen (error) + 1; - status_with_string = malloc (length); - if (status_with_string) - { - snprintf (status_with_string, length, "%c%05d%s", - '0' + WEECHAT_HOOK_CONNECT_LOCAL_HOSTNAME_ERROR, - (int)strlen (error), error); - } + string_asprintf (&status_with_string, + "%c%05d%s", + '0' + WEECHAT_HOOK_CONNECT_LOCAL_HOSTNAME_ERROR, + (int)strlen (error), + error); } if (status_with_string) { @@ -1375,13 +1369,11 @@ network_connect_child (struct t_hook *hook_connect) status_with_string = NULL; if (ptr_address) { - length = strlen (status_str) + 5 + strlen (ptr_address) + 1; - status_with_string = malloc (length); - if (status_with_string) - { - snprintf (status_with_string, length, "%s%05d%s", - status_str, (int)strlen (ptr_address), ptr_address); - } + string_asprintf (&status_with_string, + "%s%05d%s", + status_str, + (int)strlen (ptr_address), + ptr_address); } if (status_with_string) diff --git a/src/core/core-proxy.c b/src/core/core-proxy.c index b464bdf96..304b4178f 100644 --- a/src/core/core-proxy.c +++ b/src/core/core-proxy.c @@ -254,19 +254,17 @@ proxy_create_option (const char *proxy_name, int index_option, const char *value) { struct t_config_option *ptr_option; - int length; char *option_name; ptr_option = NULL; - length = strlen (proxy_name) + 1 + - strlen (proxy_option_string[index_option]) + 1; - option_name = malloc (length); - if (!option_name) + if (string_asprintf (&option_name, + "%s.%s", + proxy_name, + proxy_option_string[index_option]) < 0) + { return NULL; - - snprintf (option_name, length, "%s.%s", - proxy_name, proxy_option_string[index_option]); + } switch (index_option) { diff --git a/src/core/core-string.c b/src/core/core-string.c index dbf22c208..ec505bf94 100644 --- a/src/core/core-string.c +++ b/src/core/core-string.c @@ -1127,7 +1127,6 @@ char * string_expand_home (const char *path) { char *ptr_home, *str; - int length; if (!path) return NULL; @@ -1142,12 +1141,7 @@ string_expand_home (const char *path) if (!ptr_home) return NULL; - length = strlen (ptr_home) + strlen (path + 1) + 1; - str = malloc (length); - if (!str) - return strdup (path); - - snprintf (str, length, "%s%s", ptr_home, path + 1); + string_asprintf (&str, "%s%s", ptr_home, path + 1); return str; } @@ -1171,7 +1165,6 @@ string_eval_path_home (const char *path, { char *path1, *path2, *path3; const char *ptr_option_directory, *ptr_directory; - int length; if (!path) return NULL; @@ -1201,13 +1194,12 @@ string_eval_path_home (const char *path, else if (strcmp (ptr_option_directory, "runtime") == 0) ptr_directory = weechat_runtime_dir; } - length = strlen (ptr_directory) + strlen (path + 2) + 1; - path1 = malloc (length); - if (path1) - snprintf (path1, length, "%s%s", ptr_directory, path + 2); + string_asprintf (&path1, "%s%s", ptr_directory, path + 2); } else + { path1 = strdup (path); + } if (!path1) goto end; @@ -2184,11 +2176,7 @@ string_replace_regex (const char *string, void *regex, const char *replace, if (!string || !regex) return NULL; - length = strlen (string) + 1; - result = malloc (length); - if (!result) - return NULL; - snprintf (result, length, "%s", string); + result = strdup (string); start_offset = 0; while (result && result[start_offset]) diff --git a/src/core/core-upgrade-file.c b/src/core/core-upgrade-file.c index 83a99592e..97659a8d7 100644 --- a/src/core/core-upgrade-file.c +++ b/src/core/core-upgrade-file.c @@ -193,7 +193,6 @@ upgrade_file_new (const char *filename, const void *callback_read_pointer, void *callback_read_data) { - int length; struct t_upgrade_file *new_upgrade_file; if (!filename) @@ -203,15 +202,13 @@ upgrade_file_new (const char *filename, if (new_upgrade_file) { /* build name of file */ - length = strlen (weechat_data_dir) + 1 + strlen (filename) + 16 + 1; - new_upgrade_file->filename = malloc (length); - if (!new_upgrade_file->filename) + if (string_asprintf (&new_upgrade_file->filename, + "%s/%s.upgrade", + weechat_data_dir, filename) < 0) { free (new_upgrade_file); return NULL; } - snprintf (new_upgrade_file->filename, length, "%s/%s.upgrade", - weechat_data_dir, filename); new_upgrade_file->callback_read = callback_read; new_upgrade_file->callback_read_pointer = callback_read_pointer; new_upgrade_file->callback_read_data = callback_read_data; diff --git a/src/core/core-upgrade.c b/src/core/core-upgrade.c index f621045dc..9286e97bb 100644 --- a/src/core/core-upgrade.c +++ b/src/core/core-upgrade.c @@ -409,7 +409,7 @@ upgrade_weechat_read_buffer (struct t_infolist *infolist) const char *key, *var_name, *name, *plugin_name, *ptr_id; const char *str; char option_name[64], *option_key, *option_var, *error; - int index, length, main_buffer; + int index, main_buffer; long long id; /* "id" is new in WeeChat 4.3.0 */ @@ -612,11 +612,8 @@ upgrade_weechat_read_buffer (struct t_infolist *infolist) key = infolist_string (infolist, option_name); if (!key) break; - length = 16 + strlen (key) + 1; - option_key = malloc (length); - if (option_key) + if (string_asprintf (&option_key, "key_bind_%s", key) >= 0) { - snprintf (option_key, length, "key_bind_%s", key); snprintf (option_name, sizeof (option_name), "key_command_%05d", index); gui_buffer_set (ptr_buffer, option_key, @@ -635,11 +632,8 @@ upgrade_weechat_read_buffer (struct t_infolist *infolist) var_name = infolist_string (infolist, option_name); if (!var_name) break; - length = 32 + strlen (var_name) + 1; - option_var = malloc (length); - if (option_var) + if (string_asprintf (&option_var, "localvar_set_%s", var_name) >= 0) { - snprintf (option_var, length, "localvar_set_%s", var_name); snprintf (option_name, sizeof (option_name), "localvar_value_%05d", index); gui_buffer_set (ptr_buffer, option_var, diff --git a/src/core/hook/hook-command-run.c b/src/core/hook/hook-command-run.c index af82f3a6f..e2fa183e7 100644 --- a/src/core/hook/hook-command-run.c +++ b/src/core/hook/hook-command-run.c @@ -115,13 +115,8 @@ hook_command_run_exec (struct t_gui_buffer *buffer, const char *command) if (command[0] != '/') { ptr_string = utf8_next_char (command); - length = 1 + strlen (ptr_string) + 1; - command2 = malloc (length); - if (command2) - { - snprintf (command2, length, "/%s", ptr_string); + if (string_asprintf (&command2, "/%s", ptr_string) >= 0) ptr_command = command2; - } } ptr_hook = weechat_hooks[HOOK_TYPE_COMMAND_RUN]; diff --git a/src/core/hook/hook-focus.c b/src/core/hook/hook-focus.c index 53f42785b..ef0c6c587 100644 --- a/src/core/hook/hook-focus.c +++ b/src/core/hook/hook-focus.c @@ -122,7 +122,6 @@ hook_focus_hashtable_map2_cb (void *data, const void *key, const void *value) { struct t_hashtable *hashtable1; - int length; char *key2; /* make C compiler happy */ @@ -130,13 +129,12 @@ hook_focus_hashtable_map2_cb (void *data, hashtable1 = (struct t_hashtable *)data; - length = strlen ((const char *)key) + 1 + 1; - key2 = malloc (length); - if (key2) + if (!hashtable1 || !key || !value) + return; + + if (string_asprintf (&key2, "%s2", (const char *)key) >= 0) { - snprintf (key2, length, "%s2", (const char *)key); - if (hashtable1 && key && value) - hashtable_set (hashtable1, key2, (const char *)value); + hashtable_set (hashtable1, key2, (const char *)value); free (key2); } } @@ -157,7 +155,7 @@ hook_focus_get_data (struct t_hashtable *hashtable_focus1, struct t_hashtable *hashtable1, *hashtable2, *hashtable_ret; const char *focus1_chat, *focus1_bar_item_name, *keys; char **list_keys, *new_key; - int num_keys, i, length, focus1_is_chat; + int num_keys, i, focus1_is_chat; if (!hashtable_focus1) return NULL; @@ -256,11 +254,8 @@ hook_focus_get_data (struct t_hashtable *hashtable_focus1, { for (i = 0; i < num_keys; i++) { - length = strlen (list_keys[i]) + 1 + 1; - new_key = malloc (length); - if (new_key) + if (string_asprintf (&new_key, "%s2", list_keys[i]) >= 0) { - snprintf (new_key, length, "%s2", list_keys[i]); hashtable_set (hashtable1, new_key, hashtable_get (hashtable1, list_keys[i])); diff --git a/src/gui/curses/gui-curses-chat.c b/src/gui/curses/gui-curses-chat.c index 2ba4c870a..d542fe60d 100644 --- a/src/gui/curses/gui-curses-chat.c +++ b/src/gui/curses/gui-curses-chat.c @@ -1058,15 +1058,7 @@ gui_chat_display_time_to_prefix (struct t_gui_window *window, { str_color = gui_color_get_custom (ptr_prefix_color); if (str_color && str_color[0]) - { - length = strlen (str_color) + strlen (ptr_prefix) + 1; - ptr_prefix2 = malloc (length); - if (ptr_prefix2) - { - snprintf (ptr_prefix2, length, "%s%s", - str_color, ptr_prefix); - } - } + string_asprintf (&ptr_prefix2, "%s%s", str_color, ptr_prefix); } ptr_prefix = (ptr_prefix2) ? ptr_prefix2 : strdup (ptr_prefix); } @@ -1156,14 +1148,10 @@ gui_chat_display_time_to_prefix (struct t_gui_window *window, prefix_no_color = gui_color_decode (ptr_prefix, NULL); if (prefix_no_color) { - length = strlen (prefix_no_color) + 32; - prefix_highlighted = malloc (length); - if (prefix_highlighted) - { - snprintf (prefix_highlighted, length, "%s%s", - GUI_COLOR(GUI_COLOR_CHAT_HIGHLIGHT), - prefix_no_color); - } + string_asprintf (&prefix_highlighted, + "%s%s", + GUI_COLOR(GUI_COLOR_CHAT_HIGHLIGHT), + prefix_no_color); free (prefix_no_color); } if (!simulate) @@ -2107,7 +2095,6 @@ gui_chat_get_bare_line (struct t_gui_line *line) char *prefix, *message, str_time[256], *str_line; const char *tag_prefix_nick; struct timeval tv; - int length; prefix = NULL; message = NULL; @@ -2136,21 +2123,15 @@ gui_chat_get_bare_line (struct t_gui_line *line) } tag_prefix_nick = gui_line_search_tag_starting_with (line, "prefix_nick_"); - length = strlen (str_time) + 1 + 1 + strlen (prefix) + 1 + 1 - + strlen (message) + 1; - str_line = malloc (length); - if (str_line) - { - snprintf (str_line, length, - "%s%s%s%s%s%s%s", - str_time, - (str_time[0]) ? " " : "", - (prefix[0] && tag_prefix_nick) ? "<" : "", - prefix, - (prefix[0] && tag_prefix_nick) ? ">" : "", - (prefix[0]) ? " " : "", - message); - } + string_asprintf (&str_line, + "%s%s%s%s%s%s%s", + str_time, + (str_time[0]) ? " " : "", + (prefix[0] && tag_prefix_nick) ? "<" : "", + prefix, + (prefix[0] && tag_prefix_nick) ? ">" : "", + (prefix[0]) ? " " : "", + message); end: free (prefix); diff --git a/src/gui/gui-bar-item-custom.c b/src/gui/gui-bar-item-custom.c index 8b86609b5..96dbf9768 100644 --- a/src/gui/gui-bar-item-custom.c +++ b/src/gui/gui-bar-item-custom.c @@ -193,19 +193,17 @@ gui_bar_item_custom_create_option (const char *item_name, int index_option, const char *value) { struct t_config_option *ptr_option; - int length; char *option_name; ptr_option = NULL; - length = strlen (item_name) + 1 + - strlen (gui_bar_item_custom_option_string[index_option]) + 1; - option_name = malloc (length); - if (!option_name) + if (string_asprintf (&option_name, + "%s.%s", + item_name, + gui_bar_item_custom_option_string[index_option]) < 0) + { return NULL; - - snprintf (option_name, length, "%s.%s", - item_name, gui_bar_item_custom_option_string[index_option]); + } switch (index_option) { diff --git a/src/gui/gui-bar-item.c b/src/gui/gui-bar-item.c index 2e5220e04..81c052cb5 100644 --- a/src/gui/gui-bar-item.c +++ b/src/gui/gui-bar-item.c @@ -914,7 +914,7 @@ gui_bar_item_input_text_cb (const void *pointer, void *data, char *ptr_input, *ptr_input2, str_buffer[128], str_start_input[16]; char str_cursor[16], *buf, str_key_debug[1024], *str_lead_linebreak; const char *pos_cursor; - int length, length_cursor, length_start_input, length_lead_linebreak; + int length, length_cursor; int buf_pos, is_multiline; /* make C compiler happy */ @@ -936,7 +936,6 @@ gui_bar_item_input_text_cb (const void *pointer, void *data, GUI_COLOR_COLOR_CHAR, GUI_COLOR_BAR_CHAR, GUI_COLOR_BAR_START_INPUT_CHAR); - length_start_input = strlen (str_start_input); if (gui_key_debug) { @@ -1016,17 +1015,12 @@ gui_bar_item_input_text_cb (const void *pointer, void *data, if ((buffer->text_search == GUI_BUFFER_SEARCH_HISTORY) && buffer->text_search_ptr_history) { - length = strlen (ptr_input) + 16 - + ((buffer->text_search_ptr_history->text) ? - strlen (buffer->text_search_ptr_history->text) : 0); - buf = malloc (length); - if (buf) + if (string_asprintf (&buf, + "%s => %s", + ptr_input, + (buffer->text_search_ptr_history->text) ? + buffer->text_search_ptr_history->text : "") >= 0) { - snprintf (buf, length, - "%s => %s", - ptr_input, - (buffer->text_search_ptr_history->text) ? - buffer->text_search_ptr_history->text : ""); free (ptr_input); ptr_input = buf; } @@ -1050,29 +1044,23 @@ gui_bar_item_input_text_cb (const void *pointer, void *data, str_lead_linebreak = (is_multiline && CONFIG_BOOLEAN(config_look_input_multiline_lead_linebreak)) ? "\r" : ""; - length_lead_linebreak = strlen (str_lead_linebreak); /* insert "start input" at beginning of string */ if (ptr_input) { - length = strlen (ptr_input) + length_start_input + length_lead_linebreak + 1; - buf = malloc (length); - if (buf) + if (string_asprintf (&buf, + "%s%s%s", + str_start_input, + str_lead_linebreak, + ptr_input) >= 0) { - snprintf (buf, length, "%s%s%s", str_start_input, str_lead_linebreak, ptr_input); free (ptr_input); ptr_input = buf; } } else { - length = length_start_input + length_cursor + 1; - ptr_input = malloc (length); - if (ptr_input) - { - strcpy (ptr_input, str_start_input); - strcat (ptr_input, str_cursor); - } + string_asprintf (&ptr_input, "%s%s", str_start_input, str_cursor); } return ptr_input; @@ -2088,7 +2076,6 @@ gui_bar_item_away_cb (const void *pointer, void *data, { const char *away; char *buf, *message; - int length; /* make C compiler happy */ (void) pointer; @@ -2109,14 +2096,11 @@ gui_bar_item_away_cb (const void *pointer, void *data, strdup (away) : strdup (_("away")); if (message) { - length = strlen (message) + 64 + 1; - buf = malloc (length); - if (buf) - { - snprintf (buf, length, "%s%s", - gui_color_get_custom (gui_color_get_name (CONFIG_COLOR(config_color_item_away))), - message); - } + string_asprintf ( + &buf, + "%s%s", + gui_color_get_custom (gui_color_get_name (CONFIG_COLOR(config_color_item_away))), + message); free (message); } diff --git a/src/gui/gui-bar.c b/src/gui/gui-bar.c index 434ab4efe..c602999c9 100644 --- a/src/gui/gui-bar.c +++ b/src/gui/gui-bar.c @@ -1911,7 +1911,6 @@ void gui_bar_create_default_input () { struct t_gui_bar *ptr_bar; - int length; char *buf; /* search an input_text item */ @@ -1922,18 +1921,13 @@ gui_bar_create_default_input () if (ptr_bar) { /* add item "input_text" to input bar */ - length = 1; - if (CONFIG_STRING(ptr_bar->options[GUI_BAR_OPTION_ITEMS])) - length += strlen (CONFIG_STRING(ptr_bar->options[GUI_BAR_OPTION_ITEMS])); - length += 1; /* "," */ - length += strlen (gui_bar_item_names[GUI_BAR_ITEM_INPUT_TEXT]); - buf = malloc (length); - if (buf) + if (string_asprintf ( + &buf, + "%s,%s", + (CONFIG_STRING(ptr_bar->options[GUI_BAR_OPTION_ITEMS])) ? + CONFIG_STRING(ptr_bar->options[GUI_BAR_OPTION_ITEMS]) : "", + gui_bar_item_names[GUI_BAR_ITEM_INPUT_TEXT]) >= 0) { - snprintf (buf, length, "%s,%s", - (CONFIG_STRING(ptr_bar->options[GUI_BAR_OPTION_ITEMS])) ? - CONFIG_STRING(ptr_bar->options[GUI_BAR_OPTION_ITEMS]) : "", - gui_bar_item_names[GUI_BAR_ITEM_INPUT_TEXT]); config_file_option_set (ptr_bar->options[GUI_BAR_OPTION_ITEMS], buf, 1); gui_chat_printf (NULL, _("Bar \"%s\" updated"), gui_bar_default_name[GUI_BAR_DEFAULT_INPUT]); diff --git a/src/gui/gui-buffer.c b/src/gui/gui-buffer.c index f9bad3f99..be8929018 100644 --- a/src/gui/gui-buffer.c +++ b/src/gui/gui-buffer.c @@ -226,20 +226,14 @@ gui_buffer_get_plugin_name (struct t_gui_buffer *buffer) void gui_buffer_build_full_name (struct t_gui_buffer *buffer) { - int length; - if (!buffer) return; free (buffer->full_name); - length = strlen (gui_buffer_get_plugin_name (buffer)) + 1 + - strlen (buffer->name) + 1; - buffer->full_name = malloc (length); - if (buffer->full_name) - { - snprintf (buffer->full_name, length, "%s.%s", - gui_buffer_get_plugin_name (buffer), buffer->name); - } + string_asprintf (&buffer->full_name, + "%s.%s", + gui_buffer_get_plugin_name (buffer), + buffer->name); } /* diff --git a/src/gui/gui-chat.c b/src/gui/gui-chat.c index e9cce8ba0..e40c3d3a0 100644 --- a/src/gui/gui-chat.c +++ b/src/gui/gui-chat.c @@ -817,7 +817,7 @@ gui_chat_printf_datetime_tags_internal (struct t_gui_buffer *buffer, const char *tags, char *message) { - int display_time, length_data, length_str; + int display_time; char *ptr_msg, *pos_prefix, *pos_tab; char *modifier_data, *string, *new_string, *pos_newline; struct t_gui_line *new_line; @@ -879,107 +879,99 @@ gui_chat_printf_datetime_tags_internal (struct t_gui_buffer *buffer, goto no_print; /* call modifier for message printed ("weechat_print") */ - length_data = 64 + 1 + ((tags) ? strlen (tags) : 0) + 1; - modifier_data = malloc (length_data); - length_str = ((new_line->data->prefix && new_line->data->prefix[0]) ? strlen (new_line->data->prefix) : 1) + - 1 + - (new_line->data->message ? strlen (new_line->data->message) : 0) + - 1; - string = malloc (length_str); - if (modifier_data && string) + string_asprintf (&modifier_data, + "0x%lx;%s", + (unsigned long)buffer, + (tags) ? tags : ""); + if (display_time) { - snprintf (modifier_data, length_data, - "0x%lx;%s", - (unsigned long)buffer, - (tags) ? tags : ""); - if (display_time) + string_asprintf ( + &string, + "%s\t%s", + (new_line->data->prefix && new_line->data->prefix[0]) ? + new_line->data->prefix : " ", + (new_line->data->message) ? new_line->data->message : ""); + } + else + { + string_asprintf ( + &string, + "\t\t%s", + (new_line->data->message) ? new_line->data->message : ""); + } + new_string = hook_modifier_exec (NULL, + "weechat_print", + modifier_data, + string); + if (new_string) + { + if (!new_string[0] && string[0]) { - snprintf (string, length_str, - "%s\t%s", - (new_line->data->prefix && new_line->data->prefix[0]) ? - new_line->data->prefix : " ", - (new_line->data->message) ? new_line->data->message : ""); + /* + * modifier returned empty message, then we'll not + * print anything + */ + goto no_print; } - else + else if (strcmp (string, new_string) != 0) { - snprintf (string, length_str, - "\t\t%s", - (new_line->data->message) ? new_line->data->message : ""); - } - new_string = hook_modifier_exec (NULL, - "weechat_print", - modifier_data, - string); - if (new_string) - { - if (!new_string[0] && string[0]) + if (!buffer->input_multiline) { - /* - * modifier returned empty message, then we'll not - * print anything - */ - goto no_print; + /* if input_multiline is not set, keep only first line */ + pos_newline = strchr (new_string, '\n'); + if (pos_newline) + pos_newline[0] = '\0'; } - else if (strcmp (string, new_string) != 0) - { - if (!buffer->input_multiline) - { - /* if input_multiline is not set, keep only first line */ - pos_newline = strchr (new_string, '\n'); - if (pos_newline) - pos_newline[0] = '\0'; - } - /* use new message if there are changes */ - display_time = 1; - pos_prefix = NULL; - ptr_msg = new_string; - /* space followed by tab => prefix ignored */ - if ((ptr_msg[0] == ' ') && (ptr_msg[1] == '\t')) + /* use new message if there are changes */ + display_time = 1; + pos_prefix = NULL; + ptr_msg = new_string; + /* space followed by tab => prefix ignored */ + if ((ptr_msg[0] == ' ') && (ptr_msg[1] == '\t')) + { + ptr_msg += 2; + } + else + { + /* if two first chars are tab, then do not display time */ + if ((ptr_msg[0] == '\t') && (ptr_msg[1] == '\t')) { + display_time = 0; + new_line->data->date = 0; ptr_msg += 2; } else { - /* if two first chars are tab, then do not display time */ - if ((ptr_msg[0] == '\t') && (ptr_msg[1] == '\t')) + /* if tab found, use prefix (before tab) */ + pos_tab = strchr (ptr_msg, '\t'); + if (pos_tab) { - display_time = 0; - new_line->data->date = 0; - ptr_msg += 2; - } - else - { - /* if tab found, use prefix (before tab) */ - pos_tab = strchr (ptr_msg, '\t'); - if (pos_tab) - { - pos_tab[0] = '\0'; - pos_prefix = ptr_msg; - ptr_msg = pos_tab + 1; - } + pos_tab[0] = '\0'; + pos_prefix = ptr_msg; + ptr_msg = pos_tab + 1; } } - if ((new_line->data->date == 0) && display_time) - { - new_line->data->date = new_line->data->date_printed; - new_line->data->date_usec = new_line->data->date_usec_printed; - } - string_shared_free (new_line->data->prefix); - if (pos_prefix) - { - new_line->data->prefix = (char *)string_shared_get (pos_prefix); - } - else - { - new_line->data->prefix = (new_line->data->date != 0) ? - (char *)string_shared_get ("") : NULL; - } - new_line->data->prefix_length = gui_chat_strlen_screen ( - new_line->data->prefix); - free (new_line->data->message); - new_line->data->message = strdup (ptr_msg); } + if ((new_line->data->date == 0) && display_time) + { + new_line->data->date = new_line->data->date_printed; + new_line->data->date_usec = new_line->data->date_usec_printed; + } + string_shared_free (new_line->data->prefix); + if (pos_prefix) + { + new_line->data->prefix = (char *)string_shared_get (pos_prefix); + } + else + { + new_line->data->prefix = (new_line->data->date != 0) ? + (char *)string_shared_get ("") : NULL; + } + new_line->data->prefix_length = gui_chat_strlen_screen ( + new_line->data->prefix); + free (new_line->data->message); + new_line->data->message = strdup (ptr_msg); } } @@ -1311,8 +1303,7 @@ gui_chat_hsignal_quote_line_cb (const void *pointer, void *data, long number; struct timeval tv; struct t_gui_line *ptr_line; - int is_nick, length_time, length_nick_prefix, length_prefix; - int length_nick_suffix, length_message, length, rc; + int is_nick, rc; char str_time[128], *str, *error; /* make C compiler happy */ @@ -1380,26 +1371,17 @@ gui_chat_hsignal_quote_line_cb (const void *pointer, void *data, if (!message) return WEECHAT_RC_OK; - length_time = strlen (str_time); - length_nick_prefix = strlen (CONFIG_STRING(config_look_quote_nick_prefix)); - length_prefix = (ptr_prefix) ? strlen (ptr_prefix) : 0; - length_nick_suffix = strlen (CONFIG_STRING(config_look_quote_nick_suffix)); - length_message = strlen (message); - - length = length_time + 1 + - length_nick_prefix + length_prefix + length_nick_suffix + 1 + - length_message + 1 + 1; - str = malloc (length); - if (str) + if (string_asprintf ( + &str, + "%s%s%s%s%s%s%s ", + str_time, + (str_time[0]) ? " " : "", + (ptr_prefix && ptr_prefix[0] && is_nick) ? CONFIG_STRING(config_look_quote_nick_prefix) : "", + (ptr_prefix) ? ptr_prefix : "", + (ptr_prefix && ptr_prefix[0] && is_nick) ? CONFIG_STRING(config_look_quote_nick_suffix) : "", + (ptr_prefix && ptr_prefix[0]) ? " " : "", + message) >= 0) { - snprintf (str, length, "%s%s%s%s%s%s%s ", - str_time, - (str_time[0]) ? " " : "", - (ptr_prefix && ptr_prefix[0] && is_nick) ? CONFIG_STRING(config_look_quote_nick_prefix) : "", - (ptr_prefix) ? ptr_prefix : "", - (ptr_prefix && ptr_prefix[0] && is_nick) ? CONFIG_STRING(config_look_quote_nick_suffix) : "", - (ptr_prefix && ptr_prefix[0]) ? " " : "", - message); gui_input_insert_string (gui_current_window->buffer, str); gui_input_text_changed_modifier_and_signal (gui_current_window->buffer, 1, /* save undo */ diff --git a/src/gui/gui-nicklist.c b/src/gui/gui-nicklist.c index b79b3a107..f3b82151b 100644 --- a/src/gui/gui-nicklist.c +++ b/src/gui/gui-nicklist.c @@ -60,18 +60,14 @@ gui_nicklist_send_signal (const char *signal, struct t_gui_buffer *buffer, const char *arguments) { char *str_args; - int length; if (buffer) { - length = 128 + ((arguments) ? strlen (arguments) : 0) + 1 + 1; - str_args = malloc (length); - if (str_args) + if (string_asprintf (&str_args, + "0x%lx,%s", + (unsigned long)buffer, + (arguments) ? arguments : "") >= 0) { - snprintf (str_args, length, - "0x%lx,%s", - (unsigned long)buffer, - (arguments) ? arguments : ""); (void) hook_signal_send (signal, WEECHAT_HOOK_SIGNAL_STRING, str_args); free (str_args); diff --git a/src/plugins/plugin-config.c b/src/plugins/plugin-config.c index f8b2b8e17..be548bd1b 100644 --- a/src/plugins/plugin-config.c +++ b/src/plugins/plugin-config.c @@ -51,7 +51,6 @@ struct t_config_section *plugin_config_section_desc = NULL; struct t_config_option * plugin_config_search (const char *plugin_name, const char *option_name) { - int length; char *option_full_name; struct t_config_option *ptr_option; @@ -60,12 +59,10 @@ plugin_config_search (const char *plugin_name, const char *option_name) ptr_option = NULL; - length = strlen (plugin_name) + 1 + strlen (option_name) + 1; - option_full_name = malloc (length); - if (option_full_name) + if (string_asprintf (&option_full_name, + "%s.%s", + plugin_name, option_name) >= 0) { - snprintf (option_full_name, length, "%s.%s", - plugin_name, option_name); ptr_option = config_file_search_option (plugin_config_file, plugin_config_section_var, option_full_name); @@ -113,17 +110,15 @@ int plugin_config_set (const char *plugin_name, const char *option_name, const char *value) { - int length, rc; + int rc; char *option_full_name; rc = WEECHAT_CONFIG_OPTION_SET_ERROR; - length = strlen (plugin_name) + 1 + strlen (option_name) + 1; - option_full_name = malloc (length); - if (option_full_name) + if (string_asprintf (&option_full_name, + "%s.%s", + plugin_name, option_name) >= 0) { - snprintf (option_full_name, length, "%s.%s", - plugin_name, option_name); rc = plugin_config_set_internal (option_full_name, value); free (option_full_name); } @@ -200,15 +195,12 @@ void plugin_config_set_desc (const char *plugin_name, const char *option_name, const char *description) { - int length; char *option_full_name; - length = strlen (plugin_name) + 1 + strlen (option_name) + 1; - option_full_name = malloc (length); - if (option_full_name) + if (string_asprintf (&option_full_name, + "%s.%s", + plugin_name, option_name) >= 0) { - snprintf (option_full_name, length, "%s.%s", - plugin_name, option_name); plugin_config_set_desc_internal (option_full_name, description); free (option_full_name); } diff --git a/src/plugins/plugin.c b/src/plugins/plugin.c index aaa0a4541..f5d94c849 100644 --- a/src/plugins/plugin.c +++ b/src/plugins/plugin.c @@ -1053,7 +1053,7 @@ plugin_auto_load (char *force_plugin_autoload, struct t_plugin_args plugin_args; struct t_arraylist *arraylist; struct t_hashtable *options; - int length, i; + int i; plugin_args.argc = argc; plugin_args.argv = argv; @@ -1106,23 +1106,20 @@ plugin_auto_load (char *force_plugin_autoload, extra_libdir = getenv (WEECHAT_EXTRA_LIBDIR); if (extra_libdir && extra_libdir[0]) { - length = strlen (extra_libdir) + 16 + 1; - dir_name = malloc (length); - snprintf (dir_name, length, "%s/plugins", extra_libdir); - dir_exec_on_files (dir_name, 1, 0, - &plugin_auto_load_file, &plugin_args); - free (dir_name); + if (string_asprintf (&dir_name, "%s/plugins", extra_libdir) >= 0) + { + dir_exec_on_files (dir_name, 1, 0, + &plugin_auto_load_file, &plugin_args); + free (dir_name); + } } } /* auto-load plugins in WeeChat global lib dir */ if (load_from_lib_dir) { - length = strlen (WEECHAT_LIBDIR) + 16 + 1; - dir_name = malloc (length); - if (dir_name) + if (string_asprintf (&dir_name,"%s/plugins", WEECHAT_LIBDIR) >= 0) { - snprintf (dir_name, length, "%s/plugins", WEECHAT_LIBDIR); dir_exec_on_files (dir_name, 1, 0, &plugin_auto_load_file, &plugin_args); free (dir_name); @@ -1353,48 +1350,43 @@ plugin_reload_name (const char *name, int argc, char **argv) void plugin_display_short_list () { - const char *plugins_loaded; - char *buf; - int length; + char **buf; struct t_weechat_plugin *ptr_plugin; struct t_weelist *list; struct t_weelist_item *ptr_item; - if (weechat_plugins) + if (!weechat_plugins) + return; + + list = weelist_new (); + if (!list) + return; + + for (ptr_plugin = weechat_plugins; ptr_plugin; + ptr_plugin = ptr_plugin->next_plugin) { - list = weelist_new (); - if (list) - { - plugins_loaded = _("Plugins loaded:"); - - length = strlen (plugins_loaded) + 1; - - for (ptr_plugin = weechat_plugins; ptr_plugin; - ptr_plugin = ptr_plugin->next_plugin) - { - length += strlen (ptr_plugin->name) + 2; - weelist_add (list, ptr_plugin->name, WEECHAT_LIST_POS_SORT, NULL); - } - length++; - - buf = malloc (length); - if (buf) - { - strcpy (buf, plugins_loaded); - strcat (buf, " "); - for (ptr_item = list->items; ptr_item; - ptr_item = ptr_item->next_item) - { - strcat (buf, ptr_item->data); - if (ptr_item->next_item) - strcat (buf, ", "); - } - gui_chat_printf (NULL, "%s", buf); - free (buf); - } - weelist_free (list); - } + weelist_add (list, ptr_plugin->name, WEECHAT_LIST_POS_SORT, NULL); } + + buf = string_dyn_alloc (256); + if (!buf) + { + weelist_free (list); + return; + } + + string_dyn_concat (buf, _("Plugins loaded:"), -1); + string_dyn_concat (buf, " ", -1); + for (ptr_item = list->items; ptr_item; ptr_item = ptr_item->next_item) + { + string_dyn_concat (buf, ptr_item->data, -1); + if (ptr_item->next_item) + string_dyn_concat (buf, ", ", -1); + } + gui_chat_printf (NULL, "%s", *buf); + + string_dyn_free (buf, 1); + weelist_free (list); } /*