tests: 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: Ifab9ea58b932db11d7af0b9def119bed1bfdc44d
Signed-off-by: Pavithra Barithaya <pavithrabarithaya07@gmail.com>
diff --git a/libpldmresponder/test/libpldmresponder_pdr_sensor_test.cpp b/libpldmresponder/test/libpldmresponder_pdr_sensor_test.cpp
index ae1ed3a..f8e0a05 100644
--- a/libpldmresponder/test/libpldmresponder_pdr_sensor_test.cpp
+++ b/libpldmresponder/test/libpldmresponder_pdr_sensor_test.cpp
@@ -22,7 +22,7 @@
{
std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_GET_PDR_REQ_BYTES>
requestPayload{};
- auto req = reinterpret_cast<pldm_msg*>(requestPayload.data());
+ auto req = new (requestPayload.data()) pldm_msg;
size_t requestPayloadLength = requestPayload.size() - sizeof(pldm_msg_hdr);
MockdBusHandler mockedUtils;
@@ -49,8 +49,7 @@
auto record = pdr::getRecordByHandle(outRepo, 2, e);
ASSERT_NE(record, nullptr);
- pldm_state_sensor_pdr* pdr =
- reinterpret_cast<pldm_state_sensor_pdr*>(e.data);
+ pldm_state_sensor_pdr* pdr = new (e.data) pldm_state_sensor_pdr;
EXPECT_EQ(pdr->hdr.record_handle, 2);
EXPECT_EQ(pdr->hdr.version, 1);
EXPECT_EQ(pdr->hdr.type, PLDM_STATE_SENSOR_PDR);
@@ -74,7 +73,7 @@
{
std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_GET_PDR_REQ_BYTES>
requestPayload{};
- auto req = reinterpret_cast<pldm_msg*>(requestPayload.data());
+ auto req = new (requestPayload.data()) pldm_msg;
size_t requestPayloadLength = requestPayload.size() - sizeof(pldm_msg_hdr);
MockdBusHandler mockedUtils;