platform-mc : 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: I006bb4a5de9f7d0cef4dbc7507f63e1d6798ddfe
Signed-off-by: Pavithra Barithaya <pavithrabarithaya07@gmail.com>
diff --git a/platform-mc/terminus_manager.cpp b/platform-mc/terminus_manager.cpp
index c05d89e..1086292 100644
--- a/platform-mc/terminus_manager.cpp
+++ b/platform-mc/terminus_manager.cpp
@@ -444,7 +444,7 @@
{
auto instanceId = instanceIdDb.next(eid);
Request request(sizeof(pldm_msg_hdr));
- auto requestMsg = reinterpret_cast<pldm_msg*>(request.data());
+ auto requestMsg = new (request.data()) pldm_msg;
auto rc = encode_get_tid_req(instanceId, requestMsg);
if (rc)
{
@@ -490,7 +490,7 @@
{
auto instanceId = instanceIdDb.next(eid);
Request request(sizeof(pldm_msg_hdr) + sizeof(pldm_set_tid_req));
- auto requestMsg = reinterpret_cast<pldm_msg*>(request.data());
+ auto requestMsg = new (request.data()) pldm_msg;
auto rc = encode_set_tid_req(instanceId, tid, requestMsg);
if (rc)
{
@@ -527,7 +527,7 @@
uint64_t& supportedTypes)
{
Request request(sizeof(pldm_msg_hdr));
- auto requestMsg = reinterpret_cast<pldm_msg*>(request.data());
+ auto requestMsg = new (request.data()) pldm_msg;
auto rc = encode_get_types_req(0, requestMsg);
if (rc)
{
@@ -574,7 +574,7 @@
pldm_tid_t tid, uint8_t type, ver32_t version, bitfield8_t* supportedCmds)
{
Request request(sizeof(pldm_msg_hdr) + PLDM_GET_COMMANDS_REQ_BYTES);
- auto requestMsg = reinterpret_cast<pldm_msg*>(request.data());
+ auto requestMsg = new (request.data()) pldm_msg;
auto rc = encode_get_commands_req(0, type, version, requestMsg);
if (rc)
@@ -661,7 +661,7 @@
}
auto eid = std::get<0>(mctpInfo.value());
- auto requestMsg = reinterpret_cast<pldm_msg*>(request.data());
+ auto requestMsg = new (request.data()) pldm_msg;
requestMsg->hdr.instance_id = instanceIdDb.next(eid);
auto rc = co_await sendRecvPldmMsgOverMctp(eid, request, responseMsg,
responseLen);