Fix the return value of pack_pldm_header and unpack_pldm_header

- The intent behind this commit is to fix the return value of the
  pack_pldm_header and the unpack_pldm_header methods.

- According to PLDM spec, their return value should be `uint8_t`, not
  `int`, so 0 is PLDM_SUCCESS and non-0 is failure.

- Also, when we call the pack_pldm_header and unpack_pldm_header
  methods, we need to verify the return value of the method.

Tested: Built pldm successfully and Unit Test passes.

Signed-off-by: George Liu <liuxiwei@inspur.com>
Change-Id: I0bd6838c4fb3b90f821c10324e4536ed352ffcfa
diff --git a/libpldm/platform.c b/libpldm/platform.c
index 135a422..812e8b7 100644
--- a/libpldm/platform.c
+++ b/libpldm/platform.c
@@ -141,19 +141,24 @@
 					  uint8_t completion_code,
 					  struct pldm_msg *msg)
 {
+	if (msg == NULL) {
+		return PLDM_ERROR_INVALID_DATA;
+	}
+
 	struct pldm_header_info header = {0};
-	int rc = PLDM_SUCCESS;
-
-	msg->payload[0] = completion_code;
-
 	header.msg_type = PLDM_RESPONSE;
 	header.instance = instance_id;
 	header.pldm_type = PLDM_PLATFORM;
 	header.command = PLDM_SET_STATE_EFFECTER_STATES;
 
-	rc = pack_pldm_header(&header, &(msg->hdr));
+	uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
+	if (rc != PLDM_SUCCESS) {
+		return rc;
+	}
 
-	return rc;
+	msg->payload[0] = completion_code;
+
+	return PLDM_SUCCESS;
 }
 
 int encode_set_state_effecter_states_req(uint8_t instance_id,
@@ -162,16 +167,8 @@
 					 set_effecter_state_field *field,
 					 struct pldm_msg *msg)
 {
-	struct pldm_header_info header = {0};
-	int rc = PLDM_SUCCESS;
-
-	header.msg_type = PLDM_REQUEST;
-	header.instance = instance_id;
-	header.pldm_type = PLDM_PLATFORM;
-	header.command = PLDM_SET_STATE_EFFECTER_STATES;
-
-	if ((rc = pack_pldm_header(&header, &(msg->hdr))) > PLDM_SUCCESS) {
-		return rc;
+	if (msg == NULL) {
+		return PLDM_ERROR_INVALID_DATA;
 	}
 
 	if (comp_effecter_count < 0x1 || comp_effecter_count > 0x8 ||
@@ -179,6 +176,17 @@
 		return PLDM_ERROR_INVALID_DATA;
 	}
 
+	struct pldm_header_info header = {0};
+	header.msg_type = PLDM_REQUEST;
+	header.instance = instance_id;
+	header.pldm_type = PLDM_PLATFORM;
+	header.command = PLDM_SET_STATE_EFFECTER_STATES;
+
+	uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
+	if (rc != PLDM_SUCCESS) {
+		return rc;
+	}
+
 	struct pldm_set_state_effecter_states_req *request =
 	    (struct pldm_set_state_effecter_states_req *)msg->payload;
 	effecter_id = htole16(effecter_id);
@@ -267,25 +275,25 @@
 			uint16_t resp_cnt, const uint8_t *record_data,
 			uint8_t transfer_crc, struct pldm_msg *msg)
 {
-	struct pldm_header_info header = {0};
-	int rc = PLDM_SUCCESS;
-
 	if (msg == NULL) {
 		return PLDM_ERROR_INVALID_DATA;
 	}
-	struct pldm_get_pdr_resp *response =
-	    (struct pldm_get_pdr_resp *)msg->payload;
 
-	response->completion_code = completion_code;
-
+	struct pldm_header_info header = {0};
 	header.msg_type = PLDM_RESPONSE;
 	header.instance = instance_id;
 	header.pldm_type = PLDM_PLATFORM;
 	header.command = PLDM_GET_PDR;
-	if ((rc = pack_pldm_header(&header, &(msg->hdr))) > PLDM_SUCCESS) {
+
+	uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
+	if (rc != PLDM_SUCCESS) {
 		return rc;
 	}
 
+	struct pldm_get_pdr_resp *response =
+	    (struct pldm_get_pdr_resp *)msg->payload;
+	response->completion_code = completion_code;
+
 	if (response->completion_code == PLDM_SUCCESS) {
 		response->next_record_handle = htole32(next_record_hndl);
 		response->next_data_transfer_handle =
@@ -311,28 +319,27 @@
 		       uint16_t request_cnt, uint16_t record_chg_num,
 		       struct pldm_msg *msg, size_t payload_length)
 {
-	struct pldm_header_info header = {0};
-	int rc = PLDM_SUCCESS;
-
 	if (msg == NULL) {
 		return PLDM_ERROR_INVALID_DATA;
 	}
-	struct pldm_get_pdr_req *request =
-	    (struct pldm_get_pdr_req *)msg->payload;
-
-	header.msg_type = PLDM_REQUEST;
-	header.instance = instance_id;
-	header.pldm_type = PLDM_PLATFORM;
-	header.command = PLDM_GET_PDR;
-
-	if ((rc = pack_pldm_header(&header, &(msg->hdr))) > PLDM_SUCCESS) {
-		return rc;
-	}
 
 	if (payload_length != PLDM_GET_PDR_REQ_BYTES) {
 		return PLDM_ERROR_INVALID_LENGTH;
 	}
 
+	struct pldm_header_info header = {0};
+	header.msg_type = PLDM_REQUEST;
+	header.instance = instance_id;
+	header.pldm_type = PLDM_PLATFORM;
+	header.command = PLDM_GET_PDR;
+
+	uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
+	if (rc != PLDM_SUCCESS) {
+		return rc;
+	}
+
+	struct pldm_get_pdr_req *request =
+	    (struct pldm_get_pdr_req *)msg->payload;
 	request->record_handle = htole32(record_hndl);
 	request->data_transfer_handle = htole32(data_transfer_hndl);
 	request->transfer_op_flag = transfer_op_flag;
@@ -467,9 +474,6 @@
 					   struct pldm_msg *msg,
 					   size_t payload_length)
 {
-	struct pldm_header_info header = {0};
-	int rc = PLDM_SUCCESS;
-
 	if (msg == NULL) {
 		return PLDM_ERROR_INVALID_DATA;
 	}
@@ -478,14 +482,18 @@
 		return PLDM_ERROR_INVALID_LENGTH;
 	}
 
-	msg->payload[0] = completion_code;
-
+	struct pldm_header_info header = {0};
 	header.msg_type = PLDM_RESPONSE;
 	header.instance = instance_id;
 	header.pldm_type = PLDM_PLATFORM;
 	header.command = PLDM_SET_NUMERIC_EFFECTER_VALUE;
 
-	rc = pack_pldm_header(&header, &(msg->hdr));
+	uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
+	if (rc != PLDM_SUCCESS) {
+		return rc;
+	}
+
+	msg->payload[0] = completion_code;
 
 	return rc;
 }
@@ -494,28 +502,27 @@
     uint8_t instance_id, uint16_t effecter_id, uint8_t effecter_data_size,
     uint8_t *effecter_value, struct pldm_msg *msg, size_t payload_length)
 {
-	struct pldm_header_info header = {0};
-	int rc = PLDM_SUCCESS;
-
 	if (msg == NULL || effecter_value == NULL) {
 		return PLDM_ERROR_INVALID_DATA;
 	}
-	struct pldm_set_numeric_effecter_value_req *request =
-	    (struct pldm_set_numeric_effecter_value_req *)msg->payload;
-
-	header.msg_type = PLDM_REQUEST;
-	header.instance = instance_id;
-	header.pldm_type = PLDM_PLATFORM;
-	header.command = PLDM_SET_NUMERIC_EFFECTER_VALUE;
-
-	if ((rc = pack_pldm_header(&header, &(msg->hdr))) > PLDM_SUCCESS) {
-		return rc;
-	}
 
 	if (effecter_data_size > PLDM_EFFECTER_DATA_SIZE_SINT32) {
 		return PLDM_ERROR_INVALID_DATA;
 	}
 
+	struct pldm_header_info header = {0};
+	header.msg_type = PLDM_REQUEST;
+	header.instance = instance_id;
+	header.pldm_type = PLDM_PLATFORM;
+	header.command = PLDM_SET_NUMERIC_EFFECTER_VALUE;
+
+	uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
+	if (rc != PLDM_SUCCESS) {
+		return rc;
+	}
+
+	struct pldm_set_numeric_effecter_value_req *request =
+	    (struct pldm_set_numeric_effecter_value_req *)msg->payload;
 	if (effecter_data_size == PLDM_EFFECTER_DATA_SIZE_UINT8 ||
 	    effecter_data_size == PLDM_EFFECTER_DATA_SIZE_SINT8) {
 		if (payload_length !=
@@ -575,22 +582,25 @@
 					  get_sensor_state_field *field,
 					  struct pldm_msg *msg)
 {
-	struct pldm_header_info header = {0};
-	int rc = PLDM_SUCCESS;
+	if (msg == NULL) {
+		return PLDM_ERROR_INVALID_DATA;
+	}
 
+	if (comp_sensor_count < 0x1 || comp_sensor_count > 0x8) {
+		return PLDM_ERROR_INVALID_DATA;
+	}
+
+	struct pldm_header_info header = {0};
 	header.msg_type = PLDM_RESPONSE;
 	header.instance = instance_id;
 	header.pldm_type = PLDM_PLATFORM;
 	header.command = PLDM_GET_STATE_SENSOR_READINGS;
 
-	if ((rc = pack_pldm_header(&header, &(msg->hdr))) > PLDM_SUCCESS) {
+	uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
+	if (rc != PLDM_SUCCESS) {
 		return rc;
 	}
 
-	if (comp_sensor_count < 0x1 || comp_sensor_count > 0x8) {
-		return PLDM_ERROR_INVALID_DATA;
-	}
-
 	struct pldm_get_state_sensor_readings_resp *response =
 	    (struct pldm_get_state_sensor_readings_resp *)msg->payload;
 
@@ -599,7 +609,7 @@
 	memcpy(response->field, field,
 	       (sizeof(get_sensor_state_field) * comp_sensor_count));
 
-	return rc;
+	return PLDM_SUCCESS;
 }
 
 int encode_get_state_sensor_readings_req(uint8_t instance_id,
@@ -607,19 +617,18 @@
 					 bitfield8_t sensor_rearm,
 					 uint8_t reserved, struct pldm_msg *msg)
 {
-	struct pldm_header_info header = {0};
-	int rc = PLDM_SUCCESS;
+	if (msg == NULL) {
+		return PLDM_ERROR_INVALID_DATA;
+	}
 
+	struct pldm_header_info header = {0};
 	header.msg_type = PLDM_REQUEST;
 	header.instance = instance_id;
 	header.pldm_type = PLDM_PLATFORM;
 	header.command = PLDM_GET_STATE_SENSOR_READINGS;
 
-	if (msg == NULL) {
-		return PLDM_ERROR_INVALID_DATA;
-	}
-
-	if ((rc = pack_pldm_header(&header, &(msg->hdr))) > PLDM_SUCCESS) {
+	uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
+	if (rc != PLDM_SUCCESS) {
 		return rc;
 	}
 
@@ -762,8 +771,6 @@
 				       uint8_t platform_event_status,
 				       struct pldm_msg *msg)
 {
-	int rc = PLDM_SUCCESS;
-
 	if (msg == NULL) {
 		return PLDM_ERROR_INVALID_DATA;
 	}
@@ -772,20 +779,22 @@
 		return PLDM_ERROR_INVALID_DATA;
 	}
 
-	struct pldm_platform_event_message_resp *response =
-	    (struct pldm_platform_event_message_resp *)msg->payload;
-	response->completion_code = completion_code;
-	response->platform_event_status = platform_event_status;
-
 	struct pldm_header_info header = {0};
 	header.msg_type = PLDM_RESPONSE;
 	header.instance = instance_id;
 	header.pldm_type = PLDM_PLATFORM;
 	header.command = PLDM_PLATFORM_EVENT_MESSAGE;
 
-	if ((rc = pack_pldm_header(&header, &(msg->hdr))) > PLDM_SUCCESS) {
+	uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
+	if (rc != PLDM_SUCCESS) {
 		return rc;
 	}
+
+	struct pldm_platform_event_message_resp *response =
+	    (struct pldm_platform_event_message_resp *)msg->payload;
+	response->completion_code = completion_code;
+	response->platform_event_status = platform_event_status;
+
 	return PLDM_SUCCESS;
 }
 
@@ -795,14 +804,6 @@
     struct pldm_msg *msg, size_t payload_length)
 
 {
-	struct pldm_header_info header = {0};
-	int rc = PLDM_SUCCESS;
-
-	header.msg_type = PLDM_REQUEST;
-	header.instance = instance_id;
-	header.pldm_type = PLDM_PLATFORM;
-	header.command = PLDM_PLATFORM_EVENT_MESSAGE;
-
 	if (format_version != 1) {
 		return PLDM_ERROR_INVALID_DATA;
 	}
@@ -825,7 +826,14 @@
 		return PLDM_ERROR_INVALID_DATA;
 	}
 
-	if ((rc = pack_pldm_header(&header, &(msg->hdr))) > PLDM_SUCCESS) {
+	struct pldm_header_info header = {0};
+	header.msg_type = PLDM_REQUEST;
+	header.instance = instance_id;
+	header.pldm_type = PLDM_PLATFORM;
+	header.command = PLDM_PLATFORM_EVENT_MESSAGE;
+
+	uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
+	if (rc != PLDM_SUCCESS) {
 		return rc;
 	}
 
@@ -1019,25 +1027,23 @@
 					  uint16_t effecter_id,
 					  struct pldm_msg *msg)
 {
-	struct pldm_header_info header = {0};
-	int rc = PLDM_SUCCESS;
-
 	if (msg == NULL) {
 		return PLDM_ERROR_INVALID_DATA;
 	}
 
-	struct pldm_get_numeric_effecter_value_req *request =
-	    (struct pldm_get_numeric_effecter_value_req *)msg->payload;
-
+	struct pldm_header_info header = {0};
 	header.msg_type = PLDM_REQUEST;
 	header.instance = instance_id;
 	header.pldm_type = PLDM_PLATFORM;
 	header.command = PLDM_GET_NUMERIC_EFFECTER_VALUE;
 
-	if ((rc = pack_pldm_header(&header, &(msg->hdr))) > PLDM_SUCCESS) {
+	uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
+	if (rc != PLDM_SUCCESS) {
 		return rc;
 	}
 
+	struct pldm_get_numeric_effecter_value_req *request =
+	    (struct pldm_get_numeric_effecter_value_req *)msg->payload;
 	request->effecter_id = htole16(effecter_id);
 
 	return PLDM_SUCCESS;
@@ -1048,9 +1054,6 @@
     uint8_t effecter_oper_state, uint8_t *pending_value, uint8_t *present_value,
     struct pldm_msg *msg, size_t payload_length)
 {
-	struct pldm_header_info header = {0};
-	int rc = PLDM_SUCCESS;
-
 	if (msg == NULL || pending_value == NULL || present_value == NULL) {
 		return PLDM_ERROR_INVALID_DATA;
 	}
@@ -1063,12 +1066,14 @@
 		return PLDM_ERROR_INVALID_DATA;
 	}
 
+	struct pldm_header_info header = {0};
 	header.msg_type = PLDM_RESPONSE;
 	header.instance = instance_id;
 	header.pldm_type = PLDM_PLATFORM;
 	header.command = PLDM_GET_NUMERIC_EFFECTER_VALUE;
 
-	if ((rc = pack_pldm_header(&header, &(msg->hdr))) > PLDM_SUCCESS) {
+	uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
+	if (rc != PLDM_SUCCESS) {
 		return rc;
 	}
 
@@ -1348,19 +1353,18 @@
 				  uint8_t rearm_event_state,
 				  struct pldm_msg *msg)
 {
-	struct pldm_header_info header = {0};
-	int rc = PLDM_SUCCESS;
+	if (msg == NULL) {
+		return PLDM_ERROR_INVALID_DATA;
+	}
 
+	struct pldm_header_info header = {0};
 	header.msg_type = PLDM_REQUEST;
 	header.instance = instance_id;
 	header.pldm_type = PLDM_PLATFORM;
 	header.command = PLDM_GET_SENSOR_READING;
 
-	if (msg == NULL) {
-		return PLDM_ERROR_INVALID_DATA;
-	}
-
-	if ((rc = pack_pldm_header(&header, &(msg->hdr))) > PLDM_SUCCESS) {
+	uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
+	if (rc != PLDM_SUCCESS) {
 		return rc;
 	}
 
@@ -1450,9 +1454,6 @@
     uint8_t present_state, uint8_t previous_state, uint8_t event_state,
     uint8_t *present_reading, struct pldm_msg *msg, size_t payload_length)
 {
-	struct pldm_header_info header = {0};
-	int rc = PLDM_SUCCESS;
-
 	if (msg == NULL || present_reading == NULL) {
 		return PLDM_ERROR_INVALID_DATA;
 	}
@@ -1461,12 +1462,14 @@
 		return PLDM_ERROR_INVALID_DATA;
 	}
 
+	struct pldm_header_info header = {0};
 	header.msg_type = PLDM_RESPONSE;
 	header.instance = instance_id;
 	header.pldm_type = PLDM_PLATFORM;
 	header.command = PLDM_GET_SENSOR_READING;
 
-	if ((rc = pack_pldm_header(&header, &(msg->hdr))) > PLDM_SUCCESS) {
+	uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
+	if (rc != PLDM_SUCCESS) {
 		return rc;
 	}
 
@@ -1540,18 +1543,22 @@
 				  uint16_t heartbeat_timer,
 				  struct pldm_msg *msg)
 {
-	struct pldm_header_info header = {0};
-	int rc = PLDM_SUCCESS;
 	if (msg == NULL) {
 		return PLDM_ERROR_INVALID_DATA;
 	}
 
+	if (transport_protocol_type != PLDM_TRANSPORT_PROTOCOL_TYPE_MCTP) {
+		return PLDM_ERROR_INVALID_DATA;
+	}
+
+	struct pldm_header_info header = {0};
 	header.msg_type = PLDM_REQUEST;
 	header.instance = instance_id;
 	header.pldm_type = PLDM_PLATFORM;
 	header.command = PLDM_SET_EVENT_RECEIVER;
 
-	if ((rc = pack_pldm_header(&header, &(msg->hdr))) > PLDM_SUCCESS) {
+	uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
+	if (rc != PLDM_SUCCESS) {
 		return rc;
 	}
 
@@ -1559,9 +1566,6 @@
 	    (struct pldm_set_event_receiver_req *)msg->payload;
 	request->event_message_global_enable = event_message_global_enable;
 
-	if (transport_protocol_type != PLDM_TRANSPORT_PROTOCOL_TYPE_MCTP) {
-		return PLDM_ERROR_INVALID_DATA;
-	}
 	request->transport_protocol_type = transport_protocol_type;
 	request->event_receiver_address_info = event_receiver_address_info;
 
@@ -1635,21 +1639,22 @@
 				   struct pldm_msg *msg)
 
 {
-	struct pldm_header_info header = {0};
-	int rc = PLDM_SUCCESS;
-
 	if (msg == NULL) {
 		return PLDM_ERROR_INVALID_DATA;
 	}
 
-	msg->payload[0] = completion_code;
-
+	struct pldm_header_info header = {0};
 	header.instance = instance_id;
 	header.msg_type = PLDM_RESPONSE;
 	header.pldm_type = PLDM_PLATFORM;
 	header.command = PLDM_SET_EVENT_RECEIVER;
 
-	rc = pack_pldm_header(&header, &(msg->hdr));
+	uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
+	if (rc != PLDM_SUCCESS) {
+		return rc;
+	}
 
-	return rc;
+	msg->payload[0] = completion_code;
+
+	return PLDM_SUCCESS;
 }