From c3847eec34476fa15d8bc50671f1377a0cb27fd8 Mon Sep 17 00:00:00 2001 From: Immae Date: Mon, 23 Jun 2014 12:05:49 +0200 Subject: [PATCH 1/7] Free some memory --- src/contact.c | 2 ++ src/ui/core.c | 1 + 2 files changed, 3 insertions(+) diff --git a/src/contact.c b/src/contact.c index 52942bec..839e7d10 100644 --- a/src/contact.c +++ b/src/contact.c @@ -240,6 +240,8 @@ _get_most_available_resource(PContact contact) resources = g_list_next(resources); } + free(resources); + free(current); return highest; } diff --git a/src/ui/core.c b/src/ui/core.c index 84fcebfd..30f79659 100644 --- a/src/ui/core.c +++ b/src/ui/core.c @@ -137,6 +137,7 @@ _ui_get_idle_time(void) XFree(info); return result; } + XFree(info); // if no libxss or xss idle time failed, use profanity idle time #endif gdouble seconds_elapsed = g_timer_elapsed(ui_idle_time, NULL); From 98bc1abbe57214b6d78de94323b1433ab5fc753d Mon Sep 17 00:00:00 2001 From: James Booth Date: Mon, 23 Jun 2014 21:17:07 +0100 Subject: [PATCH 2/7] Free GError on command line option parse --- src/main.c | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main.c b/src/main.c index 5c95a994..140df545 100644 --- a/src/main.c +++ b/src/main.c @@ -82,6 +82,7 @@ main(int argc, char **argv) if (!g_option_context_parse(context, &argc, &argv, &error)) { g_print("%s\n", error->message); g_option_context_free(context); + g_error_free(error); return 1; } From 83fc97621c2828d1fcbe20e95d258fdaab71c9eb Mon Sep 17 00:00:00 2001 From: James Booth Date: Tue, 24 Jun 2014 22:23:53 +0100 Subject: [PATCH 3/7] Check for null before freeing X11info, removed free current resource --- src/contact.c | 1 - src/ui/core.c | 4 +++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/contact.c b/src/contact.c index 839e7d10..c57d1902 100644 --- a/src/contact.c +++ b/src/contact.c @@ -241,7 +241,6 @@ _get_most_available_resource(PContact contact) resources = g_list_next(resources); } free(resources); - free(current); return highest; } diff --git a/src/ui/core.c b/src/ui/core.c index 30f79659..254af048 100644 --- a/src/ui/core.c +++ b/src/ui/core.c @@ -137,7 +137,9 @@ _ui_get_idle_time(void) XFree(info); return result; } - XFree(info); + if (info != NULL) { + XFree(info); + } // if no libxss or xss idle time failed, use profanity idle time #endif gdouble seconds_elapsed = g_timer_elapsed(ui_idle_time, NULL); From 8cf7122e7a10352161450f3ca80b1b6e4738d7c1 Mon Sep 17 00:00:00 2001 From: James Booth Date: Tue, 24 Jun 2014 23:08:35 +0100 Subject: [PATCH 4/7] Use g_list_free() when finding resource availability --- src/contact.c | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/contact.c b/src/contact.c index c57d1902..f2257b9a 100644 --- a/src/contact.c +++ b/src/contact.c @@ -223,11 +223,12 @@ _get_most_available_resource(PContact contact) // xa // dnd GList *resources = g_hash_table_get_values(contact->available_resources); - Resource *current = resources->data; + GList *curr = resources; + Resource *current = curr->data; Resource *highest = current; - resources = g_list_next(resources); - while (resources != NULL) { - current = resources->data; + curr = g_list_next(curr); + while (curr != NULL) { + current = curr->data; // priority is same as current highest, choose presence if (current->priority == highest->priority) { @@ -238,9 +239,9 @@ _get_most_available_resource(PContact contact) highest = current; } - resources = g_list_next(resources); + curr = g_list_next(curr); } - free(resources); + g_list_free(resources); return highest; } From c98ce4299de670dd5d270308bf7137ea05d9c763 Mon Sep 17 00:00:00 2001 From: James Booth Date: Thu, 26 Jun 2014 23:55:57 +0100 Subject: [PATCH 5/7] Free resources on OTR shutdown --- src/command/commands.c | 1 + src/otr/otr.c | 17 +++++++++++++++-- src/otr/otr.h | 1 + src/profanity.c | 3 +++ 4 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/command/commands.c b/src/command/commands.c index f979da41..3e7dffd9 100644 --- a/src/command/commands.c +++ b/src/command/commands.c @@ -2766,6 +2766,7 @@ cmd_otr(gchar **args, struct cmd_help_t help) if (strcmp(args[0], "gen") == 0) { ProfAccount *account = accounts_get_account(jabber_get_account_name()); otr_keygen(account); + account_free(account); return TRUE; } else if (strcmp(args[0], "myfp") == 0) { diff --git a/src/otr/otr.c b/src/otr/otr.c index b6b68dcd..c9280642 100644 --- a/src/otr/otr.c +++ b/src/otr/otr.c @@ -163,6 +163,14 @@ _otr_init(void) data_loaded = FALSE; } +static void +_otr_shutdown(void) +{ + if (jid != NULL) { + free(jid); + } +} + void _otr_poll(void) { @@ -172,6 +180,9 @@ _otr_poll(void) static void _otr_on_connect(ProfAccount *account) { + if (jid != NULL) { + free(jid); + } jid = strdup(account->jid); log_info("Loading OTR key for %s", jid); @@ -253,11 +264,12 @@ _otr_keygen(ProfAccount *account) return; } + if (jid != NULL) { + free(jid); + } jid = strdup(account->jid); log_info("Generating OTR key for %s", jid); - jid = strdup(account->jid); - gchar *data_home = xdg_get_data_home(); GString *basedir = g_string_new(data_home); free(data_home); @@ -634,6 +646,7 @@ void otr_init_module(void) { otr_init = _otr_init; + otr_shutdown = _otr_shutdown; otr_libotr_version = _otr_libotr_version; otr_start_query = _otr_start_query; otr_poll = _otr_poll; diff --git a/src/otr/otr.h b/src/otr/otr.h index a8553280..0df19f42 100644 --- a/src/otr/otr.h +++ b/src/otr/otr.h @@ -41,6 +41,7 @@ OtrlMessageAppOps* otr_messageops(void); GHashTable* otr_smpinitators(void); void (*otr_init)(void); +void (*otr_shutdown)(void); char* (*otr_libotr_version)(void); char* (*otr_start_query)(void); void (*otr_poll)(void); diff --git a/src/profanity.c b/src/profanity.c index bcf4f93c..1692fceb 100644 --- a/src/profanity.c +++ b/src/profanity.c @@ -305,6 +305,9 @@ _shutdown(void) muc_close(); caps_close(); ui_close(); +#ifdef HAVE_LIBOTR + otr_shutdown(); +#endif chat_log_close(); prefs_close(); theme_close(); From 186cac34deb9f1469d5d2719b5a5746ff26156e2 Mon Sep 17 00:00:00 2001 From: James Booth Date: Fri, 27 Jun 2014 00:38:53 +0100 Subject: [PATCH 6/7] Fixed memleak in chat log history --- src/log.c | 19 +++++++++++-------- src/log.h | 2 +- src/ui/core.c | 10 +++++----- tests/log/mock_log.c | 2 +- 4 files changed, 18 insertions(+), 15 deletions(-) diff --git a/src/log.c b/src/log.c index 77ed5a6d..41ca8507 100644 --- a/src/log.c +++ b/src/log.c @@ -330,9 +330,9 @@ groupchat_log_chat(const gchar * const login, const gchar * const room, GSList * -chat_log_get_previous(const gchar * const login, const gchar * const recipient, - GSList *history) +chat_log_get_previous(const gchar * const login, const gchar * const recipient) { + GSList *history = NULL; GDateTime *now = g_date_time_new_now_local(); GDateTime *log_date = g_date_time_new(tz, g_date_time_get_year(session_started), @@ -348,14 +348,13 @@ chat_log_get_previous(const gchar * const login, const gchar * const recipient, FILE *logp = fopen(filename, "r"); if (logp != NULL) { - GString *gs_header = g_string_new(""); - g_string_append_printf(gs_header, "%d/%d/%d:", + GString *header = g_string_new(""); + g_string_append_printf(header, "%d/%d/%d:", g_date_time_get_day_of_month(log_date), g_date_time_get_month(log_date), g_date_time_get_year(log_date)); - char *header = strdup(gs_header->str); - history = g_slist_append(history, header); - g_string_free(gs_header, TRUE); + history = g_slist_append(history, header->str); + g_string_free(header, FALSE); char *line; while ((line = prof_getline(logp)) != NULL) { @@ -366,11 +365,15 @@ chat_log_get_previous(const gchar * const login, const gchar * const recipient, } free(filename); + GDateTime *next = g_date_time_add_days(log_date, 1); g_date_time_unref(log_date); - log_date = g_date_time_ref(next); + log_date = next; } + g_date_time_unref(log_date); + g_date_time_unref(now); + return history; } diff --git a/src/log.h b/src/log.h index d9cd92e1..f43b9772 100644 --- a/src/log.h +++ b/src/log.h @@ -56,7 +56,7 @@ void chat_log_chat(const gchar * const login, gchar *other, const gchar * const msg, chat_log_direction_t direction, GTimeVal *tv_stamp); void chat_log_close(void); GSList * chat_log_get_previous(const gchar * const login, - const gchar * const recipient, GSList *history); + const gchar * const recipient); void groupchat_log_init(void); void groupchat_log_chat(const gchar * const login, const gchar * const room, diff --git a/src/ui/core.c b/src/ui/core.c index 254af048..262eb4d3 100644 --- a/src/ui/core.c +++ b/src/ui/core.c @@ -2131,13 +2131,13 @@ _win_show_history(WINDOW *win, int win_index, const char * const contact) { ProfWin *window = wins_get_by_num(win_index); if (!window->history_shown) { - GSList *history = NULL; Jid *jid = jid_create(jabber_get_fulljid()); - history = chat_log_get_previous(jid->barejid, contact, history); + GSList *history = chat_log_get_previous(jid->barejid, contact); jid_destroy(jid); - while (history != NULL) { - wprintw(win, "%s\n", history->data); - history = g_slist_next(history); + GSList *curr = history; + while (curr != NULL) { + wprintw(win, "%s\n", curr->data); + curr = g_slist_next(curr); } window->history_shown = 1; diff --git a/tests/log/mock_log.c b/tests/log/mock_log.c index d423b5bb..8ace164a 100644 --- a/tests/log/mock_log.c +++ b/tests/log/mock_log.c @@ -54,7 +54,7 @@ void chat_log_chat(const gchar * const login, gchar *other, const gchar * const msg, chat_log_direction_t direction, GTimeVal *tv_stamp) {} void chat_log_close(void) {} GSList * chat_log_get_previous(const gchar * const login, - const gchar * const recipient, GSList *history) + const gchar * const recipient) { return mock_ptr_type(GSList *); } From 03ffe57063c34d2e4b79765a9cf84d0e7e3b4c4c Mon Sep 17 00:00:00 2001 From: James Booth Date: Fri, 27 Jun 2014 00:47:11 +0100 Subject: [PATCH 7/7] Fixed memleak in title bar roster_find_contact --- src/ui/titlebar.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/ui/titlebar.c b/src/ui/titlebar.c index a273ea46..812eb773 100644 --- a/src/ui/titlebar.c +++ b/src/ui/titlebar.c @@ -166,8 +166,10 @@ _title_bar_draw(void) // show privacy if (current_recipient != NULL) { char *recipient_jid = NULL; - if (roster_find_contact(current_recipient) != NULL) { + char *found_contact = roster_find_contact(current_recipient); + if (found_contact != NULL) { recipient_jid = roster_barejid_from_name(current_recipient); + free(found_contact); } else { recipient_jid = current_recipient; }