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/dbus_to_event_handler.cpp b/host-bmc/dbus_to_event_handler.cpp
index 3eef16a..6149050 100644
--- a/host-bmc/dbus_to_event_handler.cpp
+++ b/host-bmc/dbus_to_event_handler.cpp
@@ -31,7 +31,7 @@
std::vector<uint8_t> requestMsg(
sizeof(pldm_msg_hdr) + PLDM_PLATFORM_EVENT_MESSAGE_MIN_REQ_BYTES +
eventDataVec.size());
- auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
+ auto request = new (requestMsg.data()) pldm_msg;
auto rc = encode_platform_event_message_req(
instanceId, 1 /*formatVersion*/, TERMINUS_ID /*tId*/, eventType,
@@ -97,8 +97,7 @@
{
std::vector<uint8_t> sensorEventDataVec{};
sensorEventDataVec.resize(sensorEventSize);
- auto eventData = reinterpret_cast<struct pldm_sensor_event_data*>(
- sensorEventDataVec.data());
+ auto eventData = new (sensorEventDataVec.data()) pldm_sensor_event_data;
eventData->sensor_id = sensorId;
eventData->sensor_event_class_type = PLDM_STATE_SENSOR_STATE;
eventData->event_class[0] = static_cast<uint8_t>(offset);
@@ -150,9 +149,8 @@
if (findValue)
{
- auto eventData =
- reinterpret_cast<struct pldm_sensor_event_data*>(
- sensorEventDataVec.data());
+ auto eventData = new (sensorEventDataVec.data())
+ pldm_sensor_event_data;
eventData->event_class[1] = itr.first;
if (sensorCacheMap.contains(sensorId) &&
sensorCacheMap[sensorId][offset] !=
@@ -206,7 +204,7 @@
auto pdrRecord = sensorPDRs.getFirstRecord(pdrEntry);
while (pdrRecord)
{
- pdr = reinterpret_cast<pldm_state_sensor_pdr*>(pdrEntry.data);
+ pdr = new (pdrEntry.data) pldm_state_sensor_pdr;
SensorId sensorId = LE16TOH(pdr->sensor_id);
if (sensorHandlers.contains(pdrType))
{