Uploaded image for project: 'MariaDB Server'
  1. MariaDB Server
  2. MDEV-17702

fix unaligned access UB in sint4korr() and similar functions

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Closed (View Workflow)
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: 5.5, 10.0, 10.1, 10.2, 10.3, 10.4
    • Fix Version/s: 10.2.24
    • Component/s: Compiling, Server
    • Labels:
      None

      Description

      Exaple UB:

      #define sint2korr(A)	(int16) (*((int16 *) (A)))
      

      (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 Mäkelä and me:

      #define saint3korr(A) ((int32)((char)(A[2]) << 16 | (int32)(uchar)A[1] << 8 | (int32)(uchar)A[0]))
      

      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.

        Attachments

          Activity

            People

            Assignee:
            kevg Eugene Kosov
            Reporter:
            kevg Eugene Kosov
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved:

                Git Integration