view: use a temporary buffer for cruft::read

This commit is contained in:
Danny Robson 2019-01-18 17:20:18 +11:00
parent b9c0716c66
commit a94ca358a5

View File

@ -701,24 +701,17 @@ namespace cruft {
ValueT ValueT
read (view<WordT*> &buffer) read (view<WordT*> &buffer)
{ {
// we disable the class-memaccess warning so that we can memcpy into // We're going to use memcpy which requires that the type is
// types that we know are safe but the compiler will complain about. // trivially copyable.
// this occurs commonly with oddly packed structures, eg anything static_assert (std::is_trivially_copyable_v<ValueT>);
// that uses gnu::packed
#if defined(COMPILER_GCC)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wclass-memaccess"
#endif
if (unlikely (sizeof (ValueT) > buffer.size () * sizeof (WordT))) if (unlikely (sizeof (ValueT) > buffer.size () * sizeof (WordT)))
throw std::runtime_error ("insufficient data for extraction"); throw std::runtime_error ("insufficient data for extraction");
ValueT res; std::aligned_storage_t<sizeof(ValueT),alignof(ValueT)> bytes;
memcpy (&res, buffer.data (), sizeof (ValueT)); memcpy (&bytes, buffer.data (), sizeof (ValueT));
buffer = buffer.consume (sizeof (ValueT) / sizeof (WordT)); buffer = buffer.consume (sizeof (ValueT) / sizeof (WordT));
return res; return *reinterpret_cast<ValueT const*> (&bytes);
#if defined(COMPILER_GCC)
#pragma GCC diagnostic pop
#endif
} }