From d3d0948e2e734dd7ad4da41f44560aa67c274ee3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Helleu?= Date: Tue, 17 Dec 2024 21:04:42 +0100 Subject: [PATCH] script: replace calls to malloc by weechat_asprintf --- src/plugins/script/script-action.c | 102 ++++++++++----------- src/plugins/script/script-config.c | 138 +++++++++++++---------------- src/plugins/script/script-repo.c | 17 ++-- 3 files changed, 113 insertions(+), 144 deletions(-) diff --git a/src/plugins/script/script-action.c b/src/plugins/script/script-action.c index 603f2a46d..061837115 100644 --- a/src/plugins/script/script-action.c +++ b/src/plugins/script/script-action.c @@ -442,7 +442,7 @@ script_action_run_autoload (const char *name, int quiet, int autoload) { struct t_script_repo *ptr_script; char *pos, str_signal[256], *weechat_data_dir, *filename; - int language, length, script_found, script_autoloaded; + int language, script_found, script_autoloaded; struct stat st; /* find script language */ @@ -465,27 +465,30 @@ script_action_run_autoload (const char *name, int quiet, int autoload) script_found = 0; script_autoloaded = 0; weechat_data_dir = weechat_info_get ("weechat_data_dir", NULL); - length = strlen (weechat_data_dir) + strlen (name) + 64; - filename = malloc (length); - if (filename) + + if (weechat_asprintf (&filename, + "%s/%s/%s", + weechat_data_dir, + script_language[language], + name) >= 0) { /* check if script exists */ - snprintf (filename, length, "%s/%s/%s", - weechat_data_dir, - script_language[language], - name); if (stat (filename, &st) == 0) script_found = 1; - /* check if script is autoloaded */ - snprintf (filename, length, "%s/%s/autoload/%s", - weechat_data_dir, - script_language[language], - name); - if (stat (filename, &st) == 0) - script_autoloaded = 1; - free (filename); + + if (weechat_asprintf (&filename, + "%s/%s/autoload/%s", + weechat_data_dir, + script_language[language], + name) >= 0) + { + /* check if script is autoloaded */ + if (stat (filename, &st) == 0) + script_autoloaded = 1; + free (filename); + } } free (weechat_data_dir); @@ -506,15 +509,13 @@ script_action_run_autoload (const char *name, int quiet, int autoload) autoload = (script_autoloaded) ? 0 : 1; /* ask plugin to autoload (or not) script */ - length = 16 + strlen (name) + 1; - filename = malloc (length); - if (filename) + if (weechat_asprintf ( + &filename, + "%s%s%s", + (quiet && weechat_config_boolean (script_config_look_quiet_actions)) ? "-q " : "", + (autoload) ? "-a " : "", + name) >= 0) { - snprintf (filename, length, - "%s%s%s", - (quiet && weechat_config_boolean (script_config_look_quiet_actions)) ? "-q " : "", - (autoload) ? "-a " : "", - name); snprintf (str_signal, sizeof (str_signal), "%s_script_autoload", script_language[language]); @@ -567,7 +568,7 @@ script_action_install_url_cb (const void *pointer, void *data, { const char *pos_name, *ptr_error; char *filename, *filename2, str_signal[256]; - int quiet, auto_load, length; + int quiet, auto_load; struct t_script_repo *ptr_script; /* make C compiler happy */ @@ -606,23 +607,22 @@ script_action_install_url_cb (const void *pointer, void *data, if (!filename) return WEECHAT_RC_OK; - length = 16 + strlen (filename) + 1; - filename2 = malloc (length); - if (!filename2) + if (ptr_script->status & SCRIPT_STATUS_INSTALLED) + auto_load = (ptr_script->status & SCRIPT_STATUS_AUTOLOADED) ? 1 : 0; + else + auto_load = weechat_config_boolean (script_config_scripts_autoload); + + if (weechat_asprintf ( + &filename2, + "%s%s%s", + (quiet && weechat_config_boolean (script_config_look_quiet_actions)) ? "-q " : "", + (auto_load) ? "-a " : "", + filename) < 0) { free (filename); return WEECHAT_RC_OK; } - if (ptr_script->status & SCRIPT_STATUS_INSTALLED) - auto_load = (ptr_script->status & SCRIPT_STATUS_AUTOLOADED) ? 1 : 0; - else - auto_load = weechat_config_boolean (script_config_scripts_autoload); - snprintf (filename2, length, - "%s%s%s", - (quiet && weechat_config_boolean (script_config_look_quiet_actions)) ? "-q " : "", - (auto_load) ? "-a " : "", - filename); snprintf (str_signal, sizeof (str_signal), "%s_script_install", script_language[ptr_script->language]); @@ -751,7 +751,6 @@ script_action_run_remove (const char *name, int quiet) { struct t_script_repo *ptr_script; char str_signal[256], *filename; - int length; ptr_script = script_repo_search_by_name_ext (name); if (!ptr_script) @@ -802,14 +801,12 @@ script_action_run_remove (const char *name, int quiet) } /* ask plugin to remove script */ - length = 3 + strlen (ptr_script->name_with_extension) + 1; - filename = malloc (length); - if (filename) + if (weechat_asprintf ( + &filename, + "%s%s", + (quiet && weechat_config_boolean (script_config_look_quiet_actions)) ? "-q " : "", + ptr_script->name_with_extension) >= 0) { - snprintf (filename, length, - "%s%s", - (quiet && weechat_config_boolean (script_config_look_quiet_actions)) ? "-q " : "", - ptr_script->name_with_extension); snprintf (str_signal, sizeof (str_signal), "%s_script_remove", script_language[ptr_script->language]); @@ -1084,17 +1081,12 @@ script_action_show_source_url_cb (const void *pointer, void *data, filename_loaded = script_repo_get_filename_loaded (ptr_script); if (filename_loaded) { - length = strlen (ptr_diff_command) + 1 - + strlen (filename_loaded) + 1 - + strlen (filename) + 1; - diff_command = malloc (length); - if (diff_command) + if (weechat_asprintf (&diff_command, + "%s %s %s", + ptr_diff_command, + filename_loaded, + filename) >= 0) { - snprintf (diff_command, length, - "%s %s %s", - ptr_diff_command, - filename_loaded, - filename); script_buffer_detail_script_last_line++; script_buffer_detail_script_line_diff = script_buffer_detail_script_last_line; weechat_printf_y (script_buffer, diff --git a/src/plugins/script/script-config.c b/src/plugins/script/script-config.c index 98f4f0a37..3b5eed93a 100644 --- a/src/plugins/script/script-config.c +++ b/src/plugins/script/script-config.c @@ -162,7 +162,6 @@ char * script_config_get_xml_filename () { char *path, *filename; - int length; struct t_hashtable *options; options = weechat_hashtable_new ( @@ -175,10 +174,7 @@ script_config_get_xml_filename () path = weechat_string_eval_path_home ( weechat_config_string (script_config_scripts_path), NULL, NULL, options); weechat_hashtable_free (options); - length = strlen (path) + 64; - filename = malloc (length); - if (filename) - snprintf (filename, length, "%s/plugins.xml.gz", path); + weechat_asprintf (&filename, "%s/plugins.xml.gz", path); free (path); return filename; } @@ -195,7 +191,6 @@ script_config_get_script_download_filename (struct t_script_repo *script, const char *suffix) { char *path, *filename; - int length; struct t_hashtable *options; options = weechat_hashtable_new ( @@ -208,17 +203,11 @@ script_config_get_script_download_filename (struct t_script_repo *script, path = weechat_string_eval_path_home ( weechat_config_string (script_config_scripts_path), NULL, NULL, options); weechat_hashtable_free (options); - length = strlen (path) + 1 + strlen (script->name_with_extension) - + ((suffix) ? strlen (suffix) : 0) + 1; - filename = malloc (length); - if (filename) - { - snprintf (filename, length, - "%s/%s%s", - path, - script->name_with_extension, - (suffix) ? suffix : ""); - } + weechat_asprintf (&filename, + "%s/%s%s", + path, + script->name_with_extension, + (suffix) ? suffix : ""); free (path); return filename; } @@ -307,45 +296,42 @@ script_config_change_hold_cb (const void *pointer, void *data, void script_config_hold (const char *name_with_extension) { - char **items, *hold; - int num_items, i, length; + char **items, **hold; + int num_items, i; - length = strlen (weechat_config_string (script_config_scripts_hold)) + - 1 + strlen (name_with_extension) + 1; - hold = malloc (length); - if (hold) + hold = weechat_string_dyn_alloc (256); + if (!hold) + return; + + items = weechat_string_split ( + weechat_config_string (script_config_scripts_hold), + ",", + NULL, + WEECHAT_STRING_SPLIT_STRIP_LEFT + | WEECHAT_STRING_SPLIT_STRIP_RIGHT + | WEECHAT_STRING_SPLIT_COLLAPSE_SEPS, + 0, + &num_items); + if (items) { - hold[0] = '\0'; - items = weechat_string_split ( - weechat_config_string (script_config_scripts_hold), - ",", - NULL, - WEECHAT_STRING_SPLIT_STRIP_LEFT - | WEECHAT_STRING_SPLIT_STRIP_RIGHT - | WEECHAT_STRING_SPLIT_COLLAPSE_SEPS, - 0, - &num_items); - if (items) + for (i = 0; i < num_items; i++) { - for (i = 0; i < num_items; i++) + if (strcmp (items[i], name_with_extension) != 0) { - if (strcmp (items[i], name_with_extension) != 0) - { - if (hold[0]) - strcat (hold, ","); - strcat (hold, items[i]); - } + if ((*hold)[0]) + weechat_string_dyn_concat (hold, ",", -1); + weechat_string_dyn_concat (hold, items[i], -1); } - weechat_string_free_split (items); } - if (hold[0]) - strcat (hold, ","); - strcat (hold, name_with_extension); - - weechat_config_option_set (script_config_scripts_hold, hold, 0); - - free (hold); + weechat_string_free_split (items); } + if ((*hold)[0]) + weechat_string_dyn_concat (hold, ",", -1); + weechat_string_dyn_concat (hold, name_with_extension, -1); + + weechat_config_option_set (script_config_scripts_hold, *hold, 0); + + weechat_string_dyn_free (hold, 1); } /* @@ -358,41 +344,39 @@ script_config_hold (const char *name_with_extension) void script_config_unhold (const char *name_with_extension) { - char **items, *hold; - int num_items, i, length; + char **items, **hold; + int num_items, i; - length = strlen (weechat_config_string (script_config_scripts_hold)) + 1; - hold = malloc (length); - if (hold) + hold = weechat_string_dyn_alloc (256); + if (!hold) + return; + + items = weechat_string_split ( + weechat_config_string (script_config_scripts_hold), + ",", + NULL, + WEECHAT_STRING_SPLIT_STRIP_LEFT + | WEECHAT_STRING_SPLIT_STRIP_RIGHT + | WEECHAT_STRING_SPLIT_COLLAPSE_SEPS, + 0, + &num_items); + if (items) { - hold[0] = '\0'; - items = weechat_string_split ( - weechat_config_string (script_config_scripts_hold), - ",", - NULL, - WEECHAT_STRING_SPLIT_STRIP_LEFT - | WEECHAT_STRING_SPLIT_STRIP_RIGHT - | WEECHAT_STRING_SPLIT_COLLAPSE_SEPS, - 0, - &num_items); - if (items) + for (i = 0; i < num_items; i++) { - for (i = 0; i < num_items; i++) + if (strcmp (items[i], name_with_extension) != 0) { - if (strcmp (items[i], name_with_extension) != 0) - { - if (hold[0]) - strcat (hold, ","); - strcat (hold, items[i]); - } + if ((*hold)[0]) + weechat_string_dyn_concat (hold, ",", -1); + weechat_string_dyn_concat (hold, items[i], -1); } - weechat_string_free_split (items); } - - weechat_config_option_set (script_config_scripts_hold, hold, 0); - - free (hold); + weechat_string_free_split (items); } + + weechat_config_option_set (script_config_scripts_hold, *hold, 0); + + weechat_string_dyn_free (hold, 1); } /* diff --git a/src/plugins/script/script-repo.c b/src/plugins/script/script-repo.c index d47c0923a..167a192fc 100644 --- a/src/plugins/script/script-repo.c +++ b/src/plugins/script/script-repo.c @@ -1107,7 +1107,7 @@ script_repo_file_read (int quiet) const char *ptr_desc; gzFile file; struct t_script_repo *script; - int version_number, version_ok, script_ok, length; + int version_number, version_ok, script_ok; struct tm tm_script; struct t_hashtable *descriptions; @@ -1237,17 +1237,10 @@ script_repo_file_read (int quiet) if (ptr_desc) { script->description = strdup (ptr_desc); - length = strlen (script->name) + 1 + - strlen (script_extension[script->language]) + 1; - script->name_with_extension = malloc (length); - if (script->name_with_extension) - { - snprintf (script->name_with_extension, - length, - "%s.%s", - script->name, - script_extension[script->language]); - } + weechat_asprintf (&script->name_with_extension, + "%s.%s", + script->name, + script_extension[script->language]); script_repo_update_status (script); script->displayed = (script_repo_match_filter (script)); script_repo_add (script);