<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/90848>90848</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            clang 18: UBsan expects 4-byte alingment for 1- or 3-byte memcpy
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            clang
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          igsilya
      </td>
    </tr>
</table>

<pre>
    We have the following code: https://github.com/openvswitch/ovs/blob/bd8e9f48f180800292c10e12f26824833f18506a/lib/hash.c#L29-L51
It gets an array of bytes and calculates a hash.  We cast the pointer from `void *` to `uint32_t *`, but it is never directly accessed as `uint32_t`, it is always casted back before any reads are done, except for the `memcpy` call.

We expect that `memcpy` will be able to copy anything regardless of the alignment, especially since the copy size is *always* less than 4 bytes.  However, UBsan emits an error:

```
lib/hash.c:46:22: runtime error: load of misaligned address 0x507000000065 for type 'const uint32_t *' (aka 'const unsigned int *'), which requires 4 byte alignment
0x507000000065: note: pointer points here
 73 62 2e 73 6f 63 6b  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00 00
             ^
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJyUVMuO6zYM_RplQ0wg0484Cy9yexu0wN0WsyxombbVq0iuJCfjfn0hOdPOFEWBGkIUPfg451CkEPRkmTtRfxH11wOtcXa-01PQZqND74ate2WY6c4QZ4bRGeMe2k6g3MCivMAc4xJEeRF4FXiddJzX_qjcTeDVLWzv4aGjmtPqHgRee-P6NA0tn8eqHYtWtlLiGVUhucARmxartizHoq1lQwKvRieDmcJ8VALLb3h--VYXQn4V8vJzhIljALJA3tMGboR-i5x2BlBk1GooLyE7AHhlUBRiBrM4bSN7GL27gWjk3ekBBF5EIyG6tLNqG0v8NT53Bf4A_RpBR9ABLN_Zw6A9q2g2IKU4BB6AwkfTp9luQuZBW8gZ8AA9qe_Q8-g8A9kNPNMQgDzD4CwnK35TvEQYnc8Ji0be-KaWLWWoyJjjTsP--8rAbwurBI7i58sPbQz0DNQbTtiUW7YUM85JS88T-cFwCInAFImMnuyNbcxZhIWVJmM2CNqqvRKyh6D_4IRL4GWHJvAC2U-cyUK1i3EE-Mk9ElvJ2y9fAlngm951Y--dT_XzAUnibB95-akCykvViPKCmIrPrzbqG__lBIyjIWG46ZAhJDmGwaeM5FstT3L_mnrndFsYBJ6UsyHCJ7HxBAJb-k4fzm3YPWr7fkfgOUF6zFrN4Pn3VXsOT9gfOMwoPsdPyVoX8xN6r8M8B5jZ824CpxIaBOT8Z4SmhKYHkPLfx_8_kE-G4eMn6h__IcNh6MrhXJ7pwF1xKqqmxuaEh7kbxr6sy3N5GrnhAVWN_YnGSlHdIqpzc9AdSqxkLbFArEo8Dn0l23NVNHzimolEJflG2hyNud-Ozk8HHcLK3Vm2VXsw1LMJuTkhKkN2EoipT_ku3X_p1ymIShodYvjbQ9TRcJevQ9Emhp81l19HgOrlXR87JX1yLRQv4DyU-9H-cA6rN91_9LcU8Dm9LN79xioKvOb8U6vLEP4MAAD__yjYpMY">