commit 4fe9d9239f172604e16268ca1fb6fff1c06632b2
Author: Ankit Kumar <ankit@linux.vnet.ibm.com>
Date:   Sat May 20 01:38:41 2017 +0530

    Validate text string before and after bind call
    
    While binding string to query, if string is NULL then bind call gets ignored.
    While fetching data from string, if data is NULL then current code makes query
    as NULL and returns.
    
    This patch validates text string before and after bind call. It assigns string
    to "" incase data is NULL points to NULL pointer. After this we will be able
    to display information even if some of string data is NULL.
    
    It also does NULL checks for compulsory string.
    
    Signed-off-by: Ankit Kumar <ankit@linux.vnet.ibm.com>
    [Killed redundant goto statements - Vasant]
    Signed-off-by: Vasant Hegde <hegdevasant@linux.vnet.ibm.com>

diff --git a/src/addl_data.c b/src/addl_data.c
index 606d53b..33aa235 100644
--- a/src/addl_data.c
+++ b/src/addl_data.c
@@ -71,6 +71,24 @@ insert_addl_data_os(servicelog *slog, struct sl_event *event)
 	else
 		version = os->version;
 
+	if (os->subsystem == NULL) {
+		snprintf(slog->error, SL_MAX_ERR,
+			 "OS subsystem must be specified\n");
+		return -1;
+	}
+
+	if (os->driver == NULL) {
+		snprintf(slog->error, SL_MAX_ERR,
+			 "OS driver must be specified\n");
+		return -1;
+	}
+
+	if (os->device == NULL) {
+		snprintf(slog->error, SL_MAX_ERR,
+			 "Device name must be specified\n");
+		return -1;
+	}
+
 	rc = sqlite3_prepare(slog->db, "INSERT OR REPLACE INTO os (event_id,"
 		" version, subsystem, driver, device) VALUES (?, ?, ?, ?, ?);",
 		 -1, &pstmt, &out);
@@ -81,11 +99,11 @@ insert_addl_data_os(servicelog *slog, struct sl_event *event)
 	rc = rc ? rc : sqlite3_bind_text(pstmt, 2, version,
 				version ? strlen(version):0, SQLITE_STATIC);
 	rc = rc ? rc : sqlite3_bind_text(pstmt, 3, os->subsystem,
-			os->subsystem ? strlen(os->subsystem):0, SQLITE_STATIC);
+			strlen(os->subsystem), SQLITE_STATIC);
 	rc = rc ? rc : sqlite3_bind_text(pstmt, 4, os->driver,
-			os->driver ? strlen(os->driver):0, SQLITE_STATIC);
+			strlen(os->driver), SQLITE_STATIC);
 	rc = rc ? rc : sqlite3_bind_text(pstmt, 5, os->device,
-			os->device ? strlen(os->device):0, SQLITE_STATIC);
+			strlen(os->device), SQLITE_STATIC);
 
 	if (rc != SQLITE_OK)
 		goto sqlt_fail;
@@ -178,12 +196,24 @@ int
 insert_addl_data_enclosure(servicelog *slog, struct sl_event *event)
 {
 	const char *out;
-	int rc;
+	int rc = -1;
 	sqlite3_stmt *pstmt = NULL;
 	struct sl_data_enclosure *encl;
 
 	encl = (struct sl_data_enclosure *)event->addl_data;
 
+	if (encl->enclosure_serial == NULL) {
+		snprintf(slog->error, SL_MAX_ERR,
+			 "Enclosure serial number is NULL\n");
+		return rc;
+	}
+
+	if (encl->enclosure_model == NULL) {
+		snprintf(slog->error, SL_MAX_ERR,
+			 "Enclosure model name is NULL\n");
+		return rc;
+	}
+
 	rc = sqlite3_prepare(slog->db, "INSERT OR REPLACE INTO enclosure"
 		" (event_id, enclosure_model, enclosure_serial) VALUES (?,"
 		" ?, ?);", -1, &pstmt, &out);
@@ -195,11 +225,9 @@ insert_addl_data_enclosure(servicelog *slog, struct sl_event *event)
 
 	rc = sqlite3_bind_int64(pstmt, 1, event->id);
 	rc = rc ? rc : sqlite3_bind_text(pstmt, 2, encl->enclosure_model,
-			encl->enclosure_model ? strlen(encl->enclosure_model):0
-					, SQLITE_STATIC);
+			strlen(encl->enclosure_model), SQLITE_STATIC);
 	rc = rc ? rc : sqlite3_bind_text(pstmt, 3, encl->enclosure_serial,
-		encl->enclosure_serial ? strlen(encl->enclosure_serial):0,
-		SQLITE_STATIC);
+		strlen(encl->enclosure_serial), SQLITE_STATIC);
 
 	if (rc != SQLITE_OK)
 		goto sqlt_fail;
@@ -211,6 +239,7 @@ insert_addl_data_enclosure(servicelog *slog, struct sl_event *event)
 	rc = sqlite3_finalize(pstmt);
 	return rc;
 
+
 sqlt_fail:
 	snprintf(slog->error, SL_MAX_ERR, "%s", sqlite3_errmsg(slog->db));
 	rc = sqlite3_finalize(pstmt);
diff --git a/src/event.c b/src/event.c
index ad40705..32c6a9c 100644
--- a/src/event.c
+++ b/src/event.c
@@ -639,7 +639,7 @@ servicelog_event_query(servicelog *slog, char *query,
 			else if (!strcmp(name, "platform")) {
 				str = (char *)sqlite3_column_text(stmt, i);
 				if (!str)
-					goto free_mem;
+					str = "";
 				e->platform = strdup(str);
 				if (!e->platform)
 					goto free_mem;
@@ -647,7 +647,7 @@ servicelog_event_query(servicelog *slog, char *query,
 			else if (!strcmp(name, "machine_serial")) {
 				str = (char *)sqlite3_column_text(stmt, i);
 				if (!str)
-					goto free_mem;
+					str = "";
 				e->machine_serial = strdup(str);
 				if (!e->machine_serial)
 					goto free_mem;
@@ -655,7 +655,7 @@ servicelog_event_query(servicelog *slog, char *query,
 			else if (!strcmp(name, "machine_model")) {
 				str = (char *)sqlite3_column_text(stmt, i);
 				if (!str)
-					goto free_mem;
+					str = "";
 				e->machine_model = strdup(str);
 				if (!e->machine_model)
 					goto free_mem;
@@ -663,7 +663,7 @@ servicelog_event_query(servicelog *slog, char *query,
 			else if (!strcmp(name, "nodename")) {
 				str = (char *)sqlite3_column_text(stmt, i);
 				if (!str)
-					goto free_mem;
+					str = "";
 				e->nodename = strdup(str);
 				if (!e->nodename)
 					goto free_mem;
@@ -671,7 +671,7 @@ servicelog_event_query(servicelog *slog, char *query,
 			else if (!strcmp(name, "refcode")) {
 				str = (char *)sqlite3_column_text(stmt, i);
 				if (!str)
-					goto free_mem;
+					str = "";
 				e->refcode = strdup(str);
 				if (!e->refcode)
 					goto free_mem;
@@ -679,7 +679,7 @@ servicelog_event_query(servicelog *slog, char *query,
 			else if (!strcmp(name, "description")) {
 				str = (char *)sqlite3_column_text(stmt, i);
 				if (!str)
-					goto free_mem;
+					str = "";
 				e->description = strdup(str);
 				if (!e->description)
 					goto free_mem;
diff --git a/src/notify.c b/src/notify.c
index 9035e6c..e7c9a2c 100644
--- a/src/notify.c
+++ b/src/notify.c
@@ -262,7 +262,7 @@ servicelog_notify_query(servicelog *slog, char *query,
 			else if (!strcmp(name, "command")) {
 				str = (char *)sqlite3_column_text(stmt, i);
 				if (!str)
-					goto free_mem;
+					str = "";
 				n->command = strdup(str);
 				if (!n->command)
 					goto free_mem;
@@ -272,7 +272,7 @@ servicelog_notify_query(servicelog *slog, char *query,
 			else if (!strcmp(name, "match")) {
 				str = (char *)sqlite3_column_text(stmt, i);
 				if (!str)
-					goto free_mem;
+					str = "";
 				n->match = strdup(str);
 				if (!n->match)
 					goto free_mem;
diff --git a/src/repair_action.c b/src/repair_action.c
index ea21e67..1efa05a 100644
--- a/src/repair_action.c
+++ b/src/repair_action.c
@@ -395,7 +395,7 @@ servicelog_repair_query(servicelog *slog, char *query,
 			else if (!strcmp(name, "procedure")) {
 				str = (char *)sqlite3_column_text(stmt, i);
 				if (!str)
-					goto free_mem;
+					str = "";
 				r->procedure = strdup(str);
 				if (!r->procedure)
 					goto free_mem;
@@ -403,7 +403,7 @@ servicelog_repair_query(servicelog *slog, char *query,
 			else if (!strcmp(name, "location")) {
 				str = (char *)sqlite3_column_text(stmt, i);
 				if (!str)
-					goto free_mem;
+					str = "";
 				r->location = strdup(str);
 				if (!r->location)
 					goto free_mem;
@@ -411,7 +411,7 @@ servicelog_repair_query(servicelog *slog, char *query,
 			else if (!strcmp(name, "platform")) {
 				str = (char *)sqlite3_column_text(stmt, i);
 				if (!str)
-					goto free_mem;
+					str = "";
 				r->platform = strdup(str);
 				if (!r->platform)
 					goto free_mem;
@@ -419,7 +419,7 @@ servicelog_repair_query(servicelog *slog, char *query,
 			else if (!strcmp(name, "machine_serial")) {
 				str = (char *)sqlite3_column_text(stmt, i);
 				if (!str)
-					goto free_mem;
+					str = "";
 				r->machine_serial = strdup(str);
 				if (!r->machine_serial)
 					goto free_mem;
@@ -427,7 +427,7 @@ servicelog_repair_query(servicelog *slog, char *query,
 			else if (!strcmp(name, "machine_model")) {
 				str = (char *)sqlite3_column_text(stmt, i);
 				if (!str)
-					goto free_mem;
+					str = "";
 				r->machine_model = strdup(str);
 				if (!r->machine_model)
 					goto free_mem;
@@ -435,7 +435,7 @@ servicelog_repair_query(servicelog *slog, char *query,
 			else if (!strcmp(name, "notes")) {
 				str = (char *)sqlite3_column_text(stmt, i);
 				if (!str)
-					goto free_mem;
+					str = "";
 				r->notes = strdup(str);
 				if (!r->notes)
 					goto free_mem;
