libpldmresponder : 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: If96afcb2c9dbea8fb6815d74a2181ca9ed04903a
Signed-off-by: Pavithra Barithaya <pavithrabarithaya07@gmail.com>
diff --git a/libpldmresponder/base.cpp b/libpldmresponder/base.cpp
index 2eae049..4a78ee8 100644
--- a/libpldmresponder/base.cpp
+++ b/libpldmresponder/base.cpp
@@ -79,7 +79,7 @@
}
Response response(sizeof(pldm_msg_hdr) + PLDM_GET_TYPES_RESP_BYTES, 0);
- auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
+ auto responsePtr = new (response.data()) pldm_msg;
auto rc = encode_get_types_resp(request->hdr.instance_id, PLDM_SUCCESS,
types.data(), responsePtr);
if (rc != PLDM_SUCCESS)
@@ -96,7 +96,7 @@
Type type;
Response response(sizeof(pldm_msg_hdr) + PLDM_GET_COMMANDS_RESP_BYTES, 0);
- auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
+ auto responsePtr = new (response.data()) pldm_msg;
auto rc = decode_get_commands_req(request, payloadLength, &type, &version);
@@ -138,7 +138,7 @@
uint8_t transferFlag;
Response response(sizeof(pldm_msg_hdr) + PLDM_GET_VERSION_RESP_BYTES, 0);
- auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
+ auto responsePtr = new (response.data()) pldm_msg;
uint8_t rc = decode_get_version_req(request, payloadLength, &transferHandle,
&transferFlag, &type);
@@ -179,7 +179,7 @@
Response Handler::getTID(const pldm_msg* request, size_t /*payloadLength*/)
{
Response response(sizeof(pldm_msg_hdr) + PLDM_GET_TID_RESP_BYTES, 0);
- auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
+ auto responsePtr = new (response.data()) pldm_msg;
auto rc = encode_get_tid_resp(request->hdr.instance_id, PLDM_SUCCESS,
TERMINUS_ID, responsePtr);
if (rc != PLDM_SUCCESS)
diff --git a/libpldmresponder/bios.cpp b/libpldmresponder/bios.cpp
index 57668d8..1ad297c 100644
--- a/libpldmresponder/bios.cpp
+++ b/libpldmresponder/bios.cpp
@@ -120,7 +120,7 @@
constexpr auto timeInterface = "xyz.openbmc_project.Time.EpochTime";
constexpr auto bmcTimePath = "/xyz/openbmc_project/time/bmc";
Response response(sizeof(pldm_msg_hdr) + PLDM_GET_DATE_TIME_RESP_BYTES, 0);
- auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
+ auto responsePtr = new (response.data()) pldm_msg;
EpochTimeUS timeUsec;
try
@@ -246,7 +246,7 @@
Response response(sizeof(pldm_msg_hdr) +
PLDM_GET_BIOS_TABLE_MIN_RESP_BYTES + table->size());
- auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
+ auto responsePtr = new (response.data()) pldm_msg;
rc = encode_get_bios_table_resp(
request->hdr.instance_id, PLDM_SUCCESS, 0 /* nxtTransferHandle */,
@@ -281,7 +281,7 @@
}
Response response(sizeof(pldm_msg_hdr) + PLDM_SET_BIOS_TABLE_RESP_BYTES);
- auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
+ auto responsePtr = new (response.data()) pldm_msg;
rc = encode_set_bios_table_resp(request->hdr.instance_id, PLDM_SUCCESS,
0 /* nxtTransferHandle */, responsePtr);
@@ -326,7 +326,7 @@
PLDM_GET_BIOS_ATTR_CURR_VAL_BY_HANDLE_MIN_RESP_BYTES +
entryLength,
0);
- auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
+ auto responsePtr = new (response.data()) pldm_msg;
rc = encode_get_bios_current_value_by_handle_resp(
request->hdr.instance_id, PLDM_SUCCESS, 0, PLDM_START_AND_END,
reinterpret_cast<const uint8_t*>(entry), entryLength, responsePtr);
@@ -358,7 +358,7 @@
Response response(
sizeof(pldm_msg_hdr) + PLDM_SET_BIOS_ATTR_CURR_VAL_RESP_BYTES, 0);
- auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
+ auto responsePtr = new (response.data()) pldm_msg;
encode_set_bios_attribute_current_value_resp(request->hdr.instance_id, rc,
0, responsePtr);
diff --git a/libpldmresponder/bios_config.cpp b/libpldmresponder/bios_config.cpp
index f58e7de..cabb0bd 100644
--- a/libpldmresponder/bios_config.cpp
+++ b/libpldmresponder/bios_config.cpp
@@ -1103,8 +1103,7 @@
}
Table attrValueEntry(sizeof(pldm_bios_attr_val_table_entry), 0);
- auto entry = reinterpret_cast<pldm_bios_attr_val_table_entry*>(
- attrValueEntry.data());
+ auto entry = new (attrValueEntry.data()) pldm_bios_attr_val_table_entry;
auto handler = findAttrHandle(attributeName);
auto type =
diff --git a/libpldmresponder/bios_enum_attribute.cpp b/libpldmresponder/bios_enum_attribute.cpp
index 2434c79..0ea636a 100644
--- a/libpldmresponder/bios_enum_attribute.cpp
+++ b/libpldmresponder/bios_enum_attribute.cpp
@@ -272,8 +272,7 @@
{
attrValueEntry.resize(sizeof(pldm_bios_attr_val_table_entry) + 1);
- auto entry = reinterpret_cast<pldm_bios_attr_val_table_entry*>(
- attrValueEntry.data());
+ auto entry = new (attrValueEntry.data()) pldm_bios_attr_val_table_entry;
std::string value = std::get<std::string>(attributevalue);
entry->attr_type = 0;
diff --git a/libpldmresponder/bios_integer_attribute.cpp b/libpldmresponder/bios_integer_attribute.cpp
index fd578a6..c19d633 100644
--- a/libpldmresponder/bios_integer_attribute.cpp
+++ b/libpldmresponder/bios_integer_attribute.cpp
@@ -226,8 +226,7 @@
attrValueEntry.resize(
sizeof(pldm_bios_attr_val_table_entry) + sizeof(int64_t) - 1);
- auto entry = reinterpret_cast<pldm_bios_attr_val_table_entry*>(
- attrValueEntry.data());
+ auto entry = new (attrValueEntry.data()) pldm_bios_attr_val_table_entry;
int64_t value = std::get<int64_t>(attributevalue);
entry->attr_type = 3;
diff --git a/libpldmresponder/bios_string_attribute.cpp b/libpldmresponder/bios_string_attribute.cpp
index 4b4bf3d..a77907e 100644
--- a/libpldmresponder/bios_string_attribute.cpp
+++ b/libpldmresponder/bios_string_attribute.cpp
@@ -163,8 +163,7 @@
attrValueEntry.resize(
sizeof(pldm_bios_attr_val_table_entry) + sizeof(uint16_t) + len - 1);
- auto entry = reinterpret_cast<pldm_bios_attr_val_table_entry*>(
- attrValueEntry.data());
+ auto entry = new (attrValueEntry.data()) pldm_bios_attr_val_table_entry;
entry->attr_type = 1;
memcpy(entry->value, &len, sizeof(uint16_t));
diff --git a/libpldmresponder/fru.cpp b/libpldmresponder/fru.cpp
index 5a5cec9..440057f 100644
--- a/libpldmresponder/fru.cpp
+++ b/libpldmresponder/fru.cpp
@@ -455,7 +455,7 @@
Response response(sizeof(pldm_msg_hdr) +
PLDM_GET_FRU_RECORD_TABLE_METADATA_RESP_BYTES,
0);
- auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
+ auto responsePtr = new (response.data()) pldm_msg;
impl.getFRURecordTableMetadata();
@@ -484,7 +484,7 @@
Response response(
sizeof(pldm_msg_hdr) + PLDM_GET_FRU_RECORD_TABLE_MIN_RESP_BYTES, 0);
- auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
+ auto responsePtr = new (response.data()) pldm_msg;
auto rc =
encode_get_fru_record_table_resp(request->hdr.instance_id, PLDM_SUCCESS,
@@ -536,7 +536,7 @@
auto respPayloadLength =
PLDM_GET_FRU_RECORD_BY_OPTION_MIN_RESP_BYTES + fruData.size();
Response response(sizeof(pldm_msg_hdr) + respPayloadLength, 0);
- auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
+ auto responsePtr = new (response.data()) pldm_msg;
rc = encode_get_fru_record_by_option_resp(
request->hdr.instance_id, PLDM_SUCCESS, 0, PLDM_START_AND_END,
@@ -574,7 +574,7 @@
Response response(
sizeof(pldm_msg_hdr) + PLDM_SET_FRU_RECORD_TABLE_RESP_BYTES);
- struct pldm_msg* responsePtr = reinterpret_cast<pldm_msg*>(response.data());
+ struct pldm_msg* responsePtr = new (response.data()) pldm_msg;
rc = encode_set_fru_record_table_resp(
request->hdr.instance_id, PLDM_SUCCESS, 0 /* nextDataTransferHandle */,
diff --git a/libpldmresponder/pdr_state_effecter.hpp b/libpldmresponder/pdr_state_effecter.hpp
index d431b0b..9d78930 100644
--- a/libpldmresponder/pdr_state_effecter.hpp
+++ b/libpldmresponder/pdr_state_effecter.hpp
@@ -55,8 +55,8 @@
std::vector<uint8_t> entry{};
entry.resize(pdrSize);
- pldm_state_effecter_pdr* pdr =
- reinterpret_cast<pldm_state_effecter_pdr*>(entry.data());
+ pldm_state_effecter_pdr* pdr = new (entry.data())
+ pldm_state_effecter_pdr;
if (!pdr)
{
error("Failed to get state effecter PDR.");
diff --git a/libpldmresponder/platform.cpp b/libpldmresponder/platform.cpp
index 2329213..db6b36d 100644
--- a/libpldmresponder/platform.cpp
+++ b/libpldmresponder/platform.cpp
@@ -259,7 +259,7 @@
response.resize(sizeof(pldm_msg_hdr) + PLDM_GET_PDR_MIN_RESP_BYTES +
respSizeBytes,
0);
- auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
+ auto responsePtr = new (response.data()) pldm_msg;
rc = encode_get_pdr_resp(
request->hdr.instance_id, PLDM_SUCCESS, e.handle.nextRecordHandle,
0, PLDM_START_AND_END, respSizeBytes, recordData, 0, responsePtr);
@@ -283,7 +283,7 @@
{
Response response(
sizeof(pldm_msg_hdr) + PLDM_SET_STATE_EFFECTER_STATES_RESP_BYTES, 0);
- auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
+ auto responsePtr = new (response.data()) pldm_msg;
uint16_t effecterId;
uint8_t compEffecterCnt;
constexpr auto maxCompositeEffecterCnt = 8;
@@ -401,7 +401,7 @@
}
Response response(
sizeof(pldm_msg_hdr) + PLDM_PLATFORM_EVENT_MESSAGE_RESP_BYTES, 0);
- auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
+ auto responsePtr = new (response.data()) pldm_msg;
rc = encode_platform_event_message_resp(request->hdr.instance_id, rc,
PLDM_EVENT_NO_LOGGING, responsePtr);
@@ -684,7 +684,7 @@
getEffecterDataSize(effecterDataSize);
Response response(responsePayloadLength + sizeof(pldm_msg_hdr));
- auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
+ auto responsePtr = new (response.data()) pldm_msg;
rc = platform_numeric_effecter::getNumericEffecterValueHandler(
propertyType, dbusValue, effecterDataSize, responsePtr,
@@ -735,7 +735,7 @@
{
std::vector<uint8_t> pdrBuffer(sizeof(pldm_terminus_locator_pdr));
- auto pdr = reinterpret_cast<pldm_terminus_locator_pdr*>(pdrBuffer.data());
+ auto pdr = new (pdrBuffer.data()) pldm_terminus_locator_pdr;
pdr->hdr.record_handle = 0;
pdr->hdr.version = 1;
@@ -749,8 +749,8 @@
pdr->terminus_locator_type = PLDM_TERMINUS_LOCATOR_TYPE_MCTP_EID;
pdr->terminus_locator_value_size =
sizeof(pldm_terminus_locator_type_mctp_eid);
- auto locatorValue = reinterpret_cast<pldm_terminus_locator_type_mctp_eid*>(
- pdr->terminus_locator_value);
+ auto locatorValue = new (pdr->terminus_locator_value)
+ pldm_terminus_locator_type_mctp_eid;
locatorValue->eid = pldm::BmcMctpEid;
PdrEntry pdrEntry{};
@@ -820,7 +820,7 @@
Response response(
sizeof(pldm_msg_hdr) + PLDM_GET_STATE_SENSOR_READINGS_MIN_RESP_BYTES +
sizeof(get_sensor_state_field) * comSensorCnt);
- auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
+ auto responsePtr = new (response.data()) pldm_msg;
rc = encode_get_state_sensor_readings_resp(
request->hdr.instance_id, rc, comSensorCnt, stateField.data(),
responsePtr);
@@ -865,7 +865,7 @@
auto pdrRecord = stateSensorPDRs.getFirstRecord(pdrEntry);
while (pdrRecord)
{
- pdr = reinterpret_cast<pldm_state_sensor_pdr*>(pdrEntry.data);
+ pdr = new (pdrEntry.data) pldm_state_sensor_pdr;
assert(pdr != nullptr);
if (pdr->sensor_id != sensorId)
{
@@ -937,7 +937,7 @@
auto pdrRecord = stateEffecterPDRs.getFirstRecord(pdrEntry);
while (pdrRecord)
{
- pdr = reinterpret_cast<pldm_state_effecter_pdr*>(pdrEntry.data);
+ pdr = new (pdrEntry.data) pldm_state_effecter_pdr;
assert(pdr != nullptr);
if (pdr->effecter_id != effecterId)
{
@@ -984,7 +984,7 @@
{
std::vector<uint8_t> requestMsg(
sizeof(pldm_msg_hdr) + PLDM_SET_EVENT_RECEIVER_REQ_BYTES);
- auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
+ auto request = new (requestMsg.data()) pldm_msg;
auto instanceId = instanceIdDb->next(eid);
uint8_t eventMessageGlobalEnable =
PLDM_EVENT_MESSAGE_GLOBAL_ENABLE_ASYNC_KEEP_ALIVE;
diff --git a/libpldmresponder/platform.hpp b/libpldmresponder/platform.hpp
index 2422e3c..8d7a55e 100644
--- a/libpldmresponder/platform.hpp
+++ b/libpldmresponder/platform.hpp
@@ -371,7 +371,7 @@
auto pdrRecord = stateEffecterPDRs.getFirstRecord(pdrEntry);
while (pdrRecord)
{
- pdr = reinterpret_cast<pldm_state_effecter_pdr*>(pdrEntry.data);
+ pdr = new (pdrEntry.data) pldm_state_effecter_pdr;
if (pdr->effecter_id != effecterId)
{
pdr = nullptr;
diff --git a/libpldmresponder/platform_numeric_effecter.hpp b/libpldmresponder/platform_numeric_effecter.hpp
index a38863b..a3fab47 100644
--- a/libpldmresponder/platform_numeric_effecter.hpp
+++ b/libpldmresponder/platform_numeric_effecter.hpp
@@ -275,7 +275,7 @@
auto pdrRecord = numericEffecterPDRs.getFirstRecord(pdrEntry);
while (pdrRecord)
{
- pdr = reinterpret_cast<pldm_numeric_effecter_value_pdr*>(pdrEntry.data);
+ pdr = new (pdrEntry.data) pldm_numeric_effecter_value_pdr;
if (pdr->effecter_id != effecterId)
{
pdr = nullptr;
@@ -512,7 +512,7 @@
while (pdrRecord)
{
- pdr = reinterpret_cast<pldm_numeric_effecter_value_pdr*>(pdrEntry.data);
+ pdr = new (pdrEntry.data) pldm_numeric_effecter_value_pdr;
if (pdr->effecter_id != effecterId)
{
pdr = nullptr;
diff --git a/libpldmresponder/platform_state_effecter.hpp b/libpldmresponder/platform_state_effecter.hpp
index c6f7432..81d580c 100644
--- a/libpldmresponder/platform_state_effecter.hpp
+++ b/libpldmresponder/platform_state_effecter.hpp
@@ -68,7 +68,7 @@
auto pdrRecord = stateEffecterPDRs.getFirstRecord(pdrEntry);
while (pdrRecord)
{
- pdr = reinterpret_cast<pldm_state_effecter_pdr*>(pdrEntry.data);
+ pdr = new (pdrEntry.data) pldm_state_effecter_pdr;
if (pdr->effecter_id != effecterId)
{
pdr = nullptr;
diff --git a/libpldmresponder/platform_state_sensor.hpp b/libpldmresponder/platform_state_sensor.hpp
index 003763f..61ac272 100644
--- a/libpldmresponder/platform_state_sensor.hpp
+++ b/libpldmresponder/platform_state_sensor.hpp
@@ -109,7 +109,7 @@
auto pdrRecord = stateSensorPDRs.getFirstRecord(pdrEntry);
while (pdrRecord)
{
- pdr = reinterpret_cast<pldm_state_sensor_pdr*>(pdrEntry.data);
+ pdr = new (pdrEntry.data) pldm_state_sensor_pdr;
assert(pdr != nullptr);
if (pdr->sensor_id != sensorId)
{