From e886ba0c6fed433b561c4d0efb4f91b0681aa788 Mon Sep 17 00:00:00 2001 From: Steffen Jaeckel Date: Fri, 30 Dec 2022 19:36:53 +0100 Subject: [PATCH] fix more memory leaks Signed-off-by: Steffen Jaeckel --- src/command/cmd_ac.c | 4 ++-- src/command/cmd_funcs.c | 12 +++++++----- src/database.c | 14 +++++++++++--- src/tools/editor.c | 2 +- src/ui/window.c | 2 +- src/xmpp/iq.c | 25 +++++++++++++++---------- src/xmpp/session.c | 2 +- 7 files changed, 38 insertions(+), 23 deletions(-) diff --git a/src/command/cmd_ac.c b/src/command/cmd_ac.c index 559b3bda..e48e0ae2 100644 --- a/src/command/cmd_ac.c +++ b/src/command/cmd_ac.c @@ -2849,7 +2849,7 @@ _omemo_autocomplete(ProfWin* window, const char* const input, gboolean previous) gboolean result; gchar** args = parse_args(input, 2, 3, &result); if (result) { - gchar* jid = g_strdup(args[1]); + auto_gchar gchar* jid = g_strdup(args[1]); found = autocomplete_param_no_with_func(input, "/omemo trust", 4, omemo_fingerprint_autocomplete, previous, jid); if (found) { return found; @@ -2876,7 +2876,7 @@ _omemo_autocomplete(ProfWin* window, const char* const input, gboolean previous) gboolean result; gchar** args = parse_args(input, 2, 3, &result); if (result) { - gchar* jid = g_strdup(args[1]); + auto_gchar gchar* jid = g_strdup(args[1]); found = autocomplete_param_no_with_func(input, "/omemo untrust", 4, omemo_fingerprint_autocomplete, previous, jid); if (found) { return found; diff --git a/src/command/cmd_funcs.c b/src/command/cmd_funcs.c index bbfb3cb8..56a18bdb 100644 --- a/src/command/cmd_funcs.c +++ b/src/command/cmd_funcs.c @@ -8436,18 +8436,19 @@ _cmd_execute(ProfWin* window, const char* const command, const char* const inp) int i = 0; while (cmd->sub_funcs[i].cmd) { if (g_strcmp0(args[0], (char*)cmd->sub_funcs[i].cmd) == 0) { - gboolean result = cmd->sub_funcs[i].func(window, command, args); - g_strfreev(args); - return result; + result = cmd->sub_funcs[i].func(window, command, args); + goto out; } i++; } } if (!cmd->func) { ui_invalid_command_usage(cmd->cmd, cmd->setting_func); - return TRUE; + result = TRUE; + goto out; } - gboolean result = cmd->func(window, command, args); + result = cmd->func(window, command, args); +out: g_strfreev(args); return result; } else if (plugins_run_command(inp)) { @@ -8887,6 +8888,7 @@ cmd_omemo_fingerprint(ProfWin* window, const char* const command, gchar** args) free(formatted_fingerprint); } + jid_destroy(jid); g_list_free(fingerprints); win_println(window, THEME_DEFAULT, "-", "You can trust it with '/omemo trust '"); diff --git a/src/database.c b/src/database.c index 13946e8d..ce936170 100644 --- a/src/database.c +++ b/src/database.c @@ -57,6 +57,16 @@ static char* _get_db_filename(ProfAccount* account); static prof_msg_type_t _get_message_type_type(const char* const type); static prof_enc_t _get_message_enc_type(const char* const encstr); +#define auto_sqlite __attribute__((__cleanup__(auto_free_sqlite))) + +static void +auto_free_sqlite(gchar** str) +{ + if (str == NULL) + return; + sqlite3_free(*str); +} + static char* _get_db_filename(ProfAccount* account) { @@ -243,7 +253,6 @@ GSList* log_database_get_previous_chat(const gchar* const contact_barejid, char* start_time, char* end_time, gboolean from_start, gboolean flip) { sqlite3_stmt* stmt = NULL; - gchar* query; const char* jid = connection_get_fulljid(); Jid* myjid = jid_create(jid); if (!myjid) @@ -254,7 +263,7 @@ log_database_get_previous_chat(const gchar* const contact_barejid, char* start_t gchar* sort2 = !flip ? "ASC" : "DESC"; GDateTime* now = g_date_time_new_now_local(); gchar* end_date_fmt = end_time ? end_time : g_date_time_format_iso8601(now); - query = sqlite3_mprintf("SELECT * FROM (SELECT COALESCE(B.`message`, A.`message`) AS message, A.`timestamp`, A.`from_jid`, A.`type`, A.`encryption` from `ChatLogs` AS A LEFT JOIN `ChatLogs` AS B ON A.`stanza_id` = B.`replace_id` WHERE A.`replace_id` = '' AND ((A.`from_jid` = '%q' AND A.`to_jid` = '%q') OR (A.`from_jid` = '%q' AND A.`to_jid` = '%q')) AND A.`timestamp` < '%q' AND (%Q IS NULL OR A.`timestamp` > %Q) ORDER BY A.`timestamp` %s LIMIT %d) ORDER BY `timestamp` %s;", contact_barejid, myjid->barejid, myjid->barejid, contact_barejid, end_date_fmt, start_time, start_time, sort1, MESSAGES_TO_RETRIEVE, sort2); + auto_sqlite gchar* query = sqlite3_mprintf("SELECT * FROM (SELECT COALESCE(B.`message`, A.`message`) AS message, A.`timestamp`, A.`from_jid`, A.`type`, A.`encryption` from `ChatLogs` AS A LEFT JOIN `ChatLogs` AS B ON A.`stanza_id` = B.`replace_id` WHERE A.`replace_id` = '' AND ((A.`from_jid` = '%q' AND A.`to_jid` = '%q') OR (A.`from_jid` = '%q' AND A.`to_jid` = '%q')) AND A.`timestamp` < '%q' AND (%Q IS NULL OR A.`timestamp` > %Q) ORDER BY A.`timestamp` %s LIMIT %d) ORDER BY `timestamp` %s;", contact_barejid, myjid->barejid, myjid->barejid, contact_barejid, end_date_fmt, start_time, start_time, sort1, MESSAGES_TO_RETRIEVE, sort2); g_date_time_unref(now); g_free(end_date_fmt); @@ -292,7 +301,6 @@ log_database_get_previous_chat(const gchar* const contact_barejid, char* start_t history = g_slist_append(history, msg); } sqlite3_finalize(stmt); - sqlite3_free(query); return history; } diff --git a/src/tools/editor.c b/src/tools/editor.c index 4ac88a95..350fb5b1 100644 --- a/src/tools/editor.c +++ b/src/tools/editor.c @@ -56,7 +56,7 @@ get_message_from_editor(gchar* message, gchar** returned_message) gchar* filename = NULL; GError* glib_error = NULL; - char* jid = connection_get_barejid(); + auto_char char* jid = connection_get_barejid(); if (jid) { filename = files_file_in_account_data_path(DIR_EDITOR, jid, "compose.md"); } else { diff --git a/src/ui/window.c b/src/ui/window.c index 18445f57..9d7b779e 100644 --- a/src/ui/window.c +++ b/src/ui/window.c @@ -1470,7 +1470,7 @@ win_print_outgoing(ProfWin* window, const char* show_char, const char* const id, if (replace_id) { _win_correct(window, message, id, replace_id, myjid); } else { - char* outgoing_str = prefs_get_string(PREF_OUTGOING_STAMP); + auto_char gchar* outgoing_str = prefs_get_string(PREF_OUTGOING_STAMP); _win_printf(window, show_char, 0, timestamp, 0, THEME_TEXT_ME, outgoing_str, myjid, id, "%s", message); } diff --git a/src/xmpp/iq.c b/src/xmpp/iq.c index 4d0ee281..89511176 100644 --- a/src/xmpp/iq.c +++ b/src/xmpp/iq.c @@ -2187,6 +2187,7 @@ _room_info_response_id_handler(xmpp_stanza_t* const stanza, void* const userdata identity->name = strdup(name); ProfMucWin* mucwin = wins_get_muc(cb_data->room); if (mucwin) { + free(mucwin->room_name); mucwin->room_name = strdup(name); } } else { @@ -2607,6 +2608,8 @@ _mam_buffer_commit_handler(xmpp_stanza_t* const stanza, void* const userdata) return 0; } +static const gchar* mam_timestamp_format_string = "%FT%T.%f%:z"; + void iq_mam_request_older(ProfChatWin* win) { @@ -2623,7 +2626,7 @@ iq_mam_request_older(ProfChatWin* win) // If first message found if (first_msg->timestamp) { firstid = first_msg->stanzaid; - enddate = g_date_time_format(first_msg->timestamp, "%FT%T.%f%:z"); + enddate = g_date_time_format(first_msg->timestamp, mam_timestamp_format_string); } else { return; } @@ -2647,6 +2650,8 @@ _iq_mam_request(ProfChatWin* win, GDateTime* startdate, GDateTime* enddate) if (connection_supports(XMPP_FEATURE_MAM2) == FALSE) { log_warning("Server doesn't advertise %s feature.", XMPP_FEATURE_MAM2); cons_show_error("Server doesn't support MAM (%s).", XMPP_FEATURE_MAM2); + g_date_time_unref(startdate); + g_date_time_unref(enddate); return; } @@ -2656,18 +2661,18 @@ _iq_mam_request(ProfChatWin* win, GDateTime* startdate, GDateTime* enddate) gboolean fetch_next = FALSE; if (startdate) { - startdate_str = g_date_time_format(startdate, "%FT%T.%f%:z"); + startdate_str = g_date_time_format(startdate, mam_timestamp_format_string); fetch_next = TRUE; g_date_time_unref(startdate); - } else if (!enddate) { - GDateTime* now = g_date_time_new_now_utc(); - enddate_str = g_date_time_format(now, "%FT%T.%f%:z"); - g_date_time_unref(now); } if (enddate) { - enddate_str = g_date_time_format(enddate, "%FT%T.%f%:z"); + enddate_str = g_date_time_format(enddate, mam_timestamp_format_string); g_date_time_unref(enddate); + } else { + GDateTime* now = g_date_time_new_now_utc(); + enddate_str = g_date_time_format(now, mam_timestamp_format_string); + g_date_time_unref(now); } xmpp_ctx_t* const ctx = connection_get_ctx(); @@ -2695,15 +2700,15 @@ void iq_mam_request(ProfChatWin* win, GDateTime* enddate) { ProfMessage* last_msg = log_database_get_limits_info(win->barejid, TRUE); - GDateTime* startdate = last_msg->timestamp ? g_date_time_add_seconds(last_msg->timestamp, 0) : NULL; // copy timestamp + GDateTime* startdate = g_date_time_add_seconds(last_msg->timestamp, 0); message_free(last_msg); // Save request for later if disco items haven't been received yet if (!received_disco_items) { LateDeliveryUserdata* cur_del_data = malloc(sizeof(LateDeliveryUserdata)); cur_del_data->win = win; - cur_del_data->enddate = enddate; - cur_del_data->startdate = startdate; + cur_del_data->enddate = g_date_time_ref(enddate); + cur_del_data->startdate = g_date_time_ref(startdate); late_delivery_windows = g_slist_append(late_delivery_windows, cur_del_data); } diff --git a/src/xmpp/session.c b/src/xmpp/session.c index 892cbbf2..c4ea3f24 100644 --- a/src/xmpp/session.c +++ b/src/xmpp/session.c @@ -305,7 +305,7 @@ _receive_mood(xmpp_stanza_t* const stanza, void* const userdata) const char* m = xmpp_stanza_get_name(c); xmpp_stanza_t* t = xmpp_stanza_get_child_by_name(mood, STANZA_NAME_TEXT); if (t) { - const char* text = xmpp_stanza_get_text(t); + auto_char char* text = xmpp_stanza_get_text(t); cons_show("Mood from %s %s (%s)", from, m, text); } else { cons_show("Mood from %s %s", from, m);