aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndroid Build Coastguard Worker <android-build-coastguard-worker@google.com>2022-05-10 07:17:20 +0000
committerAndroid Build Coastguard Worker <android-build-coastguard-worker@google.com>2022-05-10 07:17:20 +0000
commit6dcfec89e3120d78cbfcb63a0fd4a21111f7a3c8 (patch)
tree185c84cdb381e7778dedd07a03929ee6de377f9f
parent5883d6c955f7172fed2100b4b98833e18984d60b (diff)
parent3c9787c57c4de7c966d2f43f22b26dc145d739a4 (diff)
downloadlibcppbor-6dcfec89e3120d78cbfcb63a0fd4a21111f7a3c8.tar.gz
Snap for 8564071 from 3c9787c57c4de7c966d2f43f22b26dc145d739a4 to mainline-art-release
Change-Id: Iec149a1e69b467a49cc3521d2cd1617e949faacb
-rw-r--r--include/cppbor/cppbor.h40
-rw-r--r--rules.mk8
-rw-r--r--src/cppbor.cpp6
-rw-r--r--src/cppbor_parse.cpp8
-rw-r--r--tests/cppbor_test.cpp21
5 files changed, 62 insertions, 21 deletions
diff --git a/include/cppbor/cppbor.h b/include/cppbor/cppbor.h
index b78c41f..8338441 100644
--- a/include/cppbor/cppbor.h
+++ b/include/cppbor/cppbor.h
@@ -25,6 +25,20 @@
#include <string>
#include <string_view>
#include <vector>
+#include <algorithm>
+
+#ifdef OS_WINDOWS
+#include <basetsd.h>
+
+#define ssize_t SSIZE_T
+#endif // OS_WINDOWS
+
+#ifdef TRUE
+#undef TRUE
+#endif // TRUE
+#ifdef FALSE
+#undef FALSE
+#endif // FALSE
namespace cppbor {
@@ -598,6 +612,13 @@ class Array : public Item {
Array(Args&&... args);
/**
+ * The above variadic constructor is disabled if sizeof(Args) != 1, so special
+ * case an explicit Array constructor for creating an Array with one Item.
+ */
+ template <typename T, typename Enable>
+ explicit Array(T&& v);
+
+ /**
* Append a single element to the Array, of any compatible type.
*/
template <typename T>
@@ -698,7 +719,7 @@ class Map : public Item {
*
* If the searched-for `key` is not present, returns `nullptr`.
*
- * Note that if the map is canonicalized (sorted), Map::get() peforms a binary search. If your
+ * Note that if the map is canonicalized (sorted), Map::get() performs a binary search. If your
* map is large and you're searching in it many times, it may be worthwhile to canonicalize it
* to make Map::get() faster. Any use of a method that might modify the map disables the
* speedup.
@@ -912,7 +933,7 @@ class Null : public Simple {
* for unit tests.
*/
std::string prettyPrint(const Item* item, size_t maxBStrSize = 32,
- const std::vector<std::string>& mapKeysNotToPrint = {});
+ const std::vector<std::string>& mapKeysToNotPrint = {});
/**
* Returns pretty-printed CBOR for |value|.
@@ -927,7 +948,7 @@ std::string prettyPrint(const Item* item, size_t maxBStrSize = 32,
* for unit tests.
*/
std::string prettyPrint(const std::vector<uint8_t>& encodedCbor, size_t maxBStrSize = 32,
- const std::vector<std::string>& mapKeysNotToPrint = {});
+ const std::vector<std::string>& mapKeysToNotPrint = {});
/**
* Details. Mostly you shouldn't have to look below, except perhaps at the docstring for makeItem.
@@ -1039,14 +1060,21 @@ inline void map_helper(Map& map, Key&& key, Value&& value, Rest&&... rest) {
} // namespace details
template <typename... Args,
- /* Prevent use as copy ctor */ typename = std::enable_if_t<
- (sizeof...(Args)) != 1 ||
- !(std::is_same_v<Array, std::remove_cv_t<std::remove_reference_t<Args>>> || ...)>>
+ /* Prevent implicit construction with a single argument. */
+ typename = std::enable_if_t<(sizeof...(Args)) != 1>>
Array::Array(Args&&... args) {
mEntries.reserve(sizeof...(args));
(mEntries.push_back(details::makeItem(std::forward<Args>(args))), ...);
}
+template <typename T,
+ /* Prevent use as copy constructor. */
+ typename = std::enable_if_t<
+ !std::is_same_v<Array, std::remove_cv_t<std::remove_reference_t<T>>>>>
+Array::Array(T&& v) {
+ mEntries.push_back(details::makeItem(std::forward<T>(v)));
+}
+
template <typename T>
Array& Array::add(T&& v) & {
mEntries.push_back(details::makeItem(std::forward<T>(v)));
diff --git a/rules.mk b/rules.mk
index 5bcea52..c5a19d8 100644
--- a/rules.mk
+++ b/rules.mk
@@ -24,6 +24,10 @@ MODULE_SRCS := \
$(LOCAL_DIR)/src/cppbor.cpp \
$(LOCAL_DIR)/src/cppbor_parse.cpp
-GLOBAL_INCLUDES += $(LOCAL_DIR)/include/cppbor/
+MODULE_EXPORT_INCLUDES += $(LOCAL_DIR)/include/cppbor/
-include make/module.mk \ No newline at end of file
+MODULE_LIBRARY_DEPS += \
+ trusty/user/base/lib/libstdc++-trusty \
+ external/boringssl \
+
+include make/library.mk
diff --git a/src/cppbor.cpp b/src/cppbor.cpp
index 0e9c939..9236d15 100644
--- a/src/cppbor.cpp
+++ b/src/cppbor.cpp
@@ -18,6 +18,7 @@
#include <inttypes.h>
#include <openssl/sha.h>
+#include <cstdint>
#include "cppbor_parse.h"
@@ -132,9 +133,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/src/cppbor_parse.cpp b/src/cppbor_parse.cpp
index 964a72d..a221cf4 100644
--- a/src/cppbor_parse.cpp
+++ b/src/cppbor_parse.cpp
@@ -16,6 +16,7 @@
#include "cppbor_parse.h"
+#include <sstream>
#include <stack>
#ifndef __TRUSTY__
@@ -195,6 +196,13 @@ std::tuple<const uint8_t*, ParseClient*> handleCompound(
std::tuple<const uint8_t*, ParseClient*> parseRecursively(const uint8_t* begin, const uint8_t* end,
bool emitViews, ParseClient* parseClient) {
+ if (begin == end) {
+ parseClient->error(
+ begin,
+ "Input buffer is empty. Begin and end cannot point to the same location.");
+ return {begin, nullptr};
+ }
+
const uint8_t* pos = begin;
MajorType type = static_cast<MajorType>(*pos & 0xE0);
diff --git a/tests/cppbor_test.cpp b/tests/cppbor_test.cpp
index ebdcc02..68778dc 100644
--- a/tests/cppbor_test.cpp
+++ b/tests/cppbor_test.cpp
@@ -14,6 +14,7 @@
* limitations under the License.
*/
+#include <cstdint>
#include <iomanip>
#include <sstream>
@@ -27,13 +28,11 @@ using namespace cppbor;
using namespace std;
using ::testing::_;
-using ::testing::AllOf;
using ::testing::ByRef;
using ::testing::InSequence;
using ::testing::IsNull;
using ::testing::NotNull;
using ::testing::Return;
-using ::testing::Truly;
using ::testing::Unused;
string hexDump(const string& str) {
@@ -254,7 +253,6 @@ TEST(MakeEntryTest, StdStrings) {
details::makeItem(s2)->toString()); // copy of const string
EXPECT_EQ("\x65\x68\x65\x6c\x6c\x6f"s,
details::makeItem(std::move(s1))->toString()); // move string
- EXPECT_EQ(0U, s1.size()); // Prove string was moved, not copied.
}
TEST(MakeEntryTest, StdStringViews) {
@@ -716,7 +714,7 @@ TEST(EqualityTest, ViewBstr) {
TEST(ConvertTest, Uint) {
unique_ptr<Item> item = details::makeItem(10);
- EXPECT_EQ(UINT, item->type());
+ EXPECT_EQ(cppbor::UINT, item->type());
EXPECT_NE(nullptr, item->asInt());
EXPECT_NE(nullptr, item->asUint());
EXPECT_EQ(nullptr, item->asNint());
@@ -803,7 +801,7 @@ TEST(ConvertTest, Bool) {
EXPECT_EQ(nullptr, item->asViewTstr());
EXPECT_EQ(nullptr, item->asViewBstr());
- EXPECT_EQ(BOOLEAN, item->asSimple()->simpleType());
+ EXPECT_EQ(cppbor::BOOLEAN, item->asSimple()->simpleType());
EXPECT_NE(nullptr, item->asSimple()->asBool());
EXPECT_EQ(nullptr, item->asSimple()->asNull());
@@ -944,7 +942,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());
@@ -965,7 +963,7 @@ TEST(ConvertTest, ViewBstr) {
TEST(CloningTest, Uint) {
Uint item(10);
auto clone = item.clone();
- EXPECT_EQ(clone->type(), UINT);
+ EXPECT_EQ(clone->type(), cppbor::UINT);
EXPECT_NE(clone->asUint(), nullptr);
EXPECT_EQ(item, *clone->asUint());
EXPECT_EQ(*clone->asUint(), Uint(10));
@@ -1025,7 +1023,7 @@ TEST(CloningTest, Bool) {
auto clone = item.clone();
EXPECT_EQ(clone->type(), SIMPLE);
EXPECT_NE(clone->asSimple(), nullptr);
- EXPECT_EQ(clone->asSimple()->simpleType(), BOOLEAN);
+ EXPECT_EQ(clone->asSimple()->simpleType(), cppbor::BOOLEAN);
EXPECT_NE(clone->asSimple()->asBool(), nullptr);
EXPECT_EQ(item, *clone->asSimple()->asBool());
EXPECT_EQ(*clone->asSimple()->asBool(), Bool(true));
@@ -1081,7 +1079,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 +1705,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) {