You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@celix.apache.org by pn...@apache.org on 2016/10/26 12:15:35 UTC

[12/50] [abbrv] celix git commit: Fixed many Coverity issues

Fixed many Coverity issues


Project: http://git-wip-us.apache.org/repos/asf/celix/repo
Commit: http://git-wip-us.apache.org/repos/asf/celix/commit/7766b2a7
Tree: http://git-wip-us.apache.org/repos/asf/celix/tree/7766b2a7
Diff: http://git-wip-us.apache.org/repos/asf/celix/diff/7766b2a7

Branch: refs/heads/master
Commit: 7766b2a73ecbf9bc712d00a1ead3211e64d93f0d
Parents: 5521c0c
Author: gricciardi <gr...@apache.org>
Authored: Mon Sep 26 15:10:49 2016 +0200
Committer: gricciardi <gr...@apache.org>
Committed: Mon Sep 26 15:10:49 2016 +0200

----------------------------------------------------------------------
 .../src/dm_activator_base_cxx.cc                |  8 +--
 deployment_admin/private/src/deployment_admin.c | 31 ++++++-----
 .../dm_example_cxx/phase1/include/Phase1Cmp.h   |  2 +-
 framework/private/src/bundle_archive.c          | 22 ++++++--
 framework/private/src/framework.c               |  3 +
 framework/private/src/properties.c              | 58 ++++++++++++--------
 shell/private/src/update_command.c              | 31 ++++++-----
 7 files changed, 94 insertions(+), 61 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/celix/blob/7766b2a7/dependency_manager_cxx/src/dm_activator_base_cxx.cc
----------------------------------------------------------------------
diff --git a/dependency_manager_cxx/src/dm_activator_base_cxx.cc b/dependency_manager_cxx/src/dm_activator_base_cxx.cc
index f2b6328..f895d0e 100644
--- a/dependency_manager_cxx/src/dm_activator_base_cxx.cc
+++ b/dependency_manager_cxx/src/dm_activator_base_cxx.cc
@@ -33,10 +33,10 @@ struct dm_dependency_activator_base {
     dm_dependency_activator_base(std::shared_ptr<celix::dm::DependencyManager> man, std::shared_ptr<celix::dm::DmActivator> act) : manager(man), activator(act) {}
     bundle_context_pt context {nullptr};
     service_registration_pt reg {nullptr};
-    dm_info_service_t info;
+    dm_info_service_t info {nullptr,nullptr,nullptr};
 
-    std::shared_ptr<celix::dm::DependencyManager> manager;
-    std::shared_ptr<celix::dm::DmActivator> activator;
+    std::shared_ptr<celix::dm::DependencyManager> manager {nullptr};
+    std::shared_ptr<celix::dm::DmActivator> activator {nullptr};
 };
 
 typedef struct dm_dependency_activator_base *dependency_activator_base_pt;
@@ -127,4 +127,4 @@ celix_status_t bundleActivator_destroy(void *userData, bundle_context_pt context
     return status;
 }
 
-}
\ No newline at end of file
+}

http://git-wip-us.apache.org/repos/asf/celix/blob/7766b2a7/deployment_admin/private/src/deployment_admin.c
----------------------------------------------------------------------
diff --git a/deployment_admin/private/src/deployment_admin.c b/deployment_admin/private/src/deployment_admin.c
index bac94f4..6c8c505 100644
--- a/deployment_admin/private/src/deployment_admin.c
+++ b/deployment_admin/private/src/deployment_admin.c
@@ -457,21 +457,26 @@ celix_status_t deploymentAdmin_download(deployment_admin_pt admin, char * url, c
         int fd = mkstemp(*inputFile);
         if (fd != -1) {
             FILE *fp = fopen(*inputFile, "wb+");
-            curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
-            curl_easy_setopt(curl, CURLOPT_URL, url);
-            curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, deploymentAdmin_writeData);
-            curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
-            curl_easy_setopt(curl, CURLOPT_FAILONERROR, true);
-            //curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0);
-            //curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, updateCommand_downloadProgress);
-            res = curl_easy_perform(curl);
-
-            /* always cleanup */
-            curl_easy_cleanup(curl);
-            fclose(fp);
+            if(fp!=NULL){
+            	curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
+            	curl_easy_setopt(curl, CURLOPT_URL, url);
+            	curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, deploymentAdmin_writeData);
+            	curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
+            	curl_easy_setopt(curl, CURLOPT_FAILONERROR, true);
+            	//curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0);
+            	//curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, updateCommand_downloadProgress);
+            	res = curl_easy_perform(curl);
+
+            	/* always cleanup */
+            	curl_easy_cleanup(curl);
+            	fclose(fp);
+            }
+            else{
+            	status = CELIX_FILE_IO_EXCEPTION;
+            }
         }
         else{
-		status = CELIX_FILE_IO_EXCEPTION;
+        	status = CELIX_FILE_IO_EXCEPTION;
         }
 	}
 	else{

http://git-wip-us.apache.org/repos/asf/celix/blob/7766b2a7/examples/dm_example_cxx/phase1/include/Phase1Cmp.h
----------------------------------------------------------------------
diff --git a/examples/dm_example_cxx/phase1/include/Phase1Cmp.h b/examples/dm_example_cxx/phase1/include/Phase1Cmp.h
index f847413..4a4662d 100644
--- a/examples/dm_example_cxx/phase1/include/Phase1Cmp.h
+++ b/examples/dm_example_cxx/phase1/include/Phase1Cmp.h
@@ -27,7 +27,7 @@
 #define IPHASE1_VERSION "1.0.0"
 
 class Phase1Cmp : public IPhase1 {
-    uint32_t counter;
+    uint32_t counter = 0;
 public:
     Phase1Cmp() = default;
     virtual ~Phase1Cmp() = default;

http://git-wip-us.apache.org/repos/asf/celix/blob/7766b2a7/framework/private/src/bundle_archive.c
----------------------------------------------------------------------
diff --git a/framework/private/src/bundle_archive.c b/framework/private/src/bundle_archive.c
index eee53b3..3d0513d 100644
--- a/framework/private/src/bundle_archive.c
+++ b/framework/private/src/bundle_archive.c
@@ -253,9 +253,14 @@ celix_status_t bundleArchive_getId(bundle_archive_pt archive, long *id) {
 		snprintf(bundleId, sizeof(bundleId), "%s/bundle.id", archive->archiveRoot);
 
 		bundleIdFile = fopen(bundleId, "r");
-		fgets(id, sizeof(id), bundleIdFile);
-		fclose(bundleIdFile);
-		sscanf(id, "%ld", &archive->id);
+		if(bundleIdFile!=NULL){
+			fgets(id, sizeof(id), bundleIdFile);
+			fclose(bundleIdFile);
+			sscanf(id, "%ld", &archive->id);
+		}
+		else{
+			status = CELIX_FILE_IO_EXCEPTION;
+		}
 	}
 
 	if (status == CELIX_SUCCESS) {
@@ -277,9 +282,14 @@ celix_status_t bundleArchive_getLocation(bundle_archive_pt archive, const char *
 		snprintf(bundleLocation, sizeof(bundleLocation), "%s/bundle.location", archive->archiveRoot);
 
 		bundleLocationFile = fopen(bundleLocation, "r");
-		fgets(loc, sizeof(loc), bundleLocationFile);
-		fclose(bundleLocationFile);
-		archive->location = strdup(loc);
+		if(bundleLocationFile!=NULL){
+			fgets(loc, sizeof(loc), bundleLocationFile);
+			fclose(bundleLocationFile);
+			archive->location = strdup(loc);
+		}
+		else{
+			status = CELIX_FILE_IO_EXCEPTION;
+		}
 	}
 
 	if (status == CELIX_SUCCESS) {

http://git-wip-us.apache.org/repos/asf/celix/blob/7766b2a7/framework/private/src/framework.c
----------------------------------------------------------------------
diff --git a/framework/private/src/framework.c b/framework/private/src/framework.c
index c6da0d5..c317de5 100644
--- a/framework/private/src/framework.c
+++ b/framework/private/src/framework.c
@@ -2546,6 +2546,9 @@ static celix_status_t framework_loadLibraries(framework_pt framework, const char
         if ( (status == CELIX_SUCCESS) && (activator != NULL) && (strcmp(trimmedLib, activator) == 0) ) {
 		    *activatorHandle = handle;
         }
+        else if(handle!=NULL){
+        	fw_closeLibrary(handle);
+        }
 
         token = strtok_r(NULL, ",", &last);
     }

http://git-wip-us.apache.org/repos/asf/celix/blob/7766b2a7/framework/private/src/properties.c
----------------------------------------------------------------------
diff --git a/framework/private/src/properties.c b/framework/private/src/properties.c
index 3151de5..cd3b1f1 100644
--- a/framework/private/src/properties.c
+++ b/framework/private/src/properties.c
@@ -52,6 +52,9 @@ void properties_destroy(properties_pt properties) {
 
 properties_pt properties_load(const char* filename) {
 	FILE *file = fopen(filename, "r");
+	if(file==NULL){
+		return NULL;
+	}
 	properties_pt props = properties_loadWithStream(file);
 	fclose(file);
 	return props;
@@ -59,7 +62,7 @@ properties_pt properties_load(const char* filename) {
 
 FRAMEWORK_EXPORT properties_pt properties_loadWithStream(FILE *file) {
 	properties_pt props = NULL;
-	
+
 
 	if (file != NULL ) {
 		char *saveptr;
@@ -72,16 +75,22 @@ FRAMEWORK_EXPORT properties_pt properties_loadWithStream(FILE *file) {
 		file_size = ftell(file);
 		fseek(file, 0, SEEK_SET);
 
-		filebuffer = calloc(file_size + 1, sizeof(char));
-                if(filebuffer) {
-                    fread(filebuffer, sizeof(char), file_size, file);
-                    line = strtok_r(filebuffer, "\n", &saveptr);
-                    while ( line != NULL ) {
-			    parseLine(line, props);
-                            line = strtok_r(NULL, "\n", &saveptr);
-                    }
-                    free(filebuffer);
-                }
+		if(file_size > 0){
+			filebuffer = calloc(file_size + 1, sizeof(char));
+			if(filebuffer) {
+				size_t rs = fread(filebuffer, sizeof(char), file_size, file);
+				if(rs != file_size){
+					fprintf(stderr,"fread read only %lu bytes out of %lu\n",rs,file_size);
+				}
+				filebuffer[file_size]='\0';
+				line = strtok_r(filebuffer, "\n", &saveptr);
+				while ( line != NULL ) {
+					parseLine(line, props);
+					line = strtok_r(NULL, "\n", &saveptr);
+				}
+				free(filebuffer);
+			}
+		}
 	}
 
 	return props;
@@ -163,16 +172,16 @@ const char* properties_getWithDefault(properties_pt properties, const char* key,
 }
 
 void properties_set(properties_pt properties, const char* key, const char* value) {
-    hash_map_entry_pt entry = hashMap_getEntry(properties, key);
-    char* oldValue = NULL;
+	hash_map_entry_pt entry = hashMap_getEntry(properties, key);
+	char* oldValue = NULL;
 	if (entry != NULL) {
-        char* oldKey = hashMapEntry_getKey(entry);
-        oldValue = hashMapEntry_getValue(entry);
-        hashMap_put(properties, oldKey, strndup(value, 1024*10));
+		char* oldKey = hashMapEntry_getKey(entry);
+		oldValue = hashMapEntry_getValue(entry);
+		hashMap_put(properties, oldKey, strndup(value, 1024*10));
 	} else {
-        hashMap_put(properties, strndup(key, 1024*10), strndup(value, 1024*10));
-    }
-    free(oldValue);
+		hashMap_put(properties, strndup(key, 1024*10), strndup(value, 1024*10));
+	}
+	free(oldValue);
 }
 
 static void updateBuffers(char **key, char ** value, char **output, int outputPos, int *key_len, int *value_len) {
@@ -200,21 +209,22 @@ static void parseLine(const char* line, properties_pt props) {
 	char *output = NULL;
 	int key_len = MALLOC_BLOCK_SIZE;
 	int value_len = MALLOC_BLOCK_SIZE;
-	char *key = calloc(1, key_len);
-	char *value = calloc(1, value_len);
 	linePos = 0;
 	precedingCharIsBackslash = false;
 	isComment = false;
 	output = NULL;
 	outputPos = 0;
-	key[0] = '\0';
-	value[0] = '\0';
 
 	//Ignore empty lines
 	if (line[0] == '\n' && line[1] == '\0') {
 		return;
 	}
 
+	char *key = calloc(1, key_len);
+	char *value = calloc(1, value_len);
+	key[0] = '\0';
+	value[0] = '\0';
+
 	while (line[linePos] != '\0') {
 		if (line[linePos] == ' ' || line[linePos] == '\t') {
 			if (output == NULL) {
@@ -293,4 +303,4 @@ static void parseLine(const char* line, properties_pt props) {
 		free(value);
 	}
 
-}
\ No newline at end of file
+}

http://git-wip-us.apache.org/repos/asf/celix/blob/7766b2a7/shell/private/src/update_command.c
----------------------------------------------------------------------
diff --git a/shell/private/src/update_command.c b/shell/private/src/update_command.c
index a6157a8..0e45fd7 100644
--- a/shell/private/src/update_command.c
+++ b/shell/private/src/update_command.c
@@ -75,7 +75,7 @@ void updateCommand_execute(void *handle, char * line, FILE *outStream, FILE *err
 
 celix_status_t updateCommand_download(bundle_context_pt context, char * url, char **inputFile) {
 	CURL *curl = NULL;
-	CURLcode res = 0;
+	CURLcode res = CURLE_FILE_COULDNT_READ_FILE;
 	curl = curl_easy_init();
 	if (curl) {
 		FILE *fp = NULL;
@@ -84,21 +84,26 @@ celix_status_t updateCommand_download(bundle_context_pt context, char * url, cha
 		int fd = mkstemp(*inputFile);
 		if (fd) {
 		    fp = fopen(*inputFile, "wb+");
-            printf("Temp file: %s\n", *inputFile);
-            curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
-            curl_easy_setopt(curl, CURLOPT_URL, url);
-            curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, updateCommand_writeData);
-            curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
-            //curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0);
-            //curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, updateCommand_downloadProgress);
-            res = curl_easy_perform(curl);
-            printf("Error: %d\n", res);
-            /* always cleanup */
-            curl_easy_cleanup(curl);
-            fclose(fp);
+		    if(fp!=NULL){
+		    	printf("Temp file: %s\n", *inputFile);
+		    	curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
+		    	curl_easy_setopt(curl, CURLOPT_URL, url);
+		    	curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, updateCommand_writeData);
+		    	curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
+		    	//curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0);
+		    	//curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, updateCommand_downloadProgress);
+		    	res = curl_easy_perform(curl);
+		    	fclose(fp);
+		    }
+		    /* always cleanup */
+		    curl_easy_cleanup(curl);
+		    if(fp==NULL){
+		    	return CELIX_FILE_IO_EXCEPTION;
+		    }
 		}
 	}
 	if (res != CURLE_OK) {
+		printf("Error: %d\n", res);
 		*inputFile[0] = '\0';
 		return CELIX_ILLEGAL_STATE;
 	} else {