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