about summary refs log tree commit diff stats
path: root/src/xmpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/xmpp')
-rw-r--r--src/xmpp/connection.c8
-rw-r--r--src/xmpp/message.c55
-rw-r--r--src/xmpp/presence.c12
-rw-r--r--src/xmpp/stanza.c52
4 files changed, 100 insertions, 27 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 <error/> [RFC6120, 8.3.2]
 
diff --git a/src/xmpp/message.c b/src/xmpp/message.c
index 5007cccd..1111d6f5 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);
     xmpp_stanza_t *captcha = xmpp_stanza_get_child_by_ns(stanza, STANZA_NS_CAPTCHA);
@@ -220,6 +221,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) {
@@ -256,6 +260,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);
@@ -271,6 +276,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
@@ -280,9 +286,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;
         }
     }
@@ -290,12 +298,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;
     }
 
@@ -306,11 +316,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);
         }
     }
 
@@ -323,6 +336,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);
 
@@ -331,7 +345,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);
@@ -347,14 +364,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
@@ -397,14 +417,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/presence.c b/src/xmpp/presence.c
index 5aa94c1d..f072196d 100644
--- a/src/xmpp/presence.c
+++ b/src/xmpp/presence.c
@@ -224,11 +224,12 @@ _send_room_presence(xmpp_conn_t *conn, xmpp_stanza_t *presence)
     while (rooms != NULL) {
         const char *room = rooms->data;
         const char *nick = muc_get_room_nick(room);
-        const char *full_room_jid = create_fulljid(room, nick);
+        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);
     }
@@ -384,6 +385,7 @@ _unavailable_handler(xmpp_conn_t * const conn,
         }
     }
 
+    FREE_SET_NULL(status_str);
     jid_destroy(my_jid);
     jid_destroy(from_jid);
 
@@ -469,6 +471,8 @@ _available_handler(xmpp_conn_t * const conn,
             last_activity);
     }
 
+    FREE_SET_NULL(status_str);
+    FREE_SET_NULL(show_str);
     jid_destroy(my_jid);
     jid_destroy(from_jid);
 
@@ -529,7 +533,7 @@ _get_caps_key(xmpp_stanza_t * const stanza)
 
         guint from_hash = g_str_hash(from);
         char from_hash_str[9];
-        g_sprintf(from_hash_str, "%08x", from_hash);
+        g_snprintf(from_hash_str, sizeof(from_hash_str), "%08x", from_hash);
         caps_key = strdup(from_hash_str);
         GString *id_str = g_string_new("capsreq_");
         g_string_append(id_str, from_hash_str);
@@ -626,7 +630,11 @@ _room_presence_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza,
                     }
                 }
             }
+
+           FREE_SET_NULL(show_str);
         }
+
+        FREE_SET_NULL(status_str);
     }
 
     jid_destroy(my_jid);
diff --git a/src/xmpp/stanza.c b/src/xmpp/stanza.c
index 59a89671..a5b7788a 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;
 }
@@ -371,26 +383,48 @@ stanza_get_delay(xmpp_stanza_t * const stanza, GTimeVal *tv_stamp)
 char *
 stanza_get_status(xmpp_stanza_t *stanza, char *def)
 {
+    xmpp_ctx_t *ctx = connection_get_ctx();
     xmpp_stanza_t *status =
         xmpp_stanza_get_child_by_name(stanza, STANZA_NAME_STATUS);
 
     if (status != NULL) {
-        return xmpp_stanza_get_text(status);
+        // xmpp_free and free may be different functions so convert all to
+        // libc malloc
+        char *s1, *s2 = NULL;
+        s1 = xmpp_stanza_get_text(status);
+        if (s1 != NULL) {
+            s2 = strdup(s1);
+            xmpp_free(ctx, s1);
+        }
+        return s2;
+    } else if (def != NULL) {
+        return strdup(def);
     } else {
-        return def;
+        return NULL;
     }
 }
 
 char *
 stanza_get_show(xmpp_stanza_t *stanza, char *def)
 {
+    xmpp_ctx_t *ctx = connection_get_ctx();
     xmpp_stanza_t *show =
         xmpp_stanza_get_child_by_name(stanza, STANZA_NAME_SHOW);
 
     if (show != NULL) {
-        return xmpp_stanza_get_text(show);
+        // xmpp_free and free may be different functions so convert all to
+        // libc malloc
+        char *s1, *s2 = NULL;
+        s1 = xmpp_stanza_get_text(show);
+        if (s1 != NULL) {
+            s2 = strdup(s1);
+            xmpp_free(ctx, s1);
+        }
+        return s2;
+    } else if (def != NULL) {
+        return strdup(def);
     } else {
-        return def;
+        return NULL;
     }
 }
 
@@ -704,7 +738,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 +752,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 +793,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 +816,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);
     }