pldmtool : 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: Iaebfaa78c4d3c0c3658dbf9f2f6eaa932b061e83
Signed-off-by: Pavithra Barithaya <pavithrabarithaya07@gmail.com>
diff --git a/pldmtool/pldm_bios_cmd.cpp b/pldmtool/pldm_bios_cmd.cpp
index 3b76b68..f496133 100644
--- a/pldmtool/pldm_bios_cmd.cpp
+++ b/pldmtool/pldm_bios_cmd.cpp
@@ -48,7 +48,7 @@
std::pair<int, std::vector<uint8_t>> createRequestMsg() override
{
std::vector<uint8_t> requestMsg(sizeof(pldm_msg_hdr));
- auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
+ auto request = new (requestMsg.data()) pldm_msg;
auto rc = encode_get_date_time_req(instanceId, request);
return {rc, requestMsg};
@@ -112,7 +112,7 @@
{
std::vector<uint8_t> requestMsg(
sizeof(pldm_msg_hdr) + sizeof(struct pldm_set_date_time_req));
- auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
+ auto request = new (requestMsg.data()) pldm_msg;
uint16_t year = 0;
uint8_t month = 0;
uint8_t day = 0;
@@ -197,7 +197,7 @@
{
std::vector<uint8_t> requestMsg(
sizeof(pldm_msg_hdr) + PLDM_GET_BIOS_TABLE_REQ_BYTES);
- auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
+ auto request = new (requestMsg.data()) pldm_msg;
auto rc = encode_get_bios_table_req(instanceId, 0, PLDM_GET_FIRSTPART,
tableType, request);
@@ -218,8 +218,7 @@
uint8_t cc = 0, transferFlag = 0;
uint32_t nextTransferHandle = 0;
size_t bios_table_offset;
- auto responsePtr =
- reinterpret_cast<struct pldm_msg*>(responseMsg.data());
+ auto responsePtr = new (responseMsg.data()) pldm_msg;
auto payloadLength = responseMsg.size() - sizeof(pldm_msg_hdr);
rc = decode_get_bios_table_resp(responsePtr, payloadLength, &cc,
@@ -711,7 +710,7 @@
std::vector<uint8_t> requestMsg(
sizeof(pldm_msg_hdr) +
PLDM_GET_BIOS_ATTR_CURR_VAL_BY_HANDLE_REQ_BYTES);
- auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
+ auto request = new (requestMsg.data()) pldm_msg;
auto rc = encode_get_bios_attribute_current_value_by_handle_req(
instanceId, 0, PLDM_GET_FIRSTPART, *handle, request);
@@ -732,8 +731,7 @@
uint8_t cc = 0, transferFlag = 0;
uint32_t nextTransferHandle = 0;
struct variable_field attributeData;
- auto responsePtr =
- reinterpret_cast<struct pldm_msg*>(responseMsg.data());
+ auto responsePtr = new (responseMsg.data()) pldm_msg;
auto payloadLength = responseMsg.size() - sizeof(pldm_msg_hdr);
rc = decode_get_bios_attribute_current_value_by_handle_resp(
@@ -913,8 +911,7 @@
rc = encode_set_bios_attribute_current_value_req(
instanceId, 0, PLDM_START_AND_END, attrValueEntry.data(),
- attrValueEntry.size(),
- reinterpret_cast<pldm_msg*>(requestMsg.data()),
+ attrValueEntry.size(), new (requestMsg.data()) pldm_msg,
requestMsg.size() - sizeof(pldm_msg_hdr));
if (rc != PLDM_SUCCESS)
@@ -931,8 +928,7 @@
}
uint8_t cc = 0;
uint32_t nextTransferHandle = 0;
- auto responsePtr =
- reinterpret_cast<struct pldm_msg*>(responseMsg.data());
+ auto responsePtr = new (responseMsg.data()) pldm_msg;
auto payloadLength = responseMsg.size() - sizeof(pldm_msg_hdr);
rc = decode_set_bios_attribute_current_value_resp(