about summary refs log tree commit diff stats
path: root/src
diff options
context:
space:
mode:
authorMichael Vetter <jubalh@iodoru.org>2021-03-25 15:20:53 +0100
committerMichael Vetter <jubalh@iodoru.org>2021-03-25 15:20:53 +0100
commit11382a8bf3de97b7f59a1662862a13c76f48c6e6 (patch)
treee31fd3953678cd7b6cc548bb257b163e7a8ba3fd /src
parent8a6d256fc1593e987d0e0b8098008388ceba5694 (diff)
downloadprofani-tty-11382a8bf3de97b7f59a1662862a13c76f48c6e6.tar.gz
mesage: make _handle_ox_chat safer
And on the way fix a memleak
Diffstat (limited to 'src')
-rw-r--r--src/xmpp/message.c38
1 files changed, 27 insertions, 11 deletions
diff --git a/src/xmpp/message.c b/src/xmpp/message.c
index cb7056a1..f3e81794 100644
--- a/src/xmpp/message.c
+++ b/src/xmpp/message.c
@@ -1425,22 +1425,38 @@ _handle_ox_chat(xmpp_stanza_t* const stanza, ProfMessage* message, gboolean is_m
     message->enc = PROF_MSG_ENC_OX;
 
 #ifdef HAVE_LIBGPGME
+    xmpp_ctx_t* const ctx = connection_get_ctx();
+
     xmpp_stanza_t* ox = xmpp_stanza_get_child_by_name_and_ns(stanza, "openpgp", STANZA_NS_OPENPGP_0);
-    message->plain = p_ox_gpg_decrypt(xmpp_stanza_get_text(ox));
+    if (!ox) {
+        return;
+    }
 
-    xmpp_stanza_t *x =  xmpp_stanza_new_from_string(connection_get_ctx(), message->plain);
-    xmpp_stanza_t *p =  xmpp_stanza_get_child_by_name(x, "payload");
-    xmpp_stanza_t *b =  xmpp_stanza_get_child_by_name(p, "body");
-    message->plain = xmpp_stanza_get_text(b);
-    if(message->plain == NULL ) {
-        message->plain = xmpp_stanza_get_text(stanza);
+    char* ox_text = xmpp_stanza_get_text(ox);
+    if (!ox_text) {
+        return;
     }
-	message->encrypted = xmpp_stanza_get_text(ox);
 
-    if (message->plain == NULL) {
-        message->plain = xmpp_stanza_get_text(stanza);
+    message->plain = p_ox_gpg_decrypt(ox_text);
+    xmpp_free(ctx, ox_text);
+
+    xmpp_stanza_t *x =  xmpp_stanza_new_from_string(ctx, message->plain);
+    xmpp_stanza_t *p =  xmpp_stanza_get_child_by_name(x, "payload");
+    if (p) {
+        xmpp_stanza_t *b =  xmpp_stanza_get_child_by_name(p, "body");
+        if (b) {
+            message->plain = xmpp_stanza_get_text(b);
+            if(message->plain == NULL ) {
+                message->plain = xmpp_stanza_get_text(stanza);
+            }
+            message->encrypted = xmpp_stanza_get_text(ox);
+
+            if (message->plain == NULL) {
+                message->plain = xmpp_stanza_get_text(stanza);
+            }
+            message->encrypted = xmpp_stanza_get_text(ox);
+        }
     }
-    message->encrypted = xmpp_stanza_get_text(ox);
 #endif // HAVE_LIBGPGME
 }