diff options
author | Steven Moreland <smoreland@google.com> | 2019-05-08 11:58:10 -0700 |
---|---|---|
committer | android-build-merger <android-build-merger@google.com> | 2019-05-08 11:58:10 -0700 |
commit | 083f08aacbf012cc88200db893c3d6e2e27328c8 (patch) | |
tree | 21b8fe8da907d20479168931aecdb72aed95be17 | |
parent | 3ab619c3a6c9d476f549da793745f30f23151ba3 (diff) | |
parent | 0cea857b15b40a0de345925da0287e9d1026180d (diff) | |
download | libhidl-083f08aacbf012cc88200db893c3d6e2e27328c8.tar.gz |
Zero-init HIDL core types (all)
am: 0cea857b15
Change-Id: I61f9499a4912b759287034708b318877838d2858
-rw-r--r-- | base/HidlSupport.cpp | 20 | ||||
-rw-r--r-- | base/include/hidl/HidlInternal.h | 1 | ||||
-rw-r--r-- | base/include/hidl/HidlSupport.h | 23 |
3 files changed, 26 insertions, 18 deletions
diff --git a/base/HidlSupport.cpp b/base/HidlSupport.cpp index 4de7f7c..c0888f6 100644 --- a/base/HidlSupport.cpp +++ b/base/HidlSupport.cpp @@ -36,27 +36,28 @@ bool debuggable() { } // 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; } // move constructor. -hidl_handle::hidl_handle(hidl_handle &&other) { +hidl_handle::hidl_handle(hidl_handle&& other) : hidl_handle() { mOwnsHandle = false; *this = std::move(other); } @@ -137,10 +138,11 @@ void hidl_handle::freeHandle() { 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 6c0d8df..4b29276 100644 --- a/base/include/hidl/HidlInternal.h +++ b/base/include/hidl/HidlInternal.h @@ -43,6 +43,7 @@ template<typename T> struct hidl_pointer { hidl_pointer() : _pad(0) { + static_assert(sizeof(*this) == 8, "wrong size"); } hidl_pointer(T* ptr) : mPointer(ptr) { diff --git a/base/include/hidl/HidlSupport.h b/base/include/hidl/HidlSupport.h index 6807860..ad1293f 100644 --- a/base/include/hidl/HidlSupport.h +++ b/base/include/hidl/HidlSupport.h @@ -282,29 +282,32 @@ private: template<typename T> struct hidl_vec { - hidl_vec() - : mBuffer(NULL), - 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; } hidl_vec(const hidl_vec<T> &other) : hidl_vec() { *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) - : mOwnsBuffer(true) { + 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) { @@ -774,7 +777,9 @@ private: // 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()); |