From b1bfdf650a93c84b92fef02fdd8441c1b2002e66 Mon Sep 17 00:00:00 2001 From: Dmitry Podgorny Date: Sun, 25 Aug 2013 14:54:34 +0300 Subject: fixed memory leaks in various files --- src/config/accounts.c | 61 +++++++++++++++++++++++++++++++++++---------------- 1 file changed, 42 insertions(+), 19 deletions(-) (limited to 'src/config/accounts.c') diff --git a/src/config/accounts.c b/src/config/accounts.c index c82f6b2d..3d198874 100644 --- a/src/config/accounts.c +++ b/src/config/accounts.c @@ -72,10 +72,7 @@ accounts_load(void) _fix_legacy_accounts(account_names[i]); } - for (i = 0; i < naccounts; i++) { - free(account_names[i]); - } - free(account_names); + g_strfreev(account_names); } void @@ -166,6 +163,7 @@ accounts_get_account(const char * const name) gchar *jid = g_key_file_get_string(accounts, name, "jid", NULL); if (jid != NULL) { account->jid = strdup(jid); + g_free(jid); } else { account->jid = strdup(name); g_key_file_set_string(accounts, name, "jid", name); @@ -177,6 +175,7 @@ accounts_get_account(const char * const name) gchar *server = g_key_file_get_string(accounts, name, "server", NULL); if (server != NULL) { account->server = strdup(server); + g_free(server); } else { account->server = NULL; } @@ -184,6 +183,7 @@ accounts_get_account(const char * const name) gchar *resource = g_key_file_get_string(accounts, name, "resource", NULL); if (resource != NULL) { account->resource = strdup(resource); + g_free(resource); } else { account->resource = NULL; } @@ -195,6 +195,10 @@ accounts_get_account(const char * const name) account->last_presence = strdup(presence); } + if (presence != NULL) { + g_free(presence); + } + presence = g_key_file_get_string(accounts, name, "presence.login", NULL); if (presence == NULL) { account->login_presence = strdup("online"); @@ -206,6 +210,10 @@ accounts_get_account(const char * const name) account->login_presence = strdup(presence); } + if (presence != NULL) { + g_free(presence); + } + account->priority_online = g_key_file_get_integer(accounts, name, "priority.online", NULL); account->priority_chat = g_key_file_get_integer(accounts, name, "priority.chat", NULL); account->priority_away = g_key_file_get_integer(accounts, name, "priority.away", NULL); @@ -302,7 +310,7 @@ accounts_rename(const char * const account_name, const char * const new_name) char *value = g_key_file_get_string(accounts, account_name, string_keys[i], NULL); if (value != NULL) { g_key_file_set_string(accounts, new_name, string_keys[i], value); - free(value); + g_free(value); } } @@ -469,45 +477,59 @@ accounts_set_login_presence(const char * const account_name, const char * const resource_presence_t accounts_get_last_presence(const char * const account_name) { + resource_presence_t result; gchar *setting = g_key_file_get_string(accounts, account_name, "presence.last", NULL); + if (setting == NULL || (strcmp(setting, "online") == 0)) { - return RESOURCE_ONLINE; + result = RESOURCE_ONLINE; } else if (strcmp(setting, "chat") == 0) { - return RESOURCE_CHAT; + result = RESOURCE_CHAT; } else if (strcmp(setting, "away") == 0) { - return RESOURCE_AWAY; + result = RESOURCE_AWAY; } else if (strcmp(setting, "xa") == 0) { - return RESOURCE_XA; + result = RESOURCE_XA; } else if (strcmp(setting, "dnd") == 0) { - return RESOURCE_DND; + result = RESOURCE_DND; } else { log_warning("Error reading presence.last for account: '%s', value: '%s', defaulting to 'online'", account_name, setting); - return RESOURCE_ONLINE; + result = RESOURCE_ONLINE; } + + if (setting != NULL) { + g_free(setting); + } + return result; } resource_presence_t accounts_get_login_presence(const char * const account_name) { + resource_presence_t result; gchar *setting = g_key_file_get_string(accounts, account_name, "presence.login", NULL); + if (setting == NULL || (strcmp(setting, "online") == 0)) { - return RESOURCE_ONLINE; + result = RESOURCE_ONLINE; } else if (strcmp(setting, "chat") == 0) { - return RESOURCE_CHAT; + result = RESOURCE_CHAT; } else if (strcmp(setting, "away") == 0) { - return RESOURCE_AWAY; + result = RESOURCE_AWAY; } else if (strcmp(setting, "xa") == 0) { - return RESOURCE_XA; + result = RESOURCE_XA; } else if (strcmp(setting, "dnd") == 0) { - return RESOURCE_DND; + result = RESOURCE_DND; } else if (strcmp(setting, "last") == 0) { - return accounts_get_last_presence(account_name); + result = accounts_get_last_presence(account_name); } else { log_warning("Error reading presence.login for account: '%s', value: '%s', defaulting to 'online'", account_name, setting); - return RESOURCE_ONLINE; + result = RESOURCE_ONLINE; } + + if (setting != NULL) { + g_free(setting); + } + return result; } static void @@ -543,8 +565,9 @@ static void _save_accounts(void) { gsize g_data_size; - char *g_accounts_data = g_key_file_to_data(accounts, &g_data_size, NULL); + gchar *g_accounts_data = g_key_file_to_data(accounts, &g_data_size, NULL); g_file_set_contents(accounts_loc, g_accounts_data, g_data_size, NULL); + g_free(g_accounts_data); } static gchar * -- cgit 1.4.1-2-gfad0