silcclient: Fix error path memory freeing
[silc.git] / lib / silcclient / client.c
index f72a4a7201ec64dba1f2b37f87457fbe1b81ebe6..1d64e02fd502848d099880b396bec665e42160b7 100644 (file)
@@ -604,7 +604,7 @@ silc_client_add_connection(SilcClient client,
   SilcClientConnection conn;
   SilcFSMThread thread;
 
-  if (!callback)
+  if (!client || !callback || !remote_host)
     return NULL;
 
   SILC_LOG_DEBUG(("Adding new connection to %s:%d", remote_host, port));
@@ -624,6 +624,7 @@ silc_client_add_connection(SilcClient client,
 
   conn->internal = silc_calloc(1, sizeof(*conn->internal));
   if (!conn->internal) {
+    silc_free(conn->remote_host);
     silc_free(conn);
     return NULL;
   }
@@ -632,8 +633,9 @@ silc_client_add_connection(SilcClient client,
   silc_atomic_init16(&conn->internal->cmd_ident, 0);
 
   if (!silc_hash_alloc("sha1", &conn->internal->sha1hash)) {
-    silc_free(conn);
+    silc_free(conn->remote_host);
     silc_free(conn->internal);
+    silc_free(conn);
     return NULL;
   }
 
@@ -1020,18 +1022,18 @@ SilcBool silc_client_init(SilcClient client, const char *username,
   if (!silc_identifier_verify(username, strlen(username),
                              SILC_STRING_UTF8, 128)) {
     SILC_LOG_ERROR(("Malformed username '%s'. Username must be UTF-8 string",
-                   client->username));
+                   username));
     return FALSE;
   }
   if (!silc_identifier_verify(hostname, strlen(hostname),
                              SILC_STRING_UTF8, 256)) {
     SILC_LOG_ERROR(("Malformed hostname '%s'. Hostname must be UTF-8 string",
-                   client->hostname));
+                   hostname));
     return FALSE;
   }
   if (!silc_utf8_valid(realname, strlen(realname))) {
     SILC_LOG_ERROR(("Malformed realname '%s'. Realname must be UTF-8 string",
-                   client->realname));
+                   realname));
     return FALSE;
   }