From eff5986d38640345ad0d196761a4682e0d81b76d Mon Sep 17 00:00:00 2001 From: Dmitry Podgorny Date: Sun, 9 Jun 2013 14:20:48 +0300 Subject: [PATCH] fixed several memory leaks And infinity loop in stanza_destroy_form(). --- src/xmpp/connection.c | 8 +++++-- src/xmpp/message.c | 55 ++++++++++++++++++++++++++++++------------- src/xmpp/stanza.c | 22 ++++++++++++++--- 3 files changed, 64 insertions(+), 21 deletions(-) diff --git a/src/xmpp/connection.c b/src/xmpp/connection.c index 7cc46846..85ddfa45 100644 --- a/src/xmpp/connection.c +++ b/src/xmpp/connection.c @@ -123,7 +123,7 @@ jabber_connect_with_account(const ProfAccount * const account, // connect with fulljid Jid *jidp = jid_create_from_bare_and_resource(account->jid, account->resource); jabber_conn_status_t result = _jabber_connect(jidp->fulljid, passwd, account->server); - free(jidp); + jid_destroy(jidp); return result; } @@ -334,6 +334,7 @@ int connection_error_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza, void * const userdata) { + xmpp_ctx_t *ctx = connection_get_ctx(); gchar *err_msg = NULL; gchar *from = xmpp_stanza_get_attribute(stanza, STANZA_ATTR_FROM); xmpp_stanza_t *error_stanza = xmpp_stanza_get_child_by_name(stanza, STANZA_NAME_ERROR); @@ -347,7 +348,10 @@ connection_error_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza, // check for text if (text_stanza != NULL) { err_msg = xmpp_stanza_get_text(text_stanza); - prof_handle_error_message(from, err_msg); + if (err_msg != NULL) { + prof_handle_error_message(from, err_msg); + xmpp_free(ctx, err_msg); + } // TODO : process 'type' attribute from [RFC6120, 8.3.2] diff --git a/src/xmpp/message.c b/src/xmpp/message.c index f8c2fd8f..29a7cd98 100644 --- a/src/xmpp/message.c +++ b/src/xmpp/message.c @@ -182,6 +182,7 @@ static int _conference_message_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza, void * const userdata) { + xmpp_ctx_t *ctx = connection_get_ctx(); xmpp_stanza_t *x_muc = xmpp_stanza_get_child_by_ns(stanza, STANZA_NS_MUC_USER); xmpp_stanza_t *x_groupchat = xmpp_stanza_get_child_by_ns(stanza, STANZA_NS_CONFERENCE); char *from = xmpp_stanza_get_attribute(stanza, STANZA_ATTR_FROM); @@ -219,6 +220,9 @@ _conference_message_handler(xmpp_conn_t * const conn, prof_handle_room_invite(INVITE_MEDIATED, invitor, room, reason); jid_destroy(jidp); + if (reason != NULL) { + xmpp_free(ctx, reason); + } // XEP-0429 } else if (x_groupchat != NULL) { @@ -244,6 +248,7 @@ static int _groupchat_message_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza, void * const userdata) { + xmpp_ctx_t *ctx = connection_get_ctx(); char *message = NULL; char *room_jid = xmpp_stanza_get_attribute(stanza, STANZA_ATTR_FROM); Jid *jid = jid_create(room_jid); @@ -259,6 +264,7 @@ _groupchat_message_handler(xmpp_conn_t * const conn, prof_handle_room_subject(jid->barejid, message); } + jid_destroy(jid); return 1; // handle other room broadcasts @@ -268,9 +274,11 @@ _groupchat_message_handler(xmpp_conn_t * const conn, message = xmpp_stanza_get_text(body); if (message != NULL) { prof_handle_room_broadcast(room_jid, message); + xmpp_free(ctx, message); } } + jid_destroy(jid); return 1; } } @@ -278,12 +286,14 @@ _groupchat_message_handler(xmpp_conn_t * const conn, if (!jid_is_valid_room_form(jid)) { log_error("Invalid room JID: %s", jid->str); + jid_destroy(jid); return 1; } // room not active in profanity if (!muc_room_is_active(jid)) { log_error("Message recieved for inactive chat room: %s", jid->str); + jid_destroy(jid); return 1; } @@ -294,11 +304,14 @@ _groupchat_message_handler(xmpp_conn_t * const conn, // check for and deal with message if (body != NULL) { - char *message = xmpp_stanza_get_text(body); - if (delayed) { - prof_handle_room_history(jid->barejid, jid->resourcepart, tv_stamp, message); - } else { - prof_handle_room_message(jid->barejid, jid->resourcepart, message); + message = xmpp_stanza_get_text(body); + if (message != NULL) { + if (delayed) { + prof_handle_room_history(jid->barejid, jid->resourcepart, tv_stamp, message); + } else { + prof_handle_room_message(jid->barejid, jid->resourcepart, message); + } + xmpp_free(ctx, message); } } @@ -311,6 +324,7 @@ static int _chat_message_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza, void * const userdata) { + xmpp_ctx_t *ctx = connection_get_ctx(); gchar *from = xmpp_stanza_get_attribute(stanza, STANZA_ATTR_FROM); Jid *jid = jid_create(from); @@ -319,7 +333,10 @@ _chat_message_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza, xmpp_stanza_t *body = xmpp_stanza_get_child_by_name(stanza, STANZA_NAME_BODY); if (body != NULL) { char *message = xmpp_stanza_get_text(body); - prof_handle_duck_result(message); + if (message != NULL) { + prof_handle_duck_result(message); + xmpp_free(ctx, message); + } } jid_destroy(jid); @@ -335,14 +352,17 @@ _chat_message_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza, xmpp_stanza_t *body = xmpp_stanza_get_child_by_name(stanza, STANZA_NAME_BODY); if (body != NULL) { char *message = xmpp_stanza_get_text(body); - if (delayed) { - prof_handle_delayed_message(jid->str, message, tv_stamp, TRUE); - } else { - prof_handle_incoming_message(jid->str, message, TRUE); + if (message != NULL) { + if (delayed) { + prof_handle_delayed_message(jid->str, message, tv_stamp, TRUE); + } else { + prof_handle_incoming_message(jid->str, message, TRUE); + } + xmpp_free(ctx, message); } } - free(jid); + jid_destroy(jid); return 1; // standard chat message, use jid without resource @@ -385,14 +405,17 @@ _chat_message_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza, xmpp_stanza_t *body = xmpp_stanza_get_child_by_name(stanza, STANZA_NAME_BODY); if (body != NULL) { char *message = xmpp_stanza_get_text(body); - if (delayed) { - prof_handle_delayed_message(jid->barejid, message, tv_stamp, FALSE); - } else { - prof_handle_incoming_message(jid->barejid, message, FALSE); + if (message != NULL) { + if (delayed) { + prof_handle_delayed_message(jid->barejid, message, tv_stamp, FALSE); + } else { + prof_handle_incoming_message(jid->barejid, message, FALSE); + } + xmpp_free(ctx, message); } } - free(jid); + jid_destroy(jid); return 1; } } diff --git a/src/xmpp/stanza.c b/src/xmpp/stanza.c index 59a89671..91ce10b7 100644 --- a/src/xmpp/stanza.c +++ b/src/xmpp/stanza.c @@ -48,6 +48,7 @@ stanza_create_chat_state(xmpp_ctx_t *ctx, const char * const recipient, xmpp_stanza_set_name(chat_state, state); xmpp_stanza_set_ns(chat_state, STANZA_NS_CHATSTATES); xmpp_stanza_add_child(msg, chat_state); + xmpp_stanza_release(chat_state); return msg; } @@ -75,13 +76,16 @@ stanza_create_message(xmpp_ctx_t *ctx, const char * const recipient, text = xmpp_stanza_new(ctx); xmpp_stanza_set_text(text, encoded_xml); xmpp_stanza_add_child(body, text); + xmpp_stanza_release(text); xmpp_stanza_add_child(msg, body); + xmpp_stanza_release(body); if (state != NULL) { xmpp_stanza_t *chat_state = xmpp_stanza_new(ctx); xmpp_stanza_set_name(chat_state, state); xmpp_stanza_set_ns(chat_state, STANZA_NS_CHATSTATES); xmpp_stanza_add_child(msg, chat_state); + xmpp_stanza_release(chat_state); } g_free(encoded_xml); @@ -106,7 +110,9 @@ stanza_create_roster_remove_set(xmpp_ctx_t *ctx, const char * const barejid) xmpp_stanza_set_attribute(item, STANZA_ATTR_SUBSCRIPTION, "remove"); xmpp_stanza_add_child(query, item); + xmpp_stanza_release(item); xmpp_stanza_add_child(iq, query); + xmpp_stanza_release(query); return iq; @@ -140,12 +146,16 @@ stanza_create_roster_set(xmpp_ctx_t *ctx, const char * const jid, xmpp_stanza_set_name(group, STANZA_NAME_GROUP); xmpp_stanza_set_text(groupname, groups->data); xmpp_stanza_add_child(group, groupname); + xmpp_stanza_release(groupname); xmpp_stanza_add_child(item, group); + xmpp_stanza_release(group); groups = g_slist_next(groups); } xmpp_stanza_add_child(query, item); + xmpp_stanza_release(item); xmpp_stanza_add_child(iq, query); + xmpp_stanza_release(query); return iq; } @@ -170,6 +180,7 @@ stanza_create_invite(xmpp_ctx_t *ctx, const char * const room, } xmpp_stanza_add_child(message, x); + xmpp_stanza_release(x); return message; } @@ -186,6 +197,7 @@ stanza_create_room_join_presence(xmpp_ctx_t * const ctx, xmpp_stanza_set_name(x, STANZA_NAME_X); xmpp_stanza_set_ns(x, STANZA_NS_MUC); xmpp_stanza_add_child(presence, x); + xmpp_stanza_release(x); return presence; } @@ -704,7 +716,7 @@ stanza_create_form(xmpp_stanza_t * const stanza) xmpp_stanza_t *child = xmpp_stanza_get_children(stanza); if (child != NULL) { - result = malloc(sizeof(struct data_form_t)); + result = malloc(sizeof(DataForm)); result->form_type = NULL; result->fields = NULL; } @@ -718,10 +730,11 @@ stanza_create_form(xmpp_stanza_t * const stanza) xmpp_stanza_t *value = xmpp_stanza_get_child_by_name(child, "value"); char *value_text = xmpp_stanza_get_text(value); result->form_type = strdup(value_text); + xmpp_free(ctx, value_text); // handle regular fields } else { - FormField *field = malloc(sizeof(struct form_field_t)); + FormField *field = malloc(sizeof(FormField)); field->var = strdup(var); field->values = NULL; xmpp_stanza_t *value = xmpp_stanza_get_children(child); @@ -758,10 +771,12 @@ stanza_destroy_form(DataForm *form) if ((field->values) != NULL) { g_slist_free_full(field->values, free); } + curr_field = curr_field->next; } + g_slist_free_full(form->fields, free); } - form = NULL; + free(form); } } @@ -779,6 +794,7 @@ stanza_attach_priority(xmpp_ctx_t * const ctx, xmpp_stanza_t * const presence, xmpp_stanza_set_name(priority, STANZA_NAME_PRIORITY); xmpp_stanza_set_text(value, pri_str); xmpp_stanza_add_child(priority, value); + xmpp_stanza_release(value); xmpp_stanza_add_child(presence, priority); xmpp_stanza_release(priority); }