diff --git a/cruft/util/endian.hpp b/cruft/util/endian.hpp index e2de1f2b..b6de5abf 100644 --- a/cruft/util/endian.hpp +++ b/cruft/util/endian.hpp @@ -107,9 +107,9 @@ namespace cruft { > T readhe (u08 const *data) { - std::aligned_union_t buffer; - memcpy (reinterpret_cast (&buffer), data, sizeof (T)); - return *reinterpret_cast (&buffer); + T value; + memcpy (&value, data, sizeof (T)); + return value; } diff --git a/cruft/util/view.hpp b/cruft/util/view.hpp index 62405622..3140ff75 100644 --- a/cruft/util/view.hpp +++ b/cruft/util/view.hpp @@ -931,10 +931,14 @@ namespace cruft { if (unlikely (sizeof (ValueT) > buffer.size () * sizeof (WordT))) throw std::runtime_error ("insufficient data for extraction"); - std::aligned_storage_t bytes; - memcpy (&bytes, buffer.data (), sizeof (ValueT)); + WordT* const head = buffer.begin (); buffer = buffer.consume (sizeof (ValueT) / sizeof (WordT)); - return *reinterpret_cast (&bytes); + + // Don't use reinterpret_cast or other pointer schemes because it's quite easy to run into alignment issues. + // No one should really be using this for bulk reads anyway, so it shouldn't be a performance issue. + ValueT out; + memcpy (&out, head, sizeof (ValueT)); + return out; }