diff options
author | Vikram Gaur <vikramgaur@google.com> | 2022-03-18 22:49:24 +0000 |
---|---|---|
committer | Vikram Gaur <vikramgaur@google.com> | 2022-03-18 23:33:17 +0000 |
commit | 237ccfe4c30633c1829d83d293e23bc82bcff4d9 (patch) | |
tree | 5cf6e6aed510f5de8e63831619b71c7f71f374ea | |
parent | 273776ac270a1b4a3fef27021926115900a9b9b6 (diff) | |
download | libcppbor-237ccfe4c30633c1829d83d293e23bc82bcff4d9.tar.gz |
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.
-rw-r--r-- | src/cppbor.cpp | 5 | ||||
-rw-r--r-- | tests/cppbor_test.cpp | 9 |
2 files changed, 8 insertions, 6 deletions
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 @@ bool prettyPrintInternal(const Item* item, string& out, size_t indent, size_t ma 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, ViewTstr) { 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, ViewTstr) { 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, ViewTstr) { } 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) { |