Fix the error of calling htole16/htole32/le16toh/le32toh method
libpldm is endian safe, fix the wrong usage in test case.
Change-Id: Iaa8816feda462a11fad098fe9a3074913ab367a3
Signed-off-by: Jolie Ku <jolie_ku@wistron.com>
diff --git a/libpldm/platform.c b/libpldm/platform.c
index 9c696fc..3f91ee9 100644
--- a/libpldm/platform.c
+++ b/libpldm/platform.c
@@ -925,9 +925,15 @@
PLDM_GET_NUMERIC_EFFECTER_VALUE_MIN_RESP_BYTES + 2) {
return PLDM_ERROR_INVALID_LENGTH;
}
- memcpy(response->pending_and_present_values, pending_value, 2);
- memcpy(&response->pending_and_present_values[2], present_value,
- 2);
+ uint16_t val_pending = *(uint16_t *)pending_value;
+ val_pending = htole16(val_pending);
+ memcpy(response->pending_and_present_values, &val_pending,
+ sizeof(uint16_t));
+ uint16_t val_present = *(uint16_t *)present_value;
+ val_present = htole16(val_present);
+ memcpy(
+ (response->pending_and_present_values + sizeof(uint16_t)),
+ &val_present, sizeof(uint16_t));
} else if (effecter_data_size == PLDM_EFFECTER_DATA_SIZE_UINT32 ||
effecter_data_size == PLDM_EFFECTER_DATA_SIZE_SINT32) {
@@ -935,9 +941,15 @@
PLDM_GET_NUMERIC_EFFECTER_VALUE_MIN_RESP_BYTES + 6) {
return PLDM_ERROR_INVALID_LENGTH;
}
- memcpy(response->pending_and_present_values, pending_value, 4);
- memcpy(&response->pending_and_present_values[4], present_value,
- 4);
+ uint32_t val_pending = *(uint32_t *)pending_value;
+ val_pending = htole32(val_pending);
+ memcpy(response->pending_and_present_values, &val_pending,
+ sizeof(uint32_t));
+ uint32_t val_present = *(uint32_t *)present_value;
+ val_present = htole32(val_present);
+ memcpy(
+ (response->pending_and_present_values + sizeof(uint32_t)),
+ &val_present, sizeof(uint32_t));
}
return PLDM_SUCCESS;
}
@@ -1012,9 +1024,16 @@
PLDM_GET_NUMERIC_EFFECTER_VALUE_MIN_RESP_BYTES + 2) {
return PLDM_ERROR_INVALID_LENGTH;
}
- memcpy(pending_value, response->pending_and_present_values, 2);
- memcpy(present_value, &response->pending_and_present_values[2],
- 2);
+ memcpy(pending_value, response->pending_and_present_values,
+ sizeof(uint16_t));
+ uint16_t *val_pending = (uint16_t *)pending_value;
+ *val_pending = le16toh(*val_pending);
+ memcpy(
+ present_value,
+ (response->pending_and_present_values + sizeof(uint16_t)),
+ sizeof(uint16_t));
+ uint16_t *val_present = (uint16_t *)present_value;
+ *val_present = le16toh(*val_present);
} else if (*effecter_data_size == PLDM_EFFECTER_DATA_SIZE_UINT32 ||
*effecter_data_size == PLDM_EFFECTER_DATA_SIZE_SINT32) {
@@ -1022,9 +1041,16 @@
PLDM_GET_NUMERIC_EFFECTER_VALUE_MIN_RESP_BYTES + 6) {
return PLDM_ERROR_INVALID_LENGTH;
}
- memcpy(pending_value, response->pending_and_present_values, 4);
- memcpy(present_value, &response->pending_and_present_values[4],
- 4);
+ memcpy(pending_value, response->pending_and_present_values,
+ sizeof(uint32_t));
+ uint32_t *val_pending = (uint32_t *)pending_value;
+ *val_pending = le32toh(*val_pending);
+ memcpy(
+ present_value,
+ (response->pending_and_present_values + sizeof(uint32_t)),
+ sizeof(uint32_t));
+ uint32_t *val_present = (uint32_t *)present_value;
+ *val_present = le32toh(*val_present);
}
return PLDM_SUCCESS;
}
diff --git a/libpldm/tests/libpldm_platform_test.cpp b/libpldm/tests/libpldm_platform_test.cpp
index 25aee71..d19ebf5 100644
--- a/libpldm/tests/libpldm_platform_test.cpp
+++ b/libpldm/tests/libpldm_platform_test.cpp
@@ -1318,33 +1318,33 @@
uint8_t effecter_dataSize = PLDM_EFFECTER_DATA_SIZE_UINT32;
uint8_t effecter_operState = EFFECTER_OPER_STATE_ENABLED_NOUPDATEPENDING;
uint32_t pendingValue = 0x12345678;
- uint32_t presentValue = 0xABCDEF00;
+ uint32_t presentValue = 0xABCDEF11;
std::array<uint8_t,
hdrSize + PLDM_GET_NUMERIC_EFFECTER_VALUE_MIN_RESP_BYTES + 6>
responseMsg{};
auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
- uint32_t pendingValue_le = htole32(pendingValue);
- uint32_t presentValue_le = htole32(presentValue);
-
auto rc = encode_get_numeric_effecter_value_resp(
0, completionCode, effecter_dataSize, effecter_operState,
- reinterpret_cast<uint8_t*>(&pendingValue_le),
- reinterpret_cast<uint8_t*>(&presentValue_le), response,
+ reinterpret_cast<uint8_t*>(&pendingValue),
+ reinterpret_cast<uint8_t*>(&presentValue), response,
responseMsg.size() - hdrSize);
struct pldm_get_numeric_effecter_value_resp* resp =
reinterpret_cast<struct pldm_get_numeric_effecter_value_resp*>(
response->payload);
+ uint32_t* val_pending = (uint32_t*)(&resp->pending_and_present_values[0]);
+ *val_pending = le32toh(*val_pending);
+ uint32_t* val_present = (uint32_t*)(&resp->pending_and_present_values[4]);
+ *val_present = le32toh(*val_present);
+
EXPECT_EQ(rc, PLDM_SUCCESS);
EXPECT_EQ(effecter_dataSize, resp->effecter_data_size);
EXPECT_EQ(effecter_operState, resp->effecter_oper_state);
- EXPECT_EQ(pendingValue, le32toh(*(reinterpret_cast<uint32_t*>(
- &resp->pending_and_present_values[0]))));
- EXPECT_EQ(presentValue, le32toh(*(reinterpret_cast<uint32_t*>(
- &resp->pending_and_present_values[4]))));
+ EXPECT_EQ(pendingValue, *val_pending);
+ EXPECT_EQ(presentValue, *val_present);
}
TEST(GetNumericEffecterValue, testBadEncodeResponse)
@@ -1418,17 +1418,12 @@
&reteffecter_dataSize, &reteffecter_operState, retpendingValue,
retpresentValue);
- uint16_t retpending_value =
- le16toh(*(reinterpret_cast<uint16_t*>(retpendingValue)));
- uint16_t retpresent_value =
- le16toh(*(reinterpret_cast<uint16_t*>(retpresentValue)));
-
EXPECT_EQ(rc, PLDM_SUCCESS);
EXPECT_EQ(completionCode, retcompletionCode);
EXPECT_EQ(effecter_dataSize, reteffecter_dataSize);
EXPECT_EQ(effecter_operState, reteffecter_operState);
- EXPECT_EQ(pendingValue, retpending_value);
- EXPECT_EQ(presentValue, retpresent_value);
+ EXPECT_EQ(pendingValue, *(reinterpret_cast<uint16_t*>(retpendingValue)));
+ EXPECT_EQ(presentValue, *(reinterpret_cast<uint16_t*>(retpresentValue)));
}
TEST(GetNumericEffecterValue, testBadDecodeResponse)