Some minor improvements

* destroy/free/shutdown/close in reverse order of allocation
* more static/auto_Xfree
* less variables/strlen/GString
* properly `\0`-terminate string of testcase

Signed-off-by: Steffen Jaeckel <s@jaeckel.eu>
This commit is contained in:
Steffen Jaeckel
2025-03-07 11:51:30 +01:00
parent b55bf6e4bd
commit 72b99ceb6d
9 changed files with 21 additions and 32 deletions

View File

@@ -177,9 +177,9 @@ callbacks_remove(const char* const plugin_name)
void void
callbacks_close(void) callbacks_close(void)
{ {
g_hash_table_destroy(p_commands);
g_hash_table_destroy(p_timed_functions);
g_hash_table_destroy(p_window_callbacks); g_hash_table_destroy(p_window_callbacks);
g_hash_table_destroy(p_timed_functions);
g_hash_table_destroy(p_commands);
} }
void void

View File

@@ -95,9 +95,9 @@ _plugins_shutdown(void)
c_shutdown(); c_shutdown();
#endif #endif
autocompleters_destroy();
plugin_themes_close();
plugin_settings_close(); plugin_settings_close();
plugin_themes_close();
autocompleters_destroy();
callbacks_close(); callbacks_close();
disco_close(); disco_close();
g_hash_table_destroy(plugins); g_hash_table_destroy(plugins);
@@ -399,7 +399,7 @@ plugins_reload(const char* const name, GString* error_message)
return res; return res;
} }
void static void
_plugins_unloaded_list_dir(const gchar* const dir, GSList** result) _plugins_unloaded_list_dir(const gchar* const dir, GSList** result)
{ {
GDir* plugins_dir = g_dir_open(dir, 0, NULL); GDir* plugins_dir = g_dir_open(dir, 0, NULL);

View File

@@ -100,16 +100,13 @@ python_env_init(void)
python_init_prof(); python_init_prof();
auto_gchar gchar* plugins_dir = files_get_data_path(DIR_PLUGINS); auto_gchar gchar* plugins_dir = files_get_data_path(DIR_PLUGINS);
GString* path = g_string_new("import sys\n"); auto_gchar gchar* path = g_strdup_printf(
g_string_append(path, "sys.path.append(\""); "import sys\n"
g_string_append(path, plugins_dir); "sys.path.append(\"%s/\")\n", plugins_dir);
g_string_append(path, "/\")\n");
PyRun_SimpleString(path->str); PyRun_SimpleString(path);
python_check_error(); python_check_error();
g_string_free(path, TRUE);
allow_python_threads(); allow_python_threads();
} }

View File

@@ -164,11 +164,6 @@ http_file_put(void* userdata)
FILE* fh = NULL; FILE* fh = NULL;
auto_char char* err = NULL; auto_char char* err = NULL;
gchar* content_type_header;
// Optional headers
gchar* auth_header = NULL;
gchar* cookie_header = NULL;
gchar* expires_header = NULL;
CURL* curl; CURL* curl;
CURLcode res; CURLcode res;
@@ -177,15 +172,14 @@ http_file_put(void* userdata)
upload->bytes_sent = 0; upload->bytes_sent = 0;
pthread_mutex_lock(&lock); pthread_mutex_lock(&lock);
gchar* msg = g_strdup_printf("Uploading '%s': 0%%", upload->filename); auto_gchar gchar* msg = g_strdup_printf("Uploading '%s': 0%%", upload->filename);
if (!msg) { if (!msg) {
msg = g_strdup(FALLBACK_MSG); msg = g_strdup(FALLBACK_MSG);
} }
win_print_http_transfer(upload->window, msg, upload->put_url); win_print_http_transfer(upload->window, msg, upload->put_url);
g_free(msg);
auto_gchar gchar* cert_path = prefs_get_string(PREF_TLS_CERTPATH); auto_gchar gchar* cert_path = prefs_get_string(PREF_TLS_CERTPATH);
gchar* cafile = cafile_get_name(); auto_gchar gchar* cafile = cafile_get_name();
gboolean insecure = FALSE; gboolean insecure = FALSE;
ProfAccount* account = accounts_get_account(session_get_account_name()); ProfAccount* account = accounts_get_account(session_get_account_name());
if (account) { if (account) {
@@ -201,7 +195,7 @@ http_file_put(void* userdata)
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "PUT"); curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "PUT");
struct curl_slist* headers = NULL; struct curl_slist* headers = NULL;
content_type_header = g_strdup_printf("Content-Type: %s", upload->mime_type); auto_gchar gchar* content_type_header = g_strdup_printf("Content-Type: %s", upload->mime_type);
if (!content_type_header) { if (!content_type_header) {
content_type_header = g_strdup(FALLBACK_CONTENTTYPE_HEADER); content_type_header = g_strdup(FALLBACK_CONTENTTYPE_HEADER);
} }
@@ -209,6 +203,7 @@ http_file_put(void* userdata)
headers = curl_slist_append(headers, "Expect:"); headers = curl_slist_append(headers, "Expect:");
// Optional headers // Optional headers
auto_gchar gchar* auth_header = NULL;
if (upload->authorization) { if (upload->authorization) {
auth_header = g_strdup_printf("Authorization: %s", upload->authorization); auth_header = g_strdup_printf("Authorization: %s", upload->authorization);
if (!auth_header) { if (!auth_header) {
@@ -216,6 +211,7 @@ http_file_put(void* userdata)
} }
headers = curl_slist_append(headers, auth_header); headers = curl_slist_append(headers, auth_header);
} }
auto_gchar gchar* cookie_header = NULL;
if (upload->cookie) { if (upload->cookie) {
cookie_header = g_strdup_printf("Cookie: %s", upload->cookie); cookie_header = g_strdup_printf("Cookie: %s", upload->cookie);
if (!cookie_header) { if (!cookie_header) {
@@ -223,6 +219,7 @@ http_file_put(void* userdata)
} }
headers = curl_slist_append(headers, cookie_header); headers = curl_slist_append(headers, cookie_header);
} }
auto_gchar gchar* expires_header = NULL;
if (upload->expires) { if (upload->expires) {
expires_header = g_strdup_printf("Expires: %s", upload->expires); expires_header = g_strdup_printf("Expires: %s", upload->expires);
if (!expires_header) { if (!expires_header) {
@@ -297,13 +294,8 @@ http_file_put(void* userdata)
fclose(fh); fclose(fh);
} }
free(output.buffer); free(output.buffer);
g_free(content_type_header);
g_free(auth_header);
g_free(cookie_header);
g_free(expires_header);
pthread_mutex_lock(&lock); pthread_mutex_lock(&lock);
g_free(cafile);
if (err) { if (err) {
gchar* msg; gchar* msg;

View File

@@ -160,12 +160,11 @@ cons_bad_cmd_usage(const char* const cmd)
void void
cons_show_error(const char* const msg, ...) cons_show_error(const char* const msg, ...)
{ {
ProfWin* console = wins_get_console();
va_list arg; va_list arg;
va_start(arg, msg); va_start(arg, msg);
GString* fmt_msg = g_string_new(NULL); GString* fmt_msg = g_string_new(NULL);
g_string_vprintf(fmt_msg, msg, arg); g_string_vprintf(fmt_msg, msg, arg);
win_println(console, THEME_ERROR, "-", "%s", fmt_msg->str); win_println(wins_get_console(), THEME_ERROR, "-", "%s", fmt_msg->str);
g_string_free(fmt_msg, TRUE); g_string_free(fmt_msg, TRUE);
va_end(arg); va_end(arg);

View File

@@ -93,7 +93,6 @@ static void
_ui_close(void) _ui_close(void)
{ {
g_timer_destroy(ui_idle_time); g_timer_destroy(ui_idle_time);
endwin();
notifier_uninit(); notifier_uninit();
cons_clear_alerts(); cons_clear_alerts();
wins_destroy(); wins_destroy();
@@ -102,6 +101,7 @@ _ui_close(void)
free_title_bar(); free_title_bar();
delwin(main_scr); delwin(main_scr);
delscreen(set_term(NULL)); delscreen(set_term(NULL));
endwin();
} }
void void

View File

@@ -69,8 +69,8 @@ static void
_chat_session_free(ChatSession* session) _chat_session_free(ChatSession* session)
{ {
if (session) { if (session) {
free(session->barejid);
free(session->resource); free(session->resource);
free(session->barejid);
free(session); free(session);
} }
} }

View File

@@ -2571,7 +2571,8 @@ iq_send_stanza(xmpp_stanza_t* const stanza)
if (plugin_text) { if (plugin_text) {
xmpp_send_raw_string(conn, "%s", plugin_text); xmpp_send_raw_string(conn, "%s", plugin_text);
} else { } else {
xmpp_send_raw_string(conn, "%s", text); /* libstrophe does the strlen for us, so simply always pass SIZE_MAX */
xmpp_send_raw(conn, text, SIZE_MAX);
} }
xmpp_free(connection_get_ctx(), text); xmpp_free(connection_get_ctx(), text);
} }

View File

@@ -546,7 +546,7 @@ prof_occurrences_of_large_message_tests(void** state)
char* haystack = malloc(haystack_sz); char* haystack = malloc(haystack_sz);
const char needle[] = "needle "; const char needle[] = "needle ";
while (haystack_sz - haystack_cur > sizeof(needle)) { while (haystack_sz - haystack_cur > sizeof(needle)) {
memcpy(&haystack[haystack_cur], needle, sizeof(needle) - 1); memcpy(&haystack[haystack_cur], needle, sizeof(needle));
expected = g_slist_append(expected, GINT_TO_POINTER(haystack_cur)); expected = g_slist_append(expected, GINT_TO_POINTER(haystack_cur));
haystack_cur += sizeof(needle) - 1; haystack_cur += sizeof(needle) - 1;
} }