[MDEV-17702] fix unaligned access UB in sint4korr() and similar functions Created: 2018-11-13 Updated: 2019-05-06 Resolved: 2019-05-06 |
|
| Status: | Closed |
| Project: | MariaDB Server |
| Component/s: | Compiling, Server |
| Affects Version/s: | 5.5, 10.0, 10.1, 10.2, 10.3, 10.4 |
| Fix Version/s: | 10.2.24 |
| Type: | Bug | Priority: | Major |
| Reporter: | Eugene Kosov (Inactive) | Assignee: | Eugene Kosov (Inactive) |
| Resolution: | Fixed | Votes: | 0 |
| Labels: | None | ||
| Description |
|
Exaple UB:
(int16*) casts uchar* to a bigger aligned which is UB. There is not knows observable harm but this stuff makes A LOT of noise for UBSAN builds. This reason is alone enough to fix it. One way to fix it is by using memcpy(). GCC, Clang and MSVC optimizes it away in release builds. There is also a room for micro optimizations while preserving correctness. Here is an example from marko and me:
Probably it's also a good idea to replace macros with functions. This will bring a bit of type safety and will allow to put breakpoints on these functions. |
| Comments |
| Comment by Marko Mäkelä [ 2018-11-14 ] |
|
Yes, recent GCC would optimize away memcpy() on x86 and amd64, but not on all platforms that I tried on http://godbolt.org/. For older server versions that may be built on an older compiler, we probably should avoid memcpy(). Or we should put some compiler-version check into the code. I agree that it would be a good idea to define the macros with inline functions. |
| Comment by Sergey Vojtovich [ 2019-03-20 ] |
|
Comments in the PR. |
| Comment by Eugene Kosov (Inactive) [ 2019-05-06 ] |
|
Issue was fixed in https://github.com/MariaDB/server/commit/bcc1359223c254e7649383c0993b6d27d5002c5d |