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/platform_manager.cpp b/platform-mc/platform_manager.cpp
index 4219b7d..1e35477 100644
--- a/platform-mc/platform_manager.cpp
+++ b/platform-mc/platform_manager.cpp
@@ -296,7 +296,7 @@
{
// multipart transfer
uint32_t receivedRecordSize = responseCnt;
- auto pdrHdr = reinterpret_cast<pldm_pdr_hdr*>(recvBuf.data());
+ auto pdrHdr = new (recvBuf.data()) pldm_pdr_hdr;
uint16_t recordChgNum = le16toh(pdrHdr->record_change_num);
std::vector<uint8_t> receivedPdr(recvBuf.begin(),
recvBuf.begin() + responseCnt);
@@ -343,7 +343,7 @@
std::vector<uint8_t>& recordData, uint8_t& transferCrc)
{
Request request(sizeof(pldm_msg_hdr) + PLDM_GET_PDR_REQ_BYTES);
- auto requestMsg = reinterpret_cast<pldm_msg*>(request.data());
+ auto requestMsg = new (request.data()) pldm_msg;
auto rc = encode_get_pdr_req(0, recordHndl, dataTransferHndl,
transferOpFlag, requestCnt, recordChgNum,
requestMsg, PLDM_GET_PDR_REQ_BYTES);
@@ -395,7 +395,7 @@
uint32_t& repositorySize, uint32_t& largestRecordSize)
{
Request request(sizeof(pldm_msg_hdr) + sizeof(uint8_t));
- auto requestMsg = reinterpret_cast<pldm_msg*>(request.data());
+ auto requestMsg = new (request.data()) pldm_msg;
auto rc = encode_pldm_header_only(PLDM_REQUEST, 0, PLDM_PLATFORM,
PLDM_GET_PDR_REPOSITORY_INFO, requestMsg);
if (rc)
@@ -452,7 +452,7 @@
{
Request request(
sizeof(pldm_msg_hdr) + PLDM_EVENT_MESSAGE_BUFFER_SIZE_REQ_BYTES);
- auto requestMsg = reinterpret_cast<pldm_msg*>(request.data());
+ auto requestMsg = new (request.data()) pldm_msg;
auto rc = encode_event_message_buffer_size_req(0, receiverMaxBufferSize,
requestMsg);
if (rc)
@@ -512,7 +512,7 @@
requestBytes = requestBytes - sizeof(heartbeatTimer);
}
Request request(sizeof(pldm_msg_hdr) + requestBytes);
- auto requestMsg = reinterpret_cast<pldm_msg*>(request.data());
+ auto requestMsg = new (request.data()) pldm_msg;
auto rc = encode_set_event_receiver_req(
0, eventMessageGlobalEnable, protocolType,
terminusManager.getLocalEid(), heartbeatTimer, requestMsg);
@@ -565,7 +565,7 @@
{
Request request(
sizeof(pldm_msg_hdr) + PLDM_EVENT_MESSAGE_SUPPORTED_REQ_BYTES);
- auto requestMsg = reinterpret_cast<pldm_msg*>(request.data());
+ auto requestMsg = new (request.data()) pldm_msg;
auto rc = encode_event_message_supported_req(0, formatVersion, requestMsg);
if (rc)
{
@@ -620,7 +620,7 @@
{
Request request(
sizeof(pldm_msg_hdr) + PLDM_GET_FRU_RECORD_TABLE_METADATA_REQ_BYTES);
- auto requestMsg = reinterpret_cast<pldm_msg*>(request.data());
+ auto requestMsg = new (request.data()) pldm_msg;
auto rc = encode_get_fru_record_table_metadata_req(
0, requestMsg, PLDM_GET_FRU_RECORD_TABLE_METADATA_REQ_BYTES);
@@ -689,7 +689,7 @@
std::vector<uint8_t>& recordData)
{
Request request(sizeof(pldm_msg_hdr) + PLDM_GET_FRU_RECORD_TABLE_REQ_BYTES);
- auto requestMsg = reinterpret_cast<pldm_msg*>(request.data());
+ auto requestMsg = new (request.data()) pldm_msg;
auto rc = encode_get_fru_record_table_req(
0, dataTransferHndl, transferOpFlag, requestMsg,