diff --git a/include/utils/Flattenable.h b/include/utils/Flattenable.h index 882a8b249..c37ac60c7 100644 --- a/include/utils/Flattenable.h +++ b/include/utils/Flattenable.h @@ -19,10 +19,13 @@ #include +#include #include #include #include +#include + namespace android { @@ -60,14 +63,18 @@ public: // write a POD structure template static void write(void*& buffer, size_t& size, const T& value) { - *static_cast(buffer) = value; + static_assert(std::is_trivially_copyable::value, + "Cannot flatten a non-trivially-copyable type"); + memcpy(buffer, &value, sizeof(T)); advance(buffer, size, sizeof(T)); } // read a POD structure template static void read(void const*& buffer, size_t& size, T& value) { - value = *static_cast(buffer); + static_assert(std::is_trivially_copyable::value, + "Cannot unflatten a non-trivially-copyable type"); + memcpy(&value, buffer, sizeof(T)); advance(buffer, size, sizeof(T)); } };