platform: Fix the error of calling the htole16/htole32 method
When calling htole16/32 in the encode_set_numeric_effecter_value_req
method, maybe the conversion failed since the byte misalignment.
Signed-off-by: George Liu <liuxiwei@inspur.com>
Change-Id: I4c07efc2d3cacc35d15a9eacd45f31b51b11e4df
diff --git a/libpldm/platform.c b/libpldm/platform.c
index 399f2ff..43025ae 100644
--- a/libpldm/platform.c
+++ b/libpldm/platform.c
@@ -395,8 +395,10 @@
return PLDM_ERROR_INVALID_LENGTH;
}
- memcpy(request->effecter_value, effecter_value, 2);
- *request->effecter_value = htole16(*request->effecter_value);
+ uint16_t val = *(uint16_t *)(effecter_value);
+ val = htole16(val);
+ memcpy(request->effecter_value, &val, sizeof(uint16_t));
+
} else if (effecter_data_size == PLDM_EFFECTER_DATA_SIZE_UINT32 ||
effecter_data_size == PLDM_EFFECTER_DATA_SIZE_SINT32) {
if (payload_length !=
@@ -404,8 +406,9 @@
return PLDM_ERROR_INVALID_LENGTH;
}
- memcpy(request->effecter_value, effecter_value, 4);
- *request->effecter_value = htole32(*request->effecter_value);
+ uint32_t val = *(uint32_t *)(effecter_value);
+ val = htole32(val);
+ memcpy(request->effecter_value, &val, sizeof(uint32_t));
}
request->effecter_id = htole16(effecter_id);
diff --git a/libpldm/tests/libpldm_platform_test.cpp b/libpldm/tests/libpldm_platform_test.cpp
index 5e21e48..54daa33 100644
--- a/libpldm/tests/libpldm_platform_test.cpp
+++ b/libpldm/tests/libpldm_platform_test.cpp
@@ -452,17 +452,17 @@
TEST(SetNumericEffecterValue, testGoodEncodeRequest)
{
uint16_t effecter_id = 0;
- uint8_t effecter_data_size = PLDM_EFFECTER_DATA_SIZE_UINT8;
- uint8_t effecter_value = 1;
+ uint8_t effecter_data_size = PLDM_EFFECTER_DATA_SIZE_UINT16;
+ uint16_t effecter_value = 65534;
std::vector<uint8_t> requestMsg(
- hdrSize + PLDM_SET_NUMERIC_EFFECTER_VALUE_MIN_REQ_BYTES);
+ hdrSize + PLDM_SET_NUMERIC_EFFECTER_VALUE_MIN_REQ_BYTES + 1);
auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
auto rc = encode_set_numeric_effecter_value_req(
0, effecter_id, effecter_data_size,
reinterpret_cast<uint8_t*>(&effecter_value), request,
- PLDM_SET_NUMERIC_EFFECTER_VALUE_MIN_REQ_BYTES);
+ PLDM_SET_NUMERIC_EFFECTER_VALUE_MIN_REQ_BYTES + 1);
EXPECT_EQ(rc, PLDM_SUCCESS);
struct pldm_set_numeric_effecter_value_req* req =
@@ -470,8 +470,9 @@
request->payload);
EXPECT_EQ(effecter_id, req->effecter_id);
EXPECT_EQ(effecter_data_size, req->effecter_data_size);
- EXPECT_EQ(effecter_value,
- *(reinterpret_cast<uint8_t*>(&req->effecter_value[0])));
+ uint16_t* val = (uint16_t*)req->effecter_value;
+ *val = le16toh(*val);
+ EXPECT_EQ(effecter_value, *val);
}
TEST(SetNumericEffecterValue, testBadEncodeRequest)