From e9d8741ad9493a7f23273fd8f856709bb48225ba Mon Sep 17 00:00:00 2001
From: Vitezslav Crhonek <vcrhonek@redhat.com>
Date: Mon, 3 Aug 2015 14:06:27 +0200
Subject: [PATCH] fix resource/memory leaks

---
 src/lib/u/iniparser.c                   |  25 ++++-
 src/lib/u/uri.c                         |   1 +
 src/lib/wsman-faults.c                  |   4 -
 src/lib/wsman-plugins.c                 |   2 +-
 src/lib/wsman-server-api.c              |   2 +
 src/lib/wsman-subscription-repository.c | 193 +++++++++++++++++---------------
 6 files changed, 128 insertions(+), 99 deletions(-)

diff --git a/src/lib/u/iniparser.c b/src/lib/u/iniparser.c
index aa35136305fd..b2168a1ebaa5 100644
--- a/src/lib/u/iniparser.c
+++ b/src/lib/u/iniparser.c
@@ -214,11 +214,20 @@ static dictionary * dictionary_new(int size)
     if (size<DICTMINSZ) size=DICTMINSZ ;
 
     d = (dictionary *)calloc(1, sizeof(dictionary));
-    d->size = size ;
-    d->val  = (char **)calloc(size, sizeof(char*));
-    d->key  = (char **)calloc(size, sizeof(char*));
-    d->hash = (unsigned int *)calloc(size, sizeof(unsigned));
-
+    if (d != NULL) {
+      d->size = size ;
+      d->val  = (char **)calloc(size, sizeof(char*));
+      d->key  = (char **)calloc(size, sizeof(char*));
+      d->hash = (unsigned int *)calloc(size, sizeof(unsigned));
+    }
+    if ((d == NULL) || (d->val == NULL) || (d->key == NULL) || (d->hash == NULL)) {
+      fprintf(stderr, "dictionary_new: memory allocation failure\n");
+      free(d->val);
+      free(d->key);
+      free(d->hash);
+      free(d);
+      d = NULL;
+    }
     return d;
 }
 
@@ -898,6 +907,10 @@ dictionary * iniparser_new(char *ininame)
      * Initialize a new dictionary entry
      */
     d = dictionary_new(0);
+    if (d == NULL) {
+      fclose(ini);
+      return d;
+    }
     lineno = 0 ;
     while (fgets(lin, ASCIILINESZ, ini)!=NULL) {
         lineno++ ;
@@ -931,6 +944,8 @@ dictionary * iniparser_new(char *ininame)
                     strcpy(val, strcrop(val, crop_key));
                 }
                 if (iniparser_add_entry(d, sec, key, val) != 0) {
+                  dictionary_del(d);
+                  fclose(ini);
                   return NULL;
                 }
             }
diff --git a/src/lib/u/uri.c b/src/lib/u/uri.c
index b299f7524c00..6861fbc740d2 100644
--- a/src/lib/u/uri.c
+++ b/src/lib/u/uri.c
@@ -249,6 +249,7 @@ hash_t *u_parse_query(const char *query)
 err:
 	u_free(q);
 	u_free(key);
+	hash_free(h);
 	return NULL;
 }
 
diff --git a/src/lib/wsman-faults.c b/src/lib/wsman-faults.c
index eadfa6523f08..b45d0b7bdd60 100644
--- a/src/lib/wsman-faults.c
+++ b/src/lib/wsman-faults.c
@@ -600,7 +600,3 @@ wsman_fault_occured(WsmanMessage *msg)
 {
 	return (msg->status.fault_code == WSMAN_RC_OK ) ?  0 : 1;
 }
-
-
-
-
diff --git a/src/lib/wsman-plugins.c b/src/lib/wsman-plugins.c
index 2b9dcd867506..798ae4673792 100644
--- a/src/lib/wsman-plugins.c
+++ b/src/lib/wsman-plugins.c
@@ -216,7 +216,7 @@ scan_plugins_in_directory ( WsManListenerH *listener,
             } else {
                 error("Out of memory scanning for plugins.");
             }
-			if (plugin_path)
+            if (plugin_path)
             	u_free (plugin_path);
             if (retv != 0  && (NULL != plugin))
                 plugin_free(plugin);
diff --git a/src/lib/wsman-server-api.c b/src/lib/wsman-server-api.c
index bcc0ea465fc2..ae2b89a3df12 100644
--- a/src/lib/wsman-server-api.c
+++ b/src/lib/wsman-server-api.c
@@ -112,6 +112,8 @@ void *wsman_server_create_config(char *config_file)
 		soap = ws_context_get_runtime(cntx);
 		if (listener)
 			soap->listener = (WsManListenerH *)listener;
+	} else {
+		u_free(listener);
 	}
 	//debug_add_handler (debug_message_handler, DEBUG_LEVEL_ALWAYS, NULL);
 	return (void *) soap;
diff --git a/src/lib/wsman-subscription-repository.c b/src/lib/wsman-subscription-repository.c
index c6e21a38777c..9af78f39aea6 100644
--- a/src/lib/wsman-subscription-repository.c
+++ b/src/lib/wsman-subscription-repository.c
@@ -41,6 +41,7 @@
 #ifdef HAVE_DIRENT_H
 #include <dirent.h>
 #endif
+#include "u/libu.h"
 #include "wsman-soap.h"
 #include "wsman-names.h"
 #include "wsman-types.h"
@@ -48,16 +49,25 @@
 #include "wsman-xml-binding.h"
 
 int LocalSubscriptionOpInit (char * uri_repository, void *opaqueData);
-int LocalSubscriptionOpFinalize(char * uri_repository, void *opaqueData);
-int LocalSubscriptionOpGet(char * uri_repository, char * uuid, unsigned char **subscriptionDoc, int *len);
-int LocalSubscriptionOpSearch(char * uri_repository, char * uuid);
+int LocalSubscriptionOpFinalize (char * uri_repository, void *opaqueData);
+int LocalSubscriptionOpGet (char * uri_repository, char * uuid, unsigned char **subscriptionDoc, int *len);
+int LocalSubscriptionOpSearch (char * uri_repository, char * uuid);
 int LocalSubscriptionOpLoad (char * uri_repository, list_t * subscription_list);
 int LocalSubscriptionOpSave (char * uri_repository, char * uuid, unsigned char *subscriptionDoc);
-int LocalSubscriptionOpUpdate(char * uri_repository, char * uuid, char *expire);
+int LocalSubscriptionOpUpdate (char * uri_repository, char * uuid, char *expire);
 int LocalSubscriptionOpDelete (char * uri_repository, char * uuid);
 
 
-struct __SubsRepositoryOpSet subscription_repository_op_set = {LocalSubscriptionOpInit, LocalSubscriptionOpFinalize, LocalSubscriptionOpLoad, LocalSubscriptionOpGet, LocalSubscriptionOpSearch, LocalSubscriptionOpSave, LocalSubscriptionOpUpdate, LocalSubscriptionOpDelete};
+struct __SubsRepositoryOpSet subscription_repository_op_set = { 
+  LocalSubscriptionOpInit,
+  LocalSubscriptionOpFinalize,
+  LocalSubscriptionOpLoad,
+  LocalSubscriptionOpGet,
+  LocalSubscriptionOpSearch,
+  LocalSubscriptionOpSave,
+  LocalSubscriptionOpUpdate,
+  LocalSubscriptionOpDelete
+};
 
 static int LocalSubscriptionInitFlag = 0;
 
@@ -75,8 +85,7 @@ int LocalSubscriptionOpInit (char * uri_repository, void *opaqueData)
 
 int LocalSubscriptionOpFinalize(char * uri_repository, void *opaqueData)
 {
-	if(LocalSubscriptionInitFlag == 0) return -1;
-	return 0;
+        return (LocalSubscriptionInitFlag == 0) ? -1 : 0;
 }
 
 int LocalSubscriptionOpGet(char * uri_repository, char * uuid, unsigned char  **subscriptionDoc, int *len)
@@ -85,24 +94,32 @@ int LocalSubscriptionOpGet(char * uri_repository, char * uuid, unsigned char  **
 	unsigned char *buf = NULL;
 	int count,m;
 	int pre_count;
+        char *subs_path;
+        FILE *fp;
+
 	count = m = 0;
 	*subscriptionDoc = NULL;
-	if(LocalSubscriptionInitFlag == 0) return -1;
-	char *subs_path = u_strdup_printf ("%s/uuid:%s", uri_repository, uuid);
-	FILE *fp = fopen(subs_path, "r");
+	if (LocalSubscriptionInitFlag == 0)
+          return -1;
+	subs_path = u_strdup_printf ("%s/uuid:%s", uri_repository, uuid);
+	fp = fopen(subs_path, "r");
+	if (fp == NULL) {
+          error("Can't open %s: %s", subs_path, strerror(errno));
+          u_free(subs_path);
+          return -1;
+        }
 	u_free(subs_path);
-	if(fp == NULL) return -1;
-	while(!feof(fp)) {
-				memset(block, 0, 512);
-				m = fread(block, 1, 511, fp);
-				if(m > 0) {
-					debug("read [%s] from file, len = %d",block, m);
-					pre_count = count;
-					count += m;
-					debug("buf = %0x, count = %d", buf, count);
-					buf = u_realloc(buf, count);
-					memcpy(buf+pre_count, block, m);
-				}
+	while (!feof(fp)) {
+          memset(block, 0, 512);
+          m = fread(block, 1, 511, fp);
+          if (m > 0) {
+            debug("read [%s] from file, len = %d",block, m);
+            pre_count = count;
+            count += m;
+            debug("buf = %0x, count = %d", buf, count);
+            buf = u_realloc(buf, count);
+            memcpy(buf+pre_count, block, m);
+          }
 	}
 	fclose(fp);
 	*subscriptionDoc = buf;
@@ -112,11 +129,18 @@ int LocalSubscriptionOpGet(char * uri_repository, char * uuid, unsigned char  **
 
 int LocalSubscriptionOpSearch(char * uri_repository, char * uuid)
 {
-	if(LocalSubscriptionInitFlag == 0) return -1;
-	char *subs_path = u_strdup_printf ("%s/uuid:%s", uri_repository, uuid);
-	FILE *fp = fopen(subs_path, "r");
+  char *subs_path;
+  FILE *fp;
+	if (LocalSubscriptionInitFlag == 0)
+          return -1;
+	subs_path = u_strdup_printf ("%s/uuid:%s", uri_repository, uuid);
+	fp = fopen(subs_path, "r");
+	if (fp == NULL) {
+          error("Can't open %s: %s", subs_path, strerror(errno));
+          u_free(subs_path);
+          return -1;
+        }
 	u_free(subs_path);
-	if(fp == NULL) return -1;
 	fclose(fp);
 	return 0;
 }
@@ -128,29 +152,38 @@ int LocalSubscriptionOpLoad (char * uri_repository, list_t * subscription_list)
 	int pre_count;
 	char block[512];
 	unsigned char *buf = NULL;
-	if(LocalSubscriptionInitFlag == 0) return -1;
-	if(subscription_list == NULL)
-		return -1;
-	if (0 > (n = scandir (uri_repository, &namelist, 0, alphasort)))
-    	{
-       	 return -1;
-    	} else {
-        	while (n--)
-        	{
+
+	if (LocalSubscriptionInitFlag == 0)
+          return -1;
+	if (subscription_list == NULL)
+          return -1;
+	if (0 > (n = scandir (uri_repository, &namelist, 0, alphasort))) {
+       	  return -1;
+    	}
+        else {
+        	while (n--) {
+                        char *subs_path;
+                        FILE *subs;
         		lnode_t *node;
-			if(strstr(namelist[n]->d_name,"uuid") == NULL || strlen(namelist[n]->d_name) < 41) {
+
+			if (strstr(namelist[n]->d_name,"uuid") == NULL || strlen(namelist[n]->d_name) < 41) {
 				u_free(namelist[n]);
 				continue;
 			}
-			char *subs_path = u_strdup_printf ("%s/%s", uri_repository, namelist[n]->d_name);
-			FILE *subs = fopen(subs_path, "r");
+			subs_path = u_strdup_printf ("%s/%s", uri_repository, namelist[n]->d_name);
+			subs = fopen(subs_path, "r");
+                        if (subs == NULL) {
+                          error("Can't open %s: %s", subs_path, strerror(errno));
+                          u_free(subs_path);
+                          return -1;
+                        }
 			u_free(subs_path);
 			count = 0;
 			buf = NULL;
-			while(!feof(subs)) {
+			while (!feof(subs)) {
 				memset(block, 0, 512);
 				m = fread(block, 1, 511, subs);
-				if(m > 0) {
+				if (m > 0) {
 					pre_count = count;
 					count += m;
 					buf = u_realloc(buf, count);
@@ -159,47 +192,64 @@ int LocalSubscriptionOpLoad (char * uri_repository, list_t * subscription_list)
 			}
 			fclose(subs);
 			SubsRepositoryEntryH entry = u_malloc(sizeof(*entry));
-			if(entry) {
+			if (entry) {
 				entry->strdoc = buf;
 				entry->len = count;
 				entry->uuid = u_strdup(namelist[n]->d_name);
 				node = lnode_create(entry);
             			list_append(subscription_list, node);
             			debug("subscription file found: %s", namelist[n]->d_name );
+			} else {
+				u_free(buf);
 			}
             		u_free(namelist[n]);
         	}
-       	 u_free(namelist);
+                u_free(namelist);
     	}
 	return 0;
 }
+
 int LocalSubscriptionOpSave (char * uri_repository, char * uuid, unsigned char *subscriptionDoc)
 {
 	char buf[U_NAME_MAX];
-	if(LocalSubscriptionInitFlag == 0) return -1;
+        FILE *subsfile;
+
+	if (LocalSubscriptionInitFlag == 0)
+          return -1;
 	snprintf(buf, U_NAME_MAX, "%s/uuid:%s", uri_repository, uuid);
-	FILE *subsfile = fopen(buf, "w");
-	if(subsfile == NULL) return -1;
+	subsfile = fopen(buf, "w");
+	if (subsfile == NULL) {
+          error("Can't open %s: %s", buf, strerror(errno));
+          return -1;
+        }
 	fprintf(subsfile, "%s", subscriptionDoc);
 	fclose(subsfile);
 	return 0;
 }
+
 int LocalSubscriptionOpUpdate(char * uri_repository, char * uuid, char *expire)
 {
 	char buf[U_NAME_MAX];
-	if(LocalSubscriptionInitFlag == 0) return -1;
-	snprintf(buf, U_NAME_MAX, "%s/uuid:%s", uri_repository, uuid);
 	WsXmlDocH doc= NULL;
 	char *temp;
 	int len = 0;
+
+	if (LocalSubscriptionInitFlag == 0)
+          return -1;
+	snprintf(buf, U_NAME_MAX, "%s/uuid:%s", uri_repository, uuid);
 	doc = xml_parser_file_to_doc( buf, "UTF-8", 0);
-	if(doc) {
+	if (doc) {
+		FILE *subsfile;
 		WsXmlNodeH node = ws_xml_get_child(ws_xml_get_soap_body(doc),
 			0, XML_NS_EVENTING, WSEVENT_SUBSCRIBE);
 		node = ws_xml_get_child(node, 0, XML_NS_EVENTING, WSEVENT_EXPIRES);
 		ws_xml_set_node_text(node, expire);
 		ws_xml_dump_memory_enc(doc, &temp, &len, "UTF-8");
-		FILE *subsfile = fopen(buf, "w");
+		subsfile = fopen(buf, "w");
+                if (subsfile == NULL) {
+                  error("Can't open %s: %s", buf, strerror(errno));
+                  return -1;
+                }
 		fprintf(subsfile, "%s", temp);
 		fclose(subsfile);
 		ws_xml_free_memory(temp);
@@ -207,51 +257,16 @@ int LocalSubscriptionOpUpdate(char * uri_repository, char * uuid, char *expire)
 	}
 	return 0;
 }
+
 int LocalSubscriptionOpDelete (char * uri_repository, char * uuid)
 {
 	char buf[U_NAME_MAX];
-	if(LocalSubscriptionInitFlag == 0) return -1;
+
+	if (LocalSubscriptionInitFlag == 0)
+          return -1;
 	snprintf(buf, U_NAME_MAX, "%s/uuid:%s", uri_repository, uuid);
-	int r = unlink(buf);
-	if(r) {
+	if (unlink(buf)) {
 		debug("unlink %s failed! %s", buf, strerror(errno));
 	}
 	return 0;
 }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-- 
2.7.0

