libpldm: fixup encode GetPDR response

CRC must be encoded only if the transfer flag is PLDM_END. The previous
code would do an out if bounds access because the caller wouldn't
allocate memory to hold the CRC (because the caller intended a
single-part transfer).

Signed-off-by: Deepak Kodihalli <dkodihal@in.ibm.com>
Change-Id: I6698b2542b57857ba40bd5945b61bf9af419ec8e
diff --git a/libpldm/platform.c b/libpldm/platform.c
index fc9cc56..3cbc137 100644
--- a/libpldm/platform.c
+++ b/libpldm/platform.c
@@ -157,9 +157,12 @@
 		if (record_data != NULL && resp_cnt > 0) {
 			memcpy(response->record_data, record_data, resp_cnt);
 		}
-		uint8_t *dst = msg->payload;
-		dst += (sizeof(struct pldm_get_pdr_resp) - 1) + resp_cnt;
-		*dst = transfer_crc;
+		if (transfer_flag == PLDM_END) {
+			uint8_t *dst = msg->payload;
+			dst +=
+			    (sizeof(struct pldm_get_pdr_resp) - 1) + resp_cnt;
+			*dst = transfer_crc;
+		}
 	}
 
 	return PLDM_SUCCESS;
diff --git a/test/libpldm_platform_test.cpp b/test/libpldm_platform_test.cpp
index 154bb59..425dc45 100644
--- a/test/libpldm_platform_test.cpp
+++ b/test/libpldm_platform_test.cpp
@@ -144,10 +144,10 @@
     uint8_t completionCode = 0;
     uint32_t nextRecordHndl = 0x12;
     uint32_t nextDataTransferHndl = 0x13;
-    uint8_t transferFlag = PLDM_START_AND_END;
+    uint8_t transferFlag = PLDM_END;
     uint16_t respCnt = 0x5;
     std::vector<uint8_t> recordData{1, 2, 3, 4, 5};
-    uint8_t transferCRC = 0;
+    uint8_t transferCRC = 6;
 
     // + size of record data and transfer CRC
     std::vector<uint8_t> responseMsg(hdrSize + PLDM_GET_PDR_MIN_RESP_BYTES +
@@ -169,6 +169,16 @@
     ASSERT_EQ(respCnt, resp->response_count);
     ASSERT_EQ(0,
               memcmp(recordData.data(), resp->record_data, recordData.size()));
+    ASSERT_EQ(*(response->payload + sizeof(pldm_get_pdr_resp) - 1 +
+                recordData.size()),
+              transferCRC);
+
+    transferFlag = PLDM_START_AND_END; // No CRC in this case
+    responseMsg.resize(responseMsg.size() - sizeof(transferCRC));
+    rc = encode_get_pdr_resp(0, PLDM_SUCCESS, nextRecordHndl,
+                             nextDataTransferHndl, transferFlag, respCnt,
+                             recordData.data(), transferCRC, response);
+    ASSERT_EQ(rc, PLDM_SUCCESS);
 }
 
 TEST(GetPDR, testBadEncodeResponse)