pldmd : 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: Ibb10a86bbd2f1044b79b965dfc865b6bf19075fc
Signed-off-by: Pavithra Barithaya <pavithrabarithaya07@gmail.com>
diff --git a/pldmd/handler.hpp b/pldmd/handler.hpp
index b677e47..89b8a7f 100644
--- a/pldmd/handler.hpp
+++ b/pldmd/handler.hpp
@@ -48,7 +48,7 @@
static Response ccOnlyResponse(const pldm_msg* request, uint8_t cc)
{
Response response(sizeof(pldm_msg), 0);
- auto ptr = reinterpret_cast<pldm_msg*>(response.data());
+ auto ptr = new (response.data()) pldm_msg;
auto rc =
encode_cc_only_resp(request->hdr.instance_id, request->hdr.type,
request->hdr.command, cc, ptr);
diff --git a/pldmd/pldmd.cpp b/pldmd/pldmd.cpp
index 202f794..7a62584 100644
--- a/pldmd/pldmd.cpp
+++ b/pldmd/pldmd.cpp
@@ -138,7 +138,7 @@
{
uint8_t completion_code = PLDM_ERROR_UNSUPPORTED_PLDM_CMD;
response.resize(sizeof(pldm_msg_hdr));
- auto responseHdr = reinterpret_cast<pldm_msg_hdr*>(response.data());
+ auto responseHdr = new (response.data()) pldm_msg_hdr;
pldm_header_info header{};
header.msg_type = PLDM_RESPONSE;
header.instance = hdrFields.instance;