bugfix: tools: copy string out, expected nul-termination
tools: There were some subtle issues with std::string, where it's not
always nul-terminated. Therefore comparisons that looked identical
weren't because one instance of the std::string would be nul-terminated
and the other not.
test: response from enumerate must nul-terminate bytes
The response from the IPMI command will include a trailing
nul-terminator.
Change-Id: I8ff18b0c52c5288170257414572bee2366ccc431
Signed-off-by: Patrick Venture <venture@google.com>
diff --git a/test/tools_blob_unittest.cpp b/test/tools_blob_unittest.cpp
index a04a741..cbbb151 100644
--- a/test/tools_blob_unittest.cpp
+++ b/test/tools_blob_unittest.cpp
@@ -41,7 +41,7 @@
/* return value. */
std::vector<std::uint8_t> resp = {0xcf, 0xc2, 0x00, 0x00, 0x00,
- 'a', 'b', 'c', 'd'};
+ 'a', 'b', 'c', 'd', 0x00};
EXPECT_CALL(ipmiMock, sendPacket(Eq(request))).WillOnce(Return(resp));
EXPECT_STREQ("abcd", blob.enumerateBlob(1).c_str());
@@ -86,11 +86,12 @@
/* return value. */
std::vector<std::uint8_t> resp2 = {0xcf, 0xc2, 0x00, 0x00, 0x00,
- 'a', 'b', 'c', 'd'};
+ 'a', 'b', 'c', 'd', 0x00};
EXPECT_CALL(ipmiMock, sendPacket(Eq(request2))).WillOnce(Return(resp2));
- std::vector<std::string> expectedList = {"abcd"};
+ /* A std::string is not nul-terminated by default. */
+ std::vector<std::string> expectedList = {std::string{"abcd"}};
EXPECT_EQ(expectedList, blob.getBlobList());
}
diff --git a/tools/blob_handler.cpp b/tools/blob_handler.cpp
index 75d3cff..a181559 100644
--- a/tools/blob_handler.cpp
+++ b/tools/blob_handler.cpp
@@ -128,9 +128,8 @@
std::copy(data, data + sizeof(std::uint32_t), std::back_inserter(payload));
auto resp = sendIpmiPayload(BlobOEMCommands::bmcBlobEnumerate, payload);
- std::string output;
- std::copy(resp.begin(), resp.end(), std::back_inserter(output));
- return output;
+ return (resp.size() > 0) ? std::string(&resp[0], &resp[resp.size() - 1])
+ : "";
}
std::vector<std::string> BlobHandler::getBlobList()