DO NOT MERGE - Merge pie-platform-release (PPRL.190705.004) into master

Bug: 136196576
Change-Id: Ib10a613e02514ef569c6207a83f4a9144f067865
diff --git a/base/HidlSupport.cpp b/base/HidlSupport.cpp
index 58afa69..c15d326 100644
--- a/base/HidlSupport.cpp
+++ b/base/HidlSupport.cpp
@@ -36,21 +36,22 @@
 }  // namespace details
 
 hidl_handle::hidl_handle() {
-    mHandle = nullptr;
-    mOwnsHandle = false;
+    memset(this, 0, sizeof(*this));
+    // mHandle = nullptr;
+    // mOwnsHandle = false;
 }
 
 hidl_handle::~hidl_handle() {
     freeHandle();
 }
 
-hidl_handle::hidl_handle(const native_handle_t *handle) {
+hidl_handle::hidl_handle(const native_handle_t* handle) : hidl_handle() {
     mHandle = handle;
     mOwnsHandle = false;
 }
 
 // copy constructor.
-hidl_handle::hidl_handle(const hidl_handle &other) {
+hidl_handle::hidl_handle(const hidl_handle& other) : hidl_handle() {
     mOwnsHandle = false;
     *this = other;
 }
@@ -137,10 +138,11 @@
 
 static const char *const kEmptyString = "";
 
-hidl_string::hidl_string()
-    : mBuffer(kEmptyString),
-      mSize(0),
-      mOwnsBuffer(false) {
+hidl_string::hidl_string() {
+    memset(this, 0, sizeof(*this));
+    // mSize is zero
+    // mOwnsBuffer is false
+    mBuffer = kEmptyString;
 }
 
 hidl_string::~hidl_string() {
diff --git a/base/include/hidl/HidlInternal.h b/base/include/hidl/HidlInternal.h
index 6377c46..0b80cd4 100644
--- a/base/include/hidl/HidlInternal.h
+++ b/base/include/hidl/HidlInternal.h
@@ -68,6 +68,7 @@
 struct hidl_pointer {
     hidl_pointer()
         : _pad(0) {
+        static_assert(sizeof(*this) == 8, "wrong size");
     }
     hidl_pointer(T* ptr) : hidl_pointer() { mPointer = ptr; }
     hidl_pointer(const hidl_pointer<T>& other) : hidl_pointer() { mPointer = other.mPointer; }
diff --git a/base/include/hidl/HidlSupport.h b/base/include/hidl/HidlSupport.h
index 44691e6..adf86a7 100644
--- a/base/include/hidl/HidlSupport.h
+++ b/base/include/hidl/HidlSupport.h
@@ -328,11 +328,15 @@
 struct hidl_vec {
     using value_type = T;
 
-    hidl_vec()
-        : mBuffer(nullptr),
-          mSize(0),
-          mOwnsBuffer(true) {
+    hidl_vec() {
         static_assert(hidl_vec<T>::kOffsetOfBuffer == 0, "wrong offset");
+
+        memset(this, 0, sizeof(*this));
+        // mSize is 0
+        // mBuffer is nullptr
+
+        // this is for consistency with the original implementation
+        mOwnsBuffer = true;
     }
 
     // Note, does not initialize primitive types.
@@ -342,12 +346,23 @@
         *this = other;
     }
 
-    hidl_vec(hidl_vec<T> &&other) noexcept
-    : mOwnsBuffer(false) {
+    hidl_vec(hidl_vec<T> &&other) noexcept : hidl_vec() {
         *this = std::move(other);
     }
 
-    hidl_vec(const std::initializer_list<T> list) : hidl_vec() { *this = list; }
+    hidl_vec(const std::initializer_list<T> list) : hidl_vec() {
+        if (list.size() > UINT32_MAX) {
+            details::logAlwaysFatal("hidl_vec can't hold more than 2^32 elements.");
+        }
+        mSize = static_cast<uint32_t>(list.size());
+        mBuffer = new T[mSize]();
+        mOwnsBuffer = true;
+
+        size_t idx = 0;
+        for (auto it = list.begin(); it != list.end(); ++it) {
+            mBuffer[idx++] = *it;
+        }
+    }
 
     hidl_vec(const std::vector<T> &other) : hidl_vec() {
         *this = other;
@@ -357,7 +372,7 @@
               typename = typename std::enable_if<std::is_convertible<
                   typename std::iterator_traits<InputIterator>::iterator_category,
                   std::input_iterator_tag>::value>::type>
-    hidl_vec(InputIterator first, InputIterator last) : mOwnsBuffer(true) {
+    hidl_vec(InputIterator first, InputIterator last) : hidl_vec() {
         auto size = std::distance(first, last);
         if (size > static_cast<int64_t>(UINT32_MAX)) {
             details::logAlwaysFatal("hidl_vec can't hold more than 2^32 elements.");
@@ -366,7 +381,8 @@
             details::logAlwaysFatal("size can't be negative.");
         }
         mSize = static_cast<uint32_t>(size);
-        mBuffer = new T[mSize];
+        mBuffer = new T[mSize]();
+        mOwnsBuffer = true;
 
         size_t idx = 0;
         for (; first != last; ++first) {
@@ -505,7 +521,7 @@
         if (size > UINT32_MAX) {
             details::logAlwaysFatal("hidl_vec can't hold more than 2^32 elements.");
         }
-        T *newBuffer = new T[size];
+        T* newBuffer = new T[size]();
 
         for (size_t i = 0; i < std::min(static_cast<uint32_t>(size), mSize); ++i) {
             newBuffer[i] = std::move(mBuffer[i]);
@@ -581,7 +597,7 @@
         mSize = static_cast<uint32_t>(size);
         mOwnsBuffer = true;
         if (mSize > 0) {
-            mBuffer = new T[size];
+            mBuffer = new T[size]();
             for (size_t i = 0; i < size; ++i) {
                 mBuffer[i] = data[i];
             }
@@ -861,7 +877,9 @@
 // Version functions
 struct hidl_version {
 public:
-    constexpr hidl_version(uint16_t major, uint16_t minor) : mMajor(major), mMinor(minor) {}
+    constexpr hidl_version(uint16_t major, uint16_t minor) : mMajor(major), mMinor(minor) {
+        static_assert(sizeof(*this) == 4, "wrong size");
+    }
 
     bool operator==(const hidl_version& other) const {
         return (mMajor == other.get_major() && mMinor == other.get_minor());