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/oem/ibm/pldm_oem_ibm.cpp b/pldmtool/oem/ibm/pldm_oem_ibm.cpp
index 5f96654..a032621 100644
--- a/pldmtool/oem/ibm/pldm_oem_ibm.cpp
+++ b/pldmtool/oem/ibm/pldm_oem_ibm.cpp
@@ -52,7 +52,7 @@
{
std::vector<uint8_t> requestMsg(
sizeof(pldm_msg_hdr) + PLDM_GET_ALERT_STATUS_REQ_BYTES);
- auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
+ auto request = new (requestMsg.data()) pldm_msg;
auto rc = encode_get_alert_status_req(instanceId, versionId, request,
PLDM_GET_ALERT_STATUS_REQ_BYTES);
@@ -114,7 +114,7 @@
std::vector<uint8_t> requestMsg(
sizeof(pldm_msg_hdr) + PLDM_GET_FILE_TABLE_REQ_BYTES);
- auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
+ auto request = new (requestMsg.data()) pldm_msg;
auto rc = encode_get_file_table_req(instanceId, 0, PLDM_GET_FIRSTPART,
0, request);
@@ -137,8 +137,7 @@
uint32_t nextTransferHandle = 0;
size_t fileTableDataLength = 0;
uint8_t table_data_start_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_file_table_resp(
@@ -171,8 +170,7 @@
while (startptr < endptr)
{
ordered_json fdata;
- auto filetableData =
- reinterpret_cast<pldm_file_attr_table_entry*>(startptr);
+ auto filetableData = new (startptr) pldm_file_attr_table_entry;
fdata["FileHandle"] = std::to_string(filetableData->file_handle);
startptr += sizeof(filetableData->file_handle);
diff --git a/pldmtool/pldm_base_cmd.cpp b/pldmtool/pldm_base_cmd.cpp
index 2acbbe1..3701af0 100644
--- a/pldmtool/pldm_base_cmd.cpp
+++ b/pldmtool/pldm_base_cmd.cpp
@@ -132,7 +132,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_types_req(instanceId, request);
return {rc, requestMsg};
}
@@ -199,7 +199,7 @@
{
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,
pldmType, request);
@@ -253,7 +253,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_tid_req(instanceId, request);
return {rc, requestMsg};
}
@@ -302,7 +302,7 @@
{
std::vector<uint8_t> requestMsg(
sizeof(pldm_msg_hdr) + PLDM_GET_COMMANDS_REQ_BYTES);
- auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
+ auto request = new (requestMsg.data()) pldm_msg;
ver32_t version{0xFF, 0xFF, 0xFF, 0xFF};
if (inputVersion.size() != 0)
{
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(
diff --git a/pldmtool/pldm_fru_cmd.cpp b/pldmtool/pldm_fru_cmd.cpp
index aeb6f09..5b9fc58 100644
--- a/pldmtool/pldm_fru_cmd.cpp
+++ b/pldmtool/pldm_fru_cmd.cpp
@@ -42,7 +42,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_fru_record_table_metadata_req(
instanceId, request, PLDM_GET_FRU_RECORD_TABLE_METADATA_REQ_BYTES);
@@ -361,8 +361,7 @@
std::vector<uint8_t> requestMsg(sizeof(pldm_msg_hdr) + payloadLength,
0);
- auto reqMsg = reinterpret_cast<pldm_msg*>(requestMsg.data());
-
+ auto reqMsg = new (requestMsg.data()) pldm_msg;
auto rc = encode_get_fru_record_by_option_req(
instanceId, 0 /* DataTransferHandle */, 0 /* FRUTableHandle */,
recordSetIdentifier, recordType, fieldType, PLDM_GET_FIRSTPART,
@@ -414,7 +413,7 @@
{
std::vector<uint8_t> requestMsg(
sizeof(pldm_msg_hdr) + PLDM_GET_FRU_RECORD_TABLE_REQ_BYTES);
- 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,
diff --git a/pldmtool/pldm_fw_update_cmd.cpp b/pldmtool/pldm_fw_update_cmd.cpp
index cfd4608..0efee0e 100644
--- a/pldmtool/pldm_fw_update_cmd.cpp
+++ b/pldmtool/pldm_fw_update_cmd.cpp
@@ -88,7 +88,7 @@
{
std::vector<uint8_t> requestMsg(
sizeof(pldm_msg_hdr) + PLDM_GET_STATUS_REQ_BYTES);
- auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
+ auto request = new (requestMsg.data()) pldm_msg;
auto rc = encode_get_status_req(instanceId, request,
PLDM_GET_STATUS_REQ_BYTES);
return {rc, requestMsg};
@@ -178,7 +178,7 @@
{
std::vector<uint8_t> requestMsg(
sizeof(pldm_msg_hdr) + PLDM_GET_FIRMWARE_PARAMETERS_REQ_BYTES);
- auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
+ auto request = new (requestMsg.data()) pldm_msg;
auto rc = encode_get_firmware_parameters_req(
instanceId, PLDM_GET_FIRMWARE_PARAMETERS_REQ_BYTES, request);
return {rc, requestMsg};
@@ -519,7 +519,7 @@
{
std::vector<uint8_t> requestMsg(
sizeof(pldm_msg_hdr) + PLDM_QUERY_DEVICE_IDENTIFIERS_REQ_BYTES);
- auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
+ auto request = new (requestMsg.data()) pldm_msg;
auto rc = encode_query_device_identifiers_req(
instanceId, PLDM_QUERY_DEVICE_IDENTIFIERS_REQ_BYTES, request);
return {rc, requestMsg};
diff --git a/pldmtool/pldm_platform_cmd.cpp b/pldmtool/pldm_platform_cmd.cpp
index d2c1c7d..4f1dbee 100644
--- a/pldmtool/pldm_platform_cmd.cpp
+++ b/pldmtool/pldm_platform_cmd.cpp
@@ -226,7 +226,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;
auto rc = encode_get_pdr_req(
instanceId, recordHandle, dataTransferHandle, operationFlag,
@@ -294,8 +294,8 @@
{
nextPartRequired = true;
dataTransferHandle = nextDataTransferHndl;
- struct pldm_pdr_hdr* pdr_hdr =
- reinterpret_cast<struct pldm_pdr_hdr*>(respRecordData);
+ struct pldm_pdr_hdr* pdr_hdr = new (respRecordData)
+ pldm_pdr_hdr;
recordChangeNumber = pdr_hdr->record_change_num;
operationFlag = PLDM_GET_NEXTPART;
}
@@ -852,8 +852,7 @@
}
data += sizeof(pldm_pdr_hdr);
- pldm_pdr_fru_record_set* pdr =
- reinterpret_cast<pldm_pdr_fru_record_set*>(data);
+ pldm_pdr_fru_record_set* pdr = new (data) pldm_pdr_fru_record_set;
if (!pdr)
{
std::cerr << "Failed to get the FRU record set PDR" << std::endl;
@@ -880,8 +879,8 @@
}
data += sizeof(pldm_pdr_hdr);
- pldm_pdr_entity_association* pdr =
- reinterpret_cast<pldm_pdr_entity_association*>(data);
+ pldm_pdr_entity_association* pdr = new (data)
+ pldm_pdr_entity_association;
if (!pdr)
{
std::cerr << "Failed to get the PDR eneity association"
@@ -908,7 +907,7 @@
output["containedEntityCount"] =
static_cast<unsigned>(pdr->num_children);
- auto child = reinterpret_cast<pldm_entity*>(&pdr->children[0]);
+ pldm_entity* child = new (&pdr->children[0]) pldm_entity();
for (int i = 0; i < pdr->num_children; ++i)
{
output.emplace("containedEntityType[" + std::to_string(i + 1) + "]",
@@ -1635,7 +1634,7 @@
{
std::vector<uint8_t> requestMsg(
sizeof(pldm_msg_hdr) + PLDM_SET_STATE_EFFECTER_STATES_REQ_BYTES);
- auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
+ auto request = new (requestMsg.data()) pldm_msg;
if (effecterCount > maxEffecterCount ||
effecterCount < minEffecterCount)
@@ -1729,7 +1728,8 @@
uint8_t* effecterValue = (uint8_t*)&maxEffecterValue;
- auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
+ auto request = new (requestMsg.data()) pldm_msg;
+
size_t payload_length = PLDM_SET_NUMERIC_EFFECTER_VALUE_MIN_REQ_BYTES;
if (effecterDataSize == PLDM_EFFECTER_DATA_SIZE_UINT16 ||
@@ -1802,7 +1802,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;
uint8_t reserved = 0;
bitfield8_t bf;
@@ -1895,7 +1895,7 @@
{
std::vector<uint8_t> requestMsg(
sizeof(pldm_msg_hdr) + PLDM_GET_SENSOR_READING_REQ_BYTES);
- auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
+ auto request = new (requestMsg.data()) pldm_msg;
auto rc =
encode_get_sensor_reading_req(instanceId, sensorId, rearm, request);
@@ -2048,7 +2048,7 @@
{
std::vector<uint8_t> requestMsg(
sizeof(pldm_msg_hdr) + PLDM_GET_STATE_EFFECTER_STATES_REQ_BYTES);
- auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
+ auto request = new (requestMsg.data()) pldm_msg;
auto rc = encode_get_state_effecter_states_req(
instanceId, effecter_id, request,
@@ -2117,7 +2117,7 @@
{
std::vector<uint8_t> requestMsg(
sizeof(pldm_msg_hdr) + PLDM_GET_NUMERIC_EFFECTER_VALUE_REQ_BYTES);
- auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
+ auto request = new (requestMsg.data()) pldm_msg;
auto rc = encode_get_numeric_effecter_value_req(instanceId, effecterId,
request);