tests: 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: Ifab9ea58b932db11d7af0b9def119bed1bfdc44d
Signed-off-by: Pavithra Barithaya <pavithrabarithaya07@gmail.com>
diff --git a/requester/test/handler_test.cpp b/requester/test/handler_test.cpp
index 86e2717..17560c5 100644
--- a/requester/test/handler_test.cpp
+++ b/requester/test/handler_test.cpp
@@ -170,7 +170,7 @@
size_t responseLen;
int rc = PLDM_SUCCESS;
- auto requestPtr = reinterpret_cast<pldm_msg*>(request.data());
+ auto requestPtr = new (request.data()) pldm_msg;
requestPtr->hdr.instance_id = instanceId;
try
@@ -216,7 +216,7 @@
pldm::Request request(sizeof(pldm_msg_hdr) + sizeof(uint8_t), 0);
pldm::Response response;
- auto requestPtr = reinterpret_cast<pldm_msg*>(request.data());
+ auto requestPtr = new (request.data()) pldm_msg;
requestPtr->hdr.instance_id = instanceId;
co_await reqHandler.sendRecvMsg(eid, std::move(request));
@@ -241,7 +241,7 @@
uint8_t instanceId, uint8_t& tid)
{
pldm::Request request(sizeof(pldm_msg_hdr), 0);
- auto requestMsg = reinterpret_cast<pldm_msg*>(request.data());
+ auto requestMsg = new (request.data()) pldm_msg;
const pldm_msg* responseMsg;
size_t responseLen;
@@ -281,7 +281,7 @@
pldm::Response mockResponse(sizeof(pldm_msg_hdr) + PLDM_GET_TID_RESP_BYTES,
0);
- auto mockResponseMsg = reinterpret_cast<pldm_msg*>(mockResponse.data());
+ auto mockResponseMsg = new (mockResponse.data()) pldm_msg;
// Compose response message of getTID command
encode_get_tid_resp(instanceId, PLDM_SUCCESS, expectedTid, mockResponseMsg);