utilities : Migrate to placement_new from reinterpret casting
reinterpret_cast is prohibited by the C++ core guidelines because
it takes the behavior outside the language definition and gives
problems with type safety. Placement-new on the other-hand allows
to control the object storage while still properly instantiating
an object,keeping the behavior inside the C++ language
specification.
Change-Id: I58dd021d2947e72919d33778e9e7832a50e79bae
Signed-off-by: Pavithra Barithaya <pavithrabarithaya07@gmail.com>
diff --git a/utilities/requester/set_state_effecter.cpp b/utilities/requester/set_state_effecter.cpp
index 3774054..e4a3385 100644
--- a/utilities/requester/set_state_effecter.cpp
+++ b/utilities/requester/set_state_effecter.cpp
@@ -26,7 +26,7 @@
sizeof(pldm_msg_hdr) + sizeof(effecterId) +
sizeof(effecterCount) + sizeof(set_effecter_state_field)>
requestMsg{};
- auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
+ auto request = new (requestMsg.data()) pldm_msg;
set_effecter_state_field stateField{PLDM_REQUEST_SET, state};
auto rc = encode_set_state_effecter_states_req(0, effecterId, effecterCount,
&stateField, request);
@@ -53,7 +53,7 @@
"RC", rc, "ERROR", errno);
return -1;
}
- pldm_msg* response = reinterpret_cast<pldm_msg*>(responseMsg);
+ pldm_msg* response = new (responseMsg) pldm_msg;
info(
"Done! Got the response for PLDM send receive message request, response code '{RC}'",
"RC", lg2::hex, response->payload[0]);
diff --git a/utilities/requester/set_state_effecter_async.cpp b/utilities/requester/set_state_effecter_async.cpp
index 6420328..0a1f2e8 100644
--- a/utilities/requester/set_state_effecter_async.cpp
+++ b/utilities/requester/set_state_effecter_async.cpp
@@ -33,7 +33,7 @@
sizeof(pldm_msg_hdr) + sizeof(effecterId) +
sizeof(effecterCount) + sizeof(set_effecter_state_field)>
requestMsg{};
- auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
+ auto request = new (requestMsg.data()) pldm_msg;
set_effecter_state_field stateField{PLDM_REQUEST_SET, state};
auto rc = encode_set_state_effecter_states_req(0, effecterId, effecterCount,
&stateField, request);
@@ -65,7 +65,7 @@
size_t responseMsgSize{};
pldm_tid_t srcTid;
auto rc = pldmTransport.recvMsg(srcTid, responseMsg, responseMsgSize);
- pldm_msg* response = reinterpret_cast<pldm_msg*>(responseMsg);
+ pldm_msg* response = new (responseMsg) pldm_msg;
if (rc || dstTid != srcTid ||
!pldm_msg_hdr_correlate_response(&request->hdr, &response->hdr))
{