Fixed minor bugs in libcppbor.
Fixed a dangling pointer issue in ViewBstr roundtrip test.
Used explicit template instantiation for basic_string_view in
tests instead of relying on template argument deduction.
Bug: 224815962
Change-Id: I21967e9bb0ca12653c3f4a0c1bb7a99441894dc4
Test: Corrected unit test. Existing test cases still pass.
diff --git a/src/cppbor.cpp b/src/cppbor.cpp
index 0e9c939..5c9827f 100644
--- a/src/cppbor.cpp
+++ b/src/cppbor.cpp
@@ -132,9 +132,8 @@
const ViewBstr* viewBstr = item->asViewBstr();
assert(viewBstr != nullptr);
- std::basic_string_view view = viewBstr->view();
- valueData = view.data();
- valueSize = view.size();
+ valueData = viewBstr->view().data();
+ valueSize = viewBstr->view().size();
}
if (valueSize > maxBStrSize) {
diff --git a/tests/cppbor_test.cpp b/tests/cppbor_test.cpp
index ebdcc02..7756387 100644
--- a/tests/cppbor_test.cpp
+++ b/tests/cppbor_test.cpp
@@ -944,7 +944,7 @@
TEST(ConvertTest, ViewBstr) {
array<uint8_t, 3> vec{0x23, 0x24, 0x22};
- basic_string_view sv(vec.data(), vec.size());
+ basic_string_view<uint8_t> sv(vec.data(), vec.size());
unique_ptr<Item> item = details::makeItem(ViewBstr(sv));
EXPECT_EQ(BSTR, item->type());
@@ -1081,7 +1081,7 @@
TEST(CloningTest, ViewBstr) {
array<uint8_t, 5> vec{1, 2, 3, 255, 0};
- basic_string_view sv(vec.data(), vec.size());
+ basic_string_view<uint8_t> sv(vec.data(), vec.size());
ViewBstr item(sv);
auto clone = item.clone();
EXPECT_EQ(clone->type(), BSTR);
@@ -1707,11 +1707,14 @@
}
TEST(FullParserTest, ViewBstr) {
- ViewBstr val("\x00\x01\x02"s);
+ const std::string strVal = "\x00\x01\x02"s;
+ const ViewBstr val(strVal);
+ EXPECT_EQ(val.toString(), "\x43\x00\x01\x02"s);
auto enc = val.encode();
auto [item, pos, message] = parseWithViews(enc.data(), enc.size());
EXPECT_THAT(item, MatchesItem(val));
+ EXPECT_EQ(hexDump(item->toString()), hexDump(val.toString()));
}
TEST(FullParserTest, ReservedAdditionalInformation) {