Fix some json schema validation bugs

There were a couple of places where we would add null objects when
they were not allowed.  Fix them.

Change-Id: I7c4c12ea1fa2913014e79603995267a9e560e288
Signed-off-by: Ed Tanous <etanous@nvidia.com>
diff --git a/sections/cper-section-arm.c b/sections/cper-section-arm.c
index 6961d16..8cce3c4 100644
--- a/sections/cper-section-arm.c
+++ b/sections/cper-section-arm.c
@@ -276,8 +276,11 @@
 			//Unknown/microarch, will not support.
 			break;
 		}
-		json_object_object_add(error_info_ir, "errorInformation",
-				       error_subinfo);
+		if (error_subinfo != NULL) {
+			json_object_object_add(error_info_ir,
+					       "errorInformation",
+					       error_subinfo);
+		}
 	}
 
 	//Virtual fault address, physical fault address.
diff --git a/sections/cper-section-cxl-protocol.c b/sections/cper-section-cxl-protocol.c
index ebf3648..6ae5325 100644
--- a/sections/cper-section-cxl-protocol.c
+++ b/sections/cper-section-cxl-protocol.c
@@ -42,46 +42,47 @@
 			"Unknown (Reserved)");
 		json_object_object_add(section_ir, "agentType", agent_type);
 	}
-
-	//CXL agent address, depending on the agent type.
-	json_object *agent_address = json_object_new_object();
-	if (cxl_protocol_error->CxlAgentType ==
-	    CXL_PROTOCOL_ERROR_DEVICE_AGENT) {
-		//Address is a CXL1.1 device agent.
-		json_object_object_add(
-			agent_address, "functionNumber",
-			json_object_new_uint64(
-				cxl_protocol_error->CxlAgentAddress
-					.DeviceAddress.FunctionNumber));
-		json_object_object_add(
-			agent_address, "deviceNumber",
-			json_object_new_uint64(
-				cxl_protocol_error->CxlAgentAddress
-					.DeviceAddress.DeviceNumber));
-		json_object_object_add(
-			agent_address, "busNumber",
-			json_object_new_uint64(
-				cxl_protocol_error->CxlAgentAddress
-					.DeviceAddress.BusNumber));
-		json_object_object_add(
-			agent_address, "segmentNumber",
-			json_object_new_uint64(
-				cxl_protocol_error->CxlAgentAddress
-					.DeviceAddress.SegmentNumber));
-	} else if (cxl_protocol_error->CxlAgentType ==
-		   CXL_PROTOCOL_ERROR_HOST_DOWNSTREAM_PORT_AGENT) {
-		//Address is a CXL port RCRB base address.
-		json_object_object_add(
-			agent_address, "value",
-			json_object_new_uint64(
-				cxl_protocol_error->CxlAgentAddress
-					.PortRcrbBaseAddress));
-	}
 	if (isvalid_prop_to_ir(&ui64Type, 1)) {
-		json_object_object_add(section_ir, "cxlAgentAddress",
-				       agent_address);
-	} else {
-		json_object_put(agent_address);
+		//CXL agent address, depending on the agent type.
+		json_object *agent_address = NULL;
+		if (cxl_protocol_error->CxlAgentType ==
+		    CXL_PROTOCOL_ERROR_DEVICE_AGENT) {
+			agent_address = json_object_new_object();
+			//Address is a CXL1.1 device agent.
+			json_object_object_add(
+				agent_address, "functionNumber",
+				json_object_new_uint64(
+					cxl_protocol_error->CxlAgentAddress
+						.DeviceAddress.FunctionNumber));
+			json_object_object_add(
+				agent_address, "deviceNumber",
+				json_object_new_uint64(
+					cxl_protocol_error->CxlAgentAddress
+						.DeviceAddress.DeviceNumber));
+			json_object_object_add(
+				agent_address, "busNumber",
+				json_object_new_uint64(
+					cxl_protocol_error->CxlAgentAddress
+						.DeviceAddress.BusNumber));
+			json_object_object_add(
+				agent_address, "segmentNumber",
+				json_object_new_uint64(
+					cxl_protocol_error->CxlAgentAddress
+						.DeviceAddress.SegmentNumber));
+		} else if (cxl_protocol_error->CxlAgentType ==
+			   CXL_PROTOCOL_ERROR_HOST_DOWNSTREAM_PORT_AGENT) {
+			agent_address = json_object_new_object();
+			//Address is a CXL port RCRB base address.
+			json_object_object_add(
+				agent_address, "value",
+				json_object_new_uint64(
+					cxl_protocol_error->CxlAgentAddress
+						.PortRcrbBaseAddress));
+		}
+		if (agent_address != NULL) {
+			json_object_object_add(section_ir, "cxlAgentAddress",
+					       agent_address);
+		}
 	}
 
 	json_object *device_id = json_object_new_object();
diff --git a/sections/cper-section-ia32x64.c b/sections/cper-section-ia32x64.c
index 8f84c11..2206d2a 100644
--- a/sections/cper-section-ia32x64.c
+++ b/sections/cper-section-ia32x64.c
@@ -130,10 +130,14 @@
 	void *cur_pos = (void *)current_context_info;
 	json_object *context_info_array = json_object_new_array();
 	for (int i = 0; i < processor_context_info_num; i++) {
-		json_object_array_add(context_info_array,
-				      cper_ia32x64_processor_context_info_to_ir(
-					      current_context_info, &cur_pos,
-					      &remaining_size));
+		json_object *context_info =
+			cper_ia32x64_processor_context_info_to_ir(
+				current_context_info, &cur_pos,
+				&remaining_size);
+		if (context_info == NULL) {
+			context_info = json_object_new_object();
+		}
+		json_object_array_add(context_info_array, context_info);
 		current_context_info =
 			(EFI_IA32_X64_PROCESSOR_CONTEXT_INFO *)cur_pos;
 
@@ -466,11 +470,12 @@
 	EFI_IA32_X64_PROCESSOR_CONTEXT_INFO *context_info, void **cur_pos,
 	UINT32 *remaining_size)
 {
+	json_object *context_info_ir = json_object_new_object();
+
 	if (*remaining_size < sizeof(EFI_IA32_X64_PROCESSOR_CONTEXT_INFO)) {
-		return NULL;
+		return context_info_ir;
 	}
 	*remaining_size -= sizeof(EFI_IA32_X64_PROCESSOR_CONTEXT_INFO);
-	json_object *context_info_ir = json_object_new_object();
 
 	//Register context type.
 	json_object *context_type = integer_to_readable_pair(
@@ -494,8 +499,7 @@
 	json_object *register_array = NULL;
 	if (context_info->RegisterType == EFI_REG_CONTEXT_TYPE_IA32) {
 		if (*remaining_size < sizeof(EFI_CONTEXT_IA32_REGISTER_STATE)) {
-			json_object_put(context_info_ir);
-			return NULL;
+			return context_info_ir;
 		}
 		EFI_CONTEXT_IA32_REGISTER_STATE *register_state =
 			(EFI_CONTEXT_IA32_REGISTER_STATE *)(context_info + 1);
@@ -505,8 +509,7 @@
 		*remaining_size -= sizeof(EFI_CONTEXT_IA32_REGISTER_STATE);
 	} else if (context_info->RegisterType == EFI_REG_CONTEXT_TYPE_X64) {
 		if (*remaining_size < sizeof(EFI_CONTEXT_X64_REGISTER_STATE)) {
-			json_object_put(context_info_ir);
-			return NULL;
+			return context_info_ir;
 		}
 		EFI_CONTEXT_X64_REGISTER_STATE *register_state =
 			(EFI_CONTEXT_X64_REGISTER_STATE *)(context_info + 1);
@@ -518,8 +521,7 @@
 		//No parseable data, just dump as base64 and shift the head to the next item.
 		*cur_pos = (void *)(context_info + 1);
 		if (*remaining_size < context_info->ArraySize) {
-			json_object_put(context_info_ir);
-			return NULL;
+			return context_info_ir;
 		}
 		int32_t encoded_len = 0;
 		char *encoded = base64_encode((UINT8 *)*cur_pos,