From 237ccfe4c30633c1829d83d293e23bc82bcff4d9 Mon Sep 17 00:00:00 2001 From: Vikram Gaur Date: Fri, 18 Mar 2022 22:49:24 +0000 Subject: 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. --- src/cppbor.cpp | 5 ++--- 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 vec{0x23, 0x24, 0x22}; - basic_string_view sv(vec.data(), vec.size()); + basic_string_view sv(vec.data(), vec.size()); unique_ptr item = details::makeItem(ViewBstr(sv)); EXPECT_EQ(BSTR, item->type()); @@ -1081,7 +1081,7 @@ TEST(CloningTest, ViewTstr) { TEST(CloningTest, ViewBstr) { array vec{1, 2, 3, 255, 0}; - basic_string_view sv(vec.data(), vec.size()); + basic_string_view 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) { -- cgit v1.2.3