4 ms·
You are still casting one pointer type into another which can result in unaligned access. If you need to change byte orders, you should use library to achieve
by cbmuser 5y ago
You are still casting one pointer type into another which can result in unaligned access.
If you need to change byte orders, you should use library to achieve that.
- nly 5y agoBoost.Endian is the library here and this code is safe because the big_uint32_t type has an alignment requirement of 1 byte. This is why ubsan is silent and not even injecting a check in to the compiled code. You can check the alignment constraints with static_assert (something else you can't do in standard C): https://gcc.godbolt.org/z/KTcf9ax6r https://gcc.godbolt.org/z/KTcf9ax6r
- kevin_thibedeau 5y agoC11 has static_assert: https://gcc.godbolt.org/z/E3bGc95o3 https://gcc.godbolt.org/z/E3bGc95o3 Is also has _Generic() so you can roll up a family of endianness conversion functions and safely change types without blowing up somewhere else with a hardcoded conversion routine.