about summary refs log tree commit diff stats
path: root/src/jid.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/jid.c')
-rw-r--r--src/jid.c56
1 files changed, 28 insertions, 28 deletions
diff --git a/src/jid.c b/src/jid.c
index 6347f083..b236c164 100644
--- a/src/jid.c
+++ b/src/jid.c
@@ -34,13 +34,14 @@ jid_create(const gchar * const str)
 {
     Jid *result = NULL;
 
-    if (str == NULL) {
+    /* if str is NULL g_strdup returns NULL */
+    gchar *trimmed = g_strdup(str);
+    if (trimmed == NULL) {
         return NULL;
     }
 
-    gchar *trimmed = g_strdup(str);
-
     if (strlen(trimmed) == 0) {
+        g_free(trimmed);
         return NULL;
     }
 
@@ -50,10 +51,12 @@ jid_create(const gchar * const str)
     }
 
     if (!g_utf8_validate(trimmed, -1, NULL)) {
+        g_free(trimmed);
         return NULL;
     }
 
     result = malloc(sizeof(struct jid_t));
+    result->str = NULL;
     result->localpart = NULL;
     result->domainpart = NULL;
     result->resourcepart = NULL;
@@ -81,13 +84,11 @@ jid_create(const gchar * const str)
     }
 
     if (result->domainpart == NULL) {
-        free(trimmed);
+        jid_destroy(result);
         return NULL;
     }
 
-    result->str = g_strdup(trimmed);
-
-    free(trimmed);
+    result->str = trimmed;
 
     return result;
 }
@@ -107,12 +108,12 @@ void
 jid_destroy(Jid *jid)
 {
     if (jid != NULL) {
-        FREE_SET_NULL(jid->str);
-        FREE_SET_NULL(jid->localpart);
-        FREE_SET_NULL(jid->domainpart);
-        FREE_SET_NULL(jid->resourcepart);
-        FREE_SET_NULL(jid->barejid);
-        FREE_SET_NULL(jid->fulljid);
+        GFREE_SET_NULL(jid->str);
+        GFREE_SET_NULL(jid->localpart);
+        GFREE_SET_NULL(jid->domainpart);
+        GFREE_SET_NULL(jid->resourcepart);
+        GFREE_SET_NULL(jid->barejid);
+        GFREE_SET_NULL(jid->fulljid);
         FREE_SET_NULL(jid);
     }
 }
@@ -181,17 +182,17 @@ char *
 get_room_from_full_jid(const char * const full_room_jid)
 {
     char **tokens = g_strsplit(full_room_jid, "/", 0);
-    char *room_part;
+    char *room_part = NULL;
 
-    if (tokens == NULL || tokens[0] == NULL) {
-        return NULL;
-    } else {
-        room_part = strdup(tokens[0]);
+    if (tokens != NULL) {
+        if (tokens[0] != NULL) {
+            room_part = strdup(tokens[0]);
+        }
 
         g_strfreev(tokens);
-
-        return room_part;
     }
+
+    return room_part;
 }
 
 /*
@@ -203,16 +204,15 @@ char *
 get_nick_from_full_jid(const char * const full_room_jid)
 {
     char **tokens = g_strsplit(full_room_jid, "/", 0);
-    char *nick_part;
+    char *nick_part = NULL;
 
-    if (tokens == NULL || tokens[1] == NULL) {
-        return NULL;
-    } else {
-        nick_part = strdup(tokens[1]);
+    if (tokens != NULL) {
+        if (tokens[0] != NULL && tokens[1] != NULL) {
+            nick_part = strdup(tokens[1]);
+        }
 
         g_strfreev(tokens);
-
-        return nick_part;
     }
-}
 
+    return nick_part;
+}