about summary refs log tree commit diff stats
path: root/src/xmpp
diff options
context:
space:
mode:
authorMichael Vetter <jubalh@iodoru.org>2021-03-25 16:14:40 +0100
committerMichael Vetter <jubalh@iodoru.org>2021-03-25 16:14:40 +0100
commitf81ed759f54de1f30bccc0d4dce737b41dcf216c (patch)
tree222155332db7e20a6879eab6beaee088bfb2b84c /src/xmpp
parentd23c3dd065dd2d884751c23ad2f24e6a716cc6ad (diff)
downloadprofani-tty-f81ed759f54de1f30bccc0d4dce737b41dcf216c.tar.gz
stanza: guard mallocs
If this happens we have more serious problems :-)
But anyways..
Diffstat (limited to 'src/xmpp')
-rw-r--r--src/xmpp/iq.c158
1 files changed, 88 insertions, 70 deletions
diff --git a/src/xmpp/iq.c b/src/xmpp/iq.c
index bcbb715b..e3b2314c 100644
--- a/src/xmpp/iq.c
+++ b/src/xmpp/iq.c
@@ -288,11 +288,13 @@ void
 iq_id_handler_add(const char* const id, ProfIqCallback func, ProfIqFreeCallback free_func, void* userdata)
 {
     ProfIqHandler* handler = malloc(sizeof(ProfIqHandler));
-    handler->func = func;
-    handler->free_func = free_func;
-    handler->userdata = userdata;
+    if (handler) {
+        handler->func = func;
+        handler->free_func = free_func;
+        handler->userdata = userdata;
 
-    g_hash_table_insert(id_handlers, strdup(id), handler);
+        g_hash_table_insert(id_handlers, strdup(id), handler);
+    }
 }
 
 void
@@ -481,15 +483,17 @@ iq_room_info_request(const char* const room, gboolean display_result)
     xmpp_stanza_t* iq = stanza_create_disco_info_iq(ctx, id, room, NULL);
 
     ProfRoomInfoData* cb_data = malloc(sizeof(ProfRoomInfoData));
-    cb_data->room = strdup(room);
-    cb_data->display = display_result;
+    if (cb_data) {
+        cb_data->room = strdup(room);
+        cb_data->display = display_result;
 
-    iq_id_handler_add(id, _room_info_response_id_handler, (ProfIqFreeCallback)_iq_free_room_data, cb_data);
+        iq_id_handler_add(id, _room_info_response_id_handler, (ProfIqFreeCallback)_iq_free_room_data, cb_data);
 
-    free(id);
+        iq_send_stanza(iq);
+        xmpp_stanza_release(iq);
+    }
 
-    iq_send_stanza(iq);
-    xmpp_stanza_release(iq);
+    free(id);
 }
 
 void
@@ -667,13 +671,15 @@ iq_room_affiliation_list(const char* const room, char* affiliation, bool show_ui
     const char* id = xmpp_stanza_get_id(iq);
 
     ProfAffiliationList* affiliation_list = malloc(sizeof(ProfAffiliationList));
-    affiliation_list->affiliation = strdup(affiliation);
-    affiliation_list->show_ui_message = show_ui_message;
+    if (affiliation_list) {
+        affiliation_list->affiliation = strdup(affiliation);
+        affiliation_list->show_ui_message = show_ui_message;
 
-    iq_id_handler_add(id, _room_affiliation_list_result_id_handler, (ProfIqFreeCallback)_iq_free_affiliation_list, affiliation_list);
+        iq_id_handler_add(id, _room_affiliation_list_result_id_handler, (ProfIqFreeCallback)_iq_free_affiliation_list, affiliation_list);
 
-    iq_send_stanza(iq);
-    xmpp_stanza_release(iq);
+        iq_send_stanza(iq);
+        xmpp_stanza_release(iq);
+    }
 }
 
 void
@@ -699,13 +705,15 @@ iq_room_affiliation_set(const char* const room, const char* const jid, char* aff
     const char* id = xmpp_stanza_get_id(iq);
 
     ProfPrivilegeSet* affiliation_set = malloc(sizeof(struct privilege_set_t));
-    affiliation_set->item = strdup(jid);
-    affiliation_set->privilege = strdup(affiliation);
+    if (affiliation_set) {
+        affiliation_set->item = strdup(jid);
+        affiliation_set->privilege = strdup(affiliation);
 
-    iq_id_handler_add(id, _room_affiliation_set_result_id_handler, (ProfIqFreeCallback)_iq_free_affiliation_set, affiliation_set);
+        iq_id_handler_add(id, _room_affiliation_set_result_id_handler, (ProfIqFreeCallback)_iq_free_affiliation_set, affiliation_set);
 
-    iq_send_stanza(iq);
-    xmpp_stanza_release(iq);
+        iq_send_stanza(iq);
+        xmpp_stanza_release(iq);
+    }
 }
 
 void
@@ -718,13 +726,15 @@ iq_room_role_set(const char* const room, const char* const nick, char* role,
     const char* id = xmpp_stanza_get_id(iq);
 
     struct privilege_set_t* role_set = malloc(sizeof(ProfPrivilegeSet));
-    role_set->item = strdup(nick);
-    role_set->privilege = strdup(role);
+    if (role_set) {
+        role_set->item = strdup(nick);
+        role_set->privilege = strdup(role);
 
-    iq_id_handler_add(id, _room_role_set_result_id_handler, (ProfIqFreeCallback)_iq_free_affiliation_set, role_set);
+        iq_id_handler_add(id, _room_role_set_result_id_handler, (ProfIqFreeCallback)_iq_free_affiliation_set, role_set);
 
-    iq_send_stanza(iq);
-    xmpp_stanza_release(iq);
+        iq_send_stanza(iq);
+        xmpp_stanza_release(iq);
+    }
 }
 
 void
@@ -2162,27 +2172,29 @@ _room_info_response_id_handler(xmpp_stanza_t* const stanza, void* const userdata
                 if (name || category || type) {
                     DiscoIdentity* identity = malloc(sizeof(struct disco_identity_t));
 
-                    if (name) {
-                        identity->name = strdup(name);
-                        ProfMucWin* mucwin = wins_get_muc(cb_data->room);
-                        if (mucwin) {
-                            mucwin->room_name = strdup(name);
+                    if (identity) {
+                        if (name) {
+                            identity->name = strdup(name);
+                            ProfMucWin* mucwin = wins_get_muc(cb_data->room);
+                            if (mucwin) {
+                                mucwin->room_name = strdup(name);
+                            }
+                        } else {
+                            identity->name = NULL;
+                        }
+                        if (category) {
+                            identity->category = strdup(category);
+                        } else {
+                            identity->category = NULL;
+                        }
+                        if (type) {
+                            identity->type = strdup(type);
+                        } else {
+                            identity->type = NULL;
                         }
-                    } else {
-                        identity->name = NULL;
-                    }
-                    if (category) {
-                        identity->category = strdup(category);
-                    } else {
-                        identity->category = NULL;
-                    }
-                    if (type) {
-                        identity->type = strdup(type);
-                    } else {
-                        identity->type = NULL;
-                    }
 
-                    identities = g_slist_append(identities, identity);
+                        identities = g_slist_append(identities, identity);
+                    }
                 }
             }
 
@@ -2309,23 +2321,25 @@ _disco_info_response_id_handler(xmpp_stanza_t* const stanza, void* const userdat
                 if (name || category || type) {
                     DiscoIdentity* identity = malloc(sizeof(struct disco_identity_t));
 
-                    if (name) {
-                        identity->name = strdup(name);
-                    } else {
-                        identity->name = NULL;
-                    }
-                    if (category) {
-                        identity->category = strdup(category);
-                    } else {
-                        identity->category = NULL;
-                    }
-                    if (type) {
-                        identity->type = strdup(type);
-                    } else {
-                        identity->type = NULL;
-                    }
+                    if (identity) {
+                        if (name) {
+                            identity->name = strdup(name);
+                        } else {
+                            identity->name = NULL;
+                        }
+                        if (category) {
+                            identity->category = strdup(category);
+                        } else {
+                            identity->category = NULL;
+                        }
+                        if (type) {
+                            identity->type = strdup(type);
+                        } else {
+                            identity->type = NULL;
+                        }
 
-                    identities = g_slist_append(identities, identity);
+                        identities = g_slist_append(identities, identity);
+                    }
                 }
             }
 
@@ -2491,14 +2505,16 @@ _disco_items_result_handler(xmpp_stanza_t* const stanza)
             const char* item_jid = xmpp_stanza_get_attribute(child, STANZA_ATTR_JID);
             if (item_jid) {
                 DiscoItem* item = malloc(sizeof(struct disco_item_t));
-                item->jid = strdup(item_jid);
-                const char* item_name = xmpp_stanza_get_attribute(child, STANZA_ATTR_NAME);
-                if (item_name) {
-                    item->name = strdup(item_name);
-                } else {
-                    item->name = NULL;
+                if (item) {
+                    item->jid = strdup(item_jid);
+                    const char* item_name = xmpp_stanza_get_attribute(child, STANZA_ATTR_NAME);
+                    if (item_name) {
+                        item->name = strdup(item_name);
+                    } else {
+                        item->name = NULL;
+                    }
+                    items = g_slist_append(items, item);
                 }
-                items = g_slist_append(items, item);
             }
         }
 
@@ -2578,10 +2594,12 @@ iq_mam_request(ProfChatWin* win)
     xmpp_stanza_t* iq = stanza_create_mam_iq(ctx, win->barejid, datestr, NULL);
 
     MamRsmUserdata* data = malloc(sizeof(MamRsmUserdata));
-    data->datestr = strdup(datestr);
-    data->barejid = strdup(win->barejid);
+    if (data) {
+        data->datestr = strdup(datestr);
+        data->barejid = strdup(win->barejid);
 
-    iq_id_handler_add(xmpp_stanza_get_id(iq), _mam_rsm_id_handler, NULL, data);
+        iq_id_handler_add(xmpp_stanza_get_id(iq), _mam_rsm_id_handler, NULL, data);
+    }
 
     g_free(datestr);
     g_date_time_unref(timestamp);