Merge pull request #7 from praveen-pk/master

Fix some memory leaks in redirect plugin
diff --git a/src/plugins/redirect/redirect.c b/src/plugins/redirect/redirect.c
index 17f1679..326d4c5 100644
--- a/src/plugins/redirect/redirect.c
+++ b/src/plugins/redirect/redirect.c
@@ -39,7 +39,7 @@
 
 };
 
-static struct __Redirect_Data *redirect_data;
+static struct __Redirect_Data *redirect_data =NULL;
 
 SER_START_ITEMS(Redirect)
 SER_END_ITEMS(Redirect);
@@ -65,7 +65,7 @@
     int i;
 
     list_t *l = list_create(LISTCOUNT_T_MAX);
-    WsSupportedNamespaces *ns = (WsSupportedNamespaces *)u_malloc(
+    WsSupportedNamespaces *ns = (WsSupportedNamespaces *)u_zalloc(
 			sizeof(WsSupportedNamespaces));
 
     ns->class_prefix = NULL;
@@ -96,16 +96,18 @@
 int init( void *self, void **data )
 {
     char* filename;
-    dictionary *ini, *inc_ini;
+    dictionary *ini=NULL, *inc_ini=NULL;
     filename = (char *) wsmand_options_get_config_file();
     ini = iniparser_new(filename);
+
     if (ini == NULL) {
-      error("redirect: iniparser_new failed");
+      error("Redirect Plugin: iniparser_new failed");
       return 0;
     }
-    redirect_data =  malloc (sizeof(struct __Redirect_Data));
+
+    redirect_data =  u_zalloc (sizeof(struct __Redirect_Data));
     if (redirect_data == NULL){
-	error("Failed while allocating memory for redirect_data");	
+	error("Redirect Plugin: Failed while allocating memory for redirect_data");
 	return 0;
     }
 
@@ -130,7 +132,12 @@
 	    error("Redirect Plugin: The required inputs are not provided in the config file");
 	    return 0;
 	}
-    	
+    if (ini != NULL)
+	iniparser_free(ini);
+
+    if (inc_ini != NULL)
+	iniparser_free (inc_ini);	
+
     return 1;
 }
 
@@ -258,23 +265,23 @@
 WsManClient* setup_redirect_client(WsContextH cntx, char *ws_username, char *ws_password)
 {
 	
-    WsManClient *cl = malloc(sizeof(cl));
+    WsManClient *cl = NULL;
 
-    if (cl == NULL){
-	error("Error while allocating memory for client in redirect plugin");
-	return NULL;	
-    }
-
-	
 	cl = wsmc_create(
 		get_remote_server() ,
                 get_remote_server_port() ,
                 get_remote_url_path(),
                 get_remote_cainfo() ? "https" : "http",
-                get_remote_username() ? get_remote_username() : strdup(ws_username),
-                get_remote_password() ? get_remote_password() : strdup(ws_password) 
+		/* wsmc_create duplicates the username/password passed, no need to duplicate again. */
+                get_remote_username() ? get_remote_username() : ws_username,
+                get_remote_password() ? get_remote_password() : ws_password 
          );
 
+    if (cl == NULL){
+	error("Redirect Plugin: Error while creating the client for redirection");
+	return NULL;
+    }
+
 
 	wsman_transport_set_auth_method(cl, get_remote_authentication_method());
 
diff --git a/src/plugins/redirect/redirect_stubs.c b/src/plugins/redirect/redirect_stubs.c
index becf3e5..0657457 100644
--- a/src/plugins/redirect/redirect_stubs.c
+++ b/src/plugins/redirect/redirect_stubs.c
@@ -122,7 +122,7 @@
 
     WsXmlNodeH r_header=NULL, r_node=NULL, r_body=NULL, r_opt=NULL;
     WsXmlDocH r_response=NULL;
-    char *resource_uri, *remote_enumContext;
+    char *resource_uri, *remote_enumContext=NULL;
     int op; 
     WsManClient *cl=NULL;
 
@@ -161,7 +161,7 @@
 
 
 
-    r_response = ws_xml_duplicate_doc(wsmc_build_envelope_from_response(cl));
+    r_response = wsmc_build_envelope_from_response(cl);
 
  
     if (  wsman_is_fault_envelope(r_response)){
@@ -209,7 +209,8 @@
     }
     
     wsmc_release(cl);
-
+    if (remote_enumContext != NULL)
+	free(remote_enumContext);
     
     return 0;
 
@@ -265,7 +266,7 @@
     }
 
 
-    response = ws_xml_duplicate_doc(wsmc_build_envelope_from_response(cl));
+    response = wsmc_build_envelope_from_response(cl);
 
    
     if ( ! wsman_is_fault_envelope(response) )