pldm: libpldmresponder: call ccOnlyResponse when en/decoding errors

Should return ccOnlyResponse when en/decoding process encounters an
error, to avoid redundant data in response.

Signed-off-by: George Liu <liuxiwei@inspur.com>
Change-Id: I8ae02f588740de1ffd6dde99c92046b7cc1d5c39
diff --git a/libpldmresponder/platform.cpp b/libpldmresponder/platform.cpp
index 9c5b688..3f93ff4 100644
--- a/libpldmresponder/platform.cpp
+++ b/libpldmresponder/platform.cpp
@@ -19,9 +19,7 @@
 
     if (payloadLength != PLDM_GET_PDR_REQ_BYTES)
     {
-        encode_get_pdr_resp(request->hdr.instance_id, PLDM_ERROR_INVALID_LENGTH,
-                            0, 0, 0, 0, nullptr, 0, responsePtr);
-        return response;
+        return CmdHandler::ccOnlyResponse(request, PLDM_ERROR_INVALID_LENGTH);
     }
 
     uint32_t recordHandle{};
@@ -30,9 +28,13 @@
     uint16_t reqSizeBytes{};
     uint16_t recordChangeNum{};
 
-    decode_get_pdr_req(request, payloadLength, &recordHandle,
-                       &dataTransferHandle, &transferOpFlag, &reqSizeBytes,
-                       &recordChangeNum);
+    auto rc = decode_get_pdr_req(request, payloadLength, &recordHandle,
+                                 &dataTransferHandle, &transferOpFlag,
+                                 &reqSizeBytes, &recordChangeNum);
+    if (rc != PLDM_SUCCESS)
+    {
+        return CmdHandler::ccOnlyResponse(request, rc);
+    }
 
     uint32_t nextRecordHandle{};
     uint16_t respSizeBytes{};
@@ -56,26 +58,24 @@
                             respSizeBytes,
                         0);
         responsePtr = reinterpret_cast<pldm_msg*>(response.data());
-        encode_get_pdr_resp(request->hdr.instance_id, PLDM_SUCCESS,
-                            nextRecordHandle, 0, PLDM_START, respSizeBytes,
-                            recordData, 0, responsePtr);
+        rc = encode_get_pdr_resp(request->hdr.instance_id, PLDM_SUCCESS,
+                                 nextRecordHandle, 0, PLDM_START, respSizeBytes,
+                                 recordData, 0, responsePtr);
+        if (rc != PLDM_SUCCESS)
+        {
+            return ccOnlyResponse(request, rc);
+        }
     }
     catch (const std::out_of_range& e)
     {
-        encode_get_pdr_resp(request->hdr.instance_id,
-                            PLDM_PLATFORM_INVALID_RECORD_HANDLE,
-                            nextRecordHandle, 0, PLDM_START, respSizeBytes,
-                            recordData, 0, responsePtr);
-        return response;
+        return CmdHandler::ccOnlyResponse(request,
+                                          PLDM_PLATFORM_INVALID_RECORD_HANDLE);
     }
     catch (const std::exception& e)
     {
         std::cerr << "Error accessing PDR, HANDLE=" << recordHandle
                   << " ERROR=" << e.what() << "\n";
-        encode_get_pdr_resp(request->hdr.instance_id, PLDM_ERROR,
-                            nextRecordHandle, 0, PLDM_START, respSizeBytes,
-                            recordData, 0, responsePtr);
-        return response;
+        return CmdHandler::ccOnlyResponse(request, PLDM_ERROR);
     }
     return response;
 }
@@ -96,26 +96,34 @@
         (payloadLength < sizeof(effecterId) + sizeof(compEffecterCnt) +
                              sizeof(set_effecter_state_field)))
     {
-        encode_set_state_effecter_states_resp(
-            request->hdr.instance_id, PLDM_ERROR_INVALID_LENGTH, responsePtr);
-        return response;
+        return CmdHandler::ccOnlyResponse(request, PLDM_ERROR_INVALID_LENGTH);
     }
 
     int rc = decode_set_state_effecter_states_req(request, payloadLength,
                                                   &effecterId, &compEffecterCnt,
                                                   stateField.data());
 
-    if (rc == PLDM_SUCCESS)
+    if (rc != PLDM_SUCCESS)
     {
-        stateField.resize(compEffecterCnt);
-
-        const pldm::utils::DBusHandler dBusIntf;
-        rc = setStateEffecterStatesHandler<pldm::utils::DBusHandler>(
-            dBusIntf, effecterId, stateField);
+        return CmdHandler::ccOnlyResponse(request, rc);
     }
 
-    encode_set_state_effecter_states_resp(request->hdr.instance_id, rc,
-                                          responsePtr);
+    stateField.resize(compEffecterCnt);
+    const pldm::utils::DBusHandler dBusIntf;
+    rc = setStateEffecterStatesHandler<pldm::utils::DBusHandler>(
+        dBusIntf, effecterId, stateField);
+    if (rc != PLDM_SUCCESS)
+    {
+        return CmdHandler::ccOnlyResponse(request, rc);
+    }
+
+    rc = encode_set_state_effecter_states_resp(request->hdr.instance_id, rc,
+                                               responsePtr);
+    if (rc != PLDM_SUCCESS)
+    {
+        return ccOnlyResponse(request, rc);
+    }
+
     return response;
 }