From a6e66cc57106402944445fc709625dcf88785a5b Mon Sep 17 00:00:00 2001 From: Dmitry Podgorny Date: Sat, 3 Aug 2013 14:38:38 +0300 Subject: [PATCH] fixed memory leaks Also avoided several NULL pointer dereferences. --- src/ui/notifier.c | 6 ++--- src/xmpp/capabilities.c | 32 +++++++++++----------- src/xmpp/iq.c | 20 +++++++------- src/xmpp/message.c | 6 +++++ src/xmpp/presence.c | 60 +++++++++++++++++++++++++++++++++-------- 5 files changed, 85 insertions(+), 39 deletions(-) diff --git a/src/ui/notifier.c b/src/ui/notifier.c index f43c8c25..403c215e 100644 --- a/src/ui/notifier.c +++ b/src/ui/notifier.c @@ -80,7 +80,7 @@ notify_invite(const char * const from, const char * const room, _notify(message->str, 10000, "Incoming message"); - g_string_free(message, FALSE); + g_string_free(message, TRUE); } void @@ -102,7 +102,7 @@ notify_room_message(const char * const handle, const char * const room, int win) _notify(text->str, 10000, "incoming message"); - g_string_free(text, FALSE); + g_string_free(text, TRUE); } void @@ -111,7 +111,7 @@ notify_subscription(const char * const from) GString *message = g_string_new("Subscription request: \n"); g_string_append(message, from); _notify(message->str, 10000, "Incomming message"); - g_string_free(message, FALSE); + g_string_free(message, TRUE); } void diff --git a/src/xmpp/capabilities.c b/src/xmpp/capabilities.c index f9624f90..ed3cf169 100644 --- a/src/xmpp/capabilities.c +++ b/src/xmpp/capabilities.c @@ -122,7 +122,7 @@ caps_create_sha1_str(xmpp_stanza_t * const query) GSList *form_names = NULL; DataForm *form = NULL; FormField *field = NULL; - GHashTable *forms = g_hash_table_new_full(g_str_hash, g_str_equal, free, (GDestroyNotify)stanza_destroy_form); + GHashTable *forms = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, (GDestroyNotify)stanza_destroy_form); GString *s = g_string_new(""); @@ -134,18 +134,18 @@ caps_create_sha1_str(xmpp_stanza_t * const query) lang = xmpp_stanza_get_attribute(child, "xml:lang"); name = xmpp_stanza_get_attribute(child, "name"); - GString *identity_str = g_string_new(g_strdup(category)); + GString *identity_str = g_string_new(category); g_string_append(identity_str, "/"); if (type != NULL) { - g_string_append(identity_str, g_strdup(type)); + g_string_append(identity_str, type); } g_string_append(identity_str, "/"); if (lang != NULL) { - g_string_append(identity_str, g_strdup(lang)); + g_string_append(identity_str, lang); } g_string_append(identity_str, "/"); if (name != NULL) { - g_string_append(identity_str, g_strdup(name)); + g_string_append(identity_str, name); } g_string_append(identity_str, "<"); identities = g_slist_insert_sorted(identities, g_strdup(identity_str->str), (GCompareFunc)octet_compare); @@ -156,8 +156,8 @@ caps_create_sha1_str(xmpp_stanza_t * const query) } else if (g_strcmp0(xmpp_stanza_get_name(child), STANZA_NAME_X) == 0) { if (strcmp(xmpp_stanza_get_ns(child), STANZA_NS_DATA) == 0) { form = stanza_create_form(child); - form_names = g_slist_insert_sorted(form_names, strdup(form->form_type), (GCompareFunc)octet_compare); - g_hash_table_insert(forms, strdup(form->form_type), form); + form_names = g_slist_insert_sorted(form_names, g_strdup(form->form_type), (GCompareFunc)octet_compare); + g_hash_table_insert(forms, g_strdup(form->form_type), form); } } child = xmpp_stanza_get_next(child); @@ -165,13 +165,13 @@ caps_create_sha1_str(xmpp_stanza_t * const query) GSList *curr = identities; while (curr != NULL) { - g_string_append(s, strdup(curr->data)); + g_string_append(s, curr->data); curr = g_slist_next(curr); } curr = features; while (curr != NULL) { - g_string_append(s, strdup(curr->data)); + g_string_append(s, curr->data); g_string_append(s, "<"); curr = g_slist_next(curr); } @@ -179,17 +179,17 @@ caps_create_sha1_str(xmpp_stanza_t * const query) curr = form_names; while (curr != NULL) { form = g_hash_table_lookup(forms, curr->data); - g_string_append(s, strdup(form->form_type)); + g_string_append(s, form->form_type); g_string_append(s, "<"); GSList *curr_field = form->fields; while (curr_field != NULL) { field = curr_field->data; - g_string_append(s, strdup(field->var)); + g_string_append(s, field->var); g_string_append(s, "<"); GSList *curr_value = field->values; while (curr_value != NULL) { - g_string_append(s, strdup(curr_value->data)); + g_string_append(s, curr_value->data); g_string_append(s, "<"); curr_value = g_slist_next(curr_value); } @@ -215,10 +215,10 @@ caps_create_sha1_str(xmpp_stanza_t * const query) char *result = g_base64_encode(md_value, md_len); g_string_free(s, TRUE); - g_slist_free_full(identities, free); - g_slist_free_full(features, free); - g_slist_free_full(form_names, free); - //g_hash_table_destroy(forms); + g_slist_free_full(identities, g_free); + g_slist_free_full(features, g_free); + g_slist_free_full(form_names, g_free); + g_hash_table_destroy(forms); return result; } diff --git a/src/xmpp/iq.c b/src/xmpp/iq.c index 48b561f0..742a2258 100644 --- a/src/xmpp/iq.c +++ b/src/xmpp/iq.c @@ -312,10 +312,10 @@ static void _identity_destroy(DiscoIdentity *identity) { if (identity != NULL) { - FREE_SET_NULL(identity->name); - FREE_SET_NULL(identity->type); - FREE_SET_NULL(identity->category); - FREE_SET_NULL(identity); + free(identity->name); + free(identity->type); + free(identity->category); + free(identity); } } @@ -323,9 +323,9 @@ static void _item_destroy(DiscoItem *item) { if (item != NULL) { - FREE_SET_NULL(item->jid); - FREE_SET_NULL(item->name); - FREE_SET_NULL(item); + free(item->jid); + free(item->name); + free(item); } } @@ -411,12 +411,13 @@ _iq_handle_discoinfo_result(xmpp_conn_t * const conn, xmpp_stanza_t * const stan log_info("Generated sha-1 does not match given:"); log_info("Generated : %s", generated_sha1); log_info("Given : %s", given_sha1); - FREE_SET_NULL(generated_sha1); + g_free(generated_sha1); g_strfreev(split); + free(caps_key); return 1; } - FREE_SET_NULL(generated_sha1); + g_free(generated_sha1); g_strfreev(split); // non supported hash, or legacy caps @@ -429,6 +430,7 @@ _iq_handle_discoinfo_result(xmpp_conn_t * const conn, xmpp_stanza_t * const stan // already cached if (caps_contains(caps_key)) { log_info("Client info already cached."); + free(caps_key); return 1; } diff --git a/src/xmpp/message.c b/src/xmpp/message.c index 29d11958..95b3152a 100644 --- a/src/xmpp/message.c +++ b/src/xmpp/message.c @@ -212,6 +212,9 @@ _conference_message_handler(xmpp_conn_t * const conn, } Jid *jidp = jid_create(invitor_jid); + if (jidp == NULL) { + return 1; + } invitor = jidp->barejid; xmpp_stanza_t *reason_st = xmpp_stanza_get_child_by_name(invite, STANZA_NAME_REASON); @@ -233,6 +236,9 @@ _conference_message_handler(xmpp_conn_t * const conn, } Jid *jidp = jid_create(from); + if (jidp == NULL) { + return 1; + } invitor = jidp->barejid; reason = xmpp_stanza_get_attribute(x_groupchat, STANZA_ATTR_REASON); diff --git a/src/xmpp/presence.c b/src/xmpp/presence.c index 6c87a8ac..93870088 100644 --- a/src/xmpp/presence.c +++ b/src/xmpp/presence.c @@ -156,14 +156,23 @@ presence_sub_request_find(char * search_str) gboolean presence_sub_request_exists(const char * const bare_jid) { - GSList *requests = autocomplete_get_list(sub_requests_ac); + gboolean result = FALSE; + GSList *requests_p = autocomplete_get_list(sub_requests_ac); + GSList *requests = requests_p; + while (requests != NULL) { if (strcmp(requests->data, bare_jid) == 0) { - return TRUE; + result = TRUE; + break; } requests = g_slist_next(requests); } - return FALSE; + + if (requests_p != NULL) { + g_slist_free_full(requests_p, free); + } + + return result; } void @@ -220,20 +229,28 @@ presence_update(const resource_presence_t presence_type, const char * const msg, static void _send_room_presence(xmpp_conn_t *conn, xmpp_stanza_t *presence) { - GList *rooms = muc_get_active_room_list(); + GList *rooms_p = muc_get_active_room_list(); + GList *rooms = rooms_p; + while (rooms != NULL) { const char *room = rooms->data; const char *nick = muc_get_room_nick(room); - char *full_room_jid = create_fulljid(room, nick); - xmpp_stanza_set_attribute(presence, STANZA_ATTR_TO, full_room_jid); - log_debug("Sending presence to room: %s", full_room_jid); - xmpp_send(conn, presence); - free(full_room_jid); + if (nick != NULL) { + char *full_room_jid = create_fulljid(room, nick); + + xmpp_stanza_set_attribute(presence, STANZA_ATTR_TO, full_room_jid); + log_debug("Sending presence to room: %s", full_room_jid); + xmpp_send(conn, presence); + free(full_room_jid); + } rooms = g_list_next(rooms); } - g_list_free(rooms); + + if (rooms_p != NULL) { + g_list_free(rooms_p); + } } void @@ -347,9 +364,13 @@ _subscribe_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza, void * const userdata) { char *from = xmpp_stanza_get_attribute(stanza, STANZA_ATTR_FROM); - Jid *from_jid = jid_create(from); log_debug("Subscribe presence handler fired for %s", from); + Jid *from_jid = jid_create(from); + if (from_jid == NULL) { + return 1; + } + prof_handle_subscription(from_jid->barejid, PRESENCE_SUBSCRIBE); autocomplete_add(sub_requests_ac, strdup(from_jid->barejid)); @@ -368,6 +389,11 @@ _unavailable_handler(xmpp_conn_t * const conn, Jid *my_jid = jid_create(jid); Jid *from_jid = jid_create(from); + if (my_jid == NULL || from_jid == NULL) { + jid_destroy(my_jid); + jid_destroy(from_jid); + return 1; + } char *status_str = stanza_get_status(stanza, NULL); @@ -420,6 +446,11 @@ _available_handler(xmpp_conn_t * const conn, Jid *my_jid = jid_create(jid); Jid *from_jid = jid_create(from); + if (my_jid == NULL || from_jid == NULL) { + jid_destroy(my_jid); + jid_destroy(from_jid); + return 1; + } char *show_str = stanza_get_show(stanza, "online"); char *status_str = stanza_get_status(stanza, NULL); @@ -515,6 +546,10 @@ _get_caps_key(xmpp_stanza_t * const stanza) log_debug("Presence contains capabilities."); + if (node == NULL) { + return NULL; + } + // xep-0115 if ((hash_type != NULL) && (strcmp(hash_type, "sha-1") == 0)) { log_debug("Hash type %s supported.", hash_type); @@ -544,6 +579,8 @@ _get_caps_key(xmpp_stanza_t * const stanza) g_string_free(id_str, TRUE); } + g_free(node); + return caps_key; } @@ -626,6 +663,7 @@ _room_presence_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza, if (old_nick != NULL) { muc_add_to_roster(room, nick, show_str, status_str, caps_key); prof_handle_room_member_nick_change(room, old_nick, nick); + free(old_nick); } else { if (!muc_nick_in_roster(room, nick)) { prof_handle_room_member_online(room, nick, show_str, status_str, caps_key);