host-bmc: 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: If8dcc8ebb592692110a7c96485021e0df660f576
Signed-off-by: Pavithra Barithaya <pavithrabarithaya07@gmail.com>
diff --git a/host-bmc/host_pdr_handler.cpp b/host-bmc/host_pdr_handler.cpp
index 8da01a7..052e7e2 100644
--- a/host-bmc/host_pdr_handler.cpp
+++ b/host-bmc/host_pdr_handler.cpp
@@ -172,7 +172,7 @@
std::vector<uint8_t> requestMsg(
sizeof(pldm_msg_hdr) + PLDM_GET_PDR_REQ_BYTES);
- auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
+ auto request = new (requestMsg.data()) pldm_msg;
uint32_t recordHandle{};
if (!nextRecordHandle && (!modifiedPDRRecordHandles.empty()) &&
isHostPdrModified)
@@ -234,8 +234,8 @@
size_t numEntities{};
pldm_entity* entities = nullptr;
bool merged = false;
- auto entityPdr = reinterpret_cast<pldm_pdr_entity_association*>(
- const_cast<uint8_t*>(pdr.data()) + sizeof(pldm_pdr_hdr));
+ auto entityPdr = new (const_cast<uint8_t*>(pdr.data()) +
+ sizeof(pldm_pdr_hdr)) pldm_pdr_entity_association;
if (oemPlatformHandler &&
oemPlatformHandler->checkRecordHandleInRange(record_handle))
@@ -372,9 +372,8 @@
changeEntries[0].size() * sizeof(uint32_t);
std::vector<uint8_t> eventDataVec{};
eventDataVec.resize(maxSize);
- auto eventData =
- reinterpret_cast<struct pldm_pdr_repository_chg_event_data*>(
- eventDataVec.data());
+ auto eventData = new (eventDataVec.data())
+ pldm_pdr_repository_chg_event_data;
size_t actualSize{};
auto firstEntry = changeEntries[0].data();
auto rc = encode_pldm_pdr_repository_chg_event_data(
@@ -391,7 +390,7 @@
std::vector<uint8_t> requestMsg(
sizeof(pldm_msg_hdr) + PLDM_PLATFORM_EVENT_MESSAGE_MIN_REQ_BYTES +
actualSize);
- auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
+ auto request = new (requestMsg.data()) pldm_msg;
rc = encode_platform_event_message_req(
instanceId, 1, TERMINUS_ID, PLDM_PDR_REPOSITORY_CHG_EVENT,
eventDataVec.data(), actualSize, request,
@@ -531,7 +530,7 @@
rh = nextRecordHandle - 1;
}
- auto pdrHdr = reinterpret_cast<pldm_pdr_hdr*>(pdr.data());
+ auto pdrHdr = new (pdr.data()) pldm_pdr_hdr;
if (!rh)
{
rh = pdrHdr->record_handle;
@@ -717,7 +716,7 @@
auto instanceId = instanceIdDb.next(mctp_eid);
std::vector<uint8_t> requestMsg(
sizeof(pldm_msg_hdr) + PLDM_GET_VERSION_REQ_BYTES);
- auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
+ auto request = new (requestMsg.data()) pldm_msg;
auto rc = encode_get_version_req(instanceId, 0, PLDM_GET_FIRSTPART,
PLDM_BASE, request);
if (rc != PLDM_SUCCESS)
@@ -790,7 +789,7 @@
std::vector<uint8_t> requestMsg(
sizeof(pldm_msg_hdr) +
PLDM_GET_STATE_SENSOR_READINGS_REQ_BYTES);
- auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
+ auto request = new (requestMsg.data()) pldm_msg;
auto rc = encode_get_state_sensor_readings_req(
instanceId, sensorId, sensorRearm, 0, request);
@@ -933,7 +932,7 @@
sizeof(pldm_msg_hdr) + PLDM_GET_FRU_RECORD_TABLE_METADATA_REQ_BYTES);
// GetFruRecordTableMetadata
- auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
+ auto request = new (requestMsg.data()) pldm_msg;
auto rc = encode_get_fru_record_table_metadata_req(
instanceId, request, requestMsg.size() - sizeof(pldm_msg_hdr));
if (rc != PLDM_SUCCESS)
@@ -1011,7 +1010,7 @@
sizeof(pldm_msg_hdr) + PLDM_GET_FRU_RECORD_TABLE_REQ_BYTES);
// send the getFruRecordTable command
- auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
+ auto request = new (requestMsg.data()) pldm_msg;
auto rc = encode_get_fru_record_table_req(
instanceId, 0, PLDM_GET_FIRSTPART, request,
requestMsg.size() - sizeof(pldm_msg_hdr));