-
Notifications
You must be signed in to change notification settings - Fork 265
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
Fixed misaligned memory access flagged by UBSan #2800
Conversation
Use memcpy to copy correctly even for unaligned memory. This was already done for some functions here, but not all. Also took the oppurtunity to remove a bunch of seemingly obsolete/commented code.
@WardF @DennisHeimbigner here's another small PR hoisted from #2692 |
Hah, this looks very much like what I'm working on in #2799. Thanks! |
Good sign that running UBSan on a regular basis in CI might have some value. |
@dopplershift No doubt. |
@seanm Indeed, although sadly it can be deceptively tricky. In this case, running this change/the change submitted in #2799 fixes the warnings under Linux and MacOS, but results in a crash on Windows/Visual Studio. So, it'll take a little bit of doing to figure out what is going on. But it's absolutely for the best we eliminate undefined behavior, so thank you very much! |
That said, I'll be picking up with this in the AM; thanks again for the PR's, we'll get these sorted. |
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.
I would have thought that using 2 byte copies would be faster than
using memcpy:
something like this:
unsigned char* op = (unsigned char*)dst;
unsigned char* ip = (unsigned char*)src;
for(i=0;i<nnsizeof(uint16_t);i+=sizeof(uint16_t)) {
op[i] = ip[i+1]; / instead of swap2 */
op[i+1] = ip[i];
}
memcpy() is highly optimized by compilers. Let me see if I can create a demo on godbolt... |
@DennisHeimbigner so here's the current code vs my proposal: https://godbolt.org/z/nrY8fnhaW It's less CPU instructions, so hopefully faster. Your proposal, if I'm not mistaken, won't work when |
@WardF so all was ok on Windows afterall? |
There was a Visual Studio update, and the issue went away. A little more testing convinced me that the issue wasn't being introduced by the PR, it was environmental or something in VS, and either way, I wasn't introducing something we'd have to chase down later. So yeah, it looks like it was ok. Thanks! |
Use memcpy to copy correctly even for unaligned memory. This was already done for some functions here, but not all.
Also took the oppurtunity to remove a bunch of seemingly obsolete/commented code.