-
Notifications
You must be signed in to change notification settings - Fork 47
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix cast-align error #240
fix cast-align error #240
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Eh, neither the old nor the current version works on Big Endian systems. Not that there are many of those around, but still, we should write portable code.
The EbmlCrc32 element always stores data in Little Endian format. Therefore a Big Endian processor cannot just memcpy Little Endian data into a memory region & then access it as an integer.
The correct code to use would take this into account. For example (untested):
auto buf = reinterpret_cast<const std::uint8_t *>(input);
std::uint32_t tmp{};
for (int idx = 3; idx >= 0; --idx)
tmp = (tmp << 8) | buf[idx];
crc ^= tmp;
That looks like it would still throw a cast-align error. big endian systems are available on cfarm. |
No, it wouldn't. Alignment only matters when accessing structures bigger than a single byte in one instruction, e.g. reading a 32 bit integer. Accessing single bytes one by one is always OK and never a problem (though with performance penalties, obviously). |
Non x86 platforms are more strict about alignment and thus throw a Wcast-align warning on these two reinterpret_casts. Use memcpy to avoid. Signed-off-by: Rosen Penev <[email protected]>
@mbunkus without this PR on a big endian machine:
same as with. test_crc needs work looks like. |
The memcpy is equivalent to the cast that was there before and is the proper way to get around such cast issues. If the code was wrong before, it's wrong now. This is what we use in VLC and some byte swap when endianness needs to be changed. libavutil seems to have the concept of unaligned integers (that depends on compiler support). |
@neheb The Crc test only tests creation & reading on the same architecture. It doesn't test creating on BE & reading on LE & vice versa. Endian issues only show up if both sides are different, or if the test compares created values against fixed, known-good values. In other words, the current test is insufficient for detecting Endian issues. |
Exactly. It was wrong before for Big Endian machines.
Yes. The |
Se do have |
anything to do here? |
Looking at this further, I can't say I like this. Makes more sense to byteswap on big endian. |
Non x86 platforms are more strict about alignment and thus throw a Wcast-align warning on these two reinterpret_casts. Use memcpy to avoid.