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

    <tr>
        <th>Summary</th>
        <td>
            Discarded array accesses are optimized out before -fsanitize=array-bounds
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

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

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

<pre>
    In principle, (I would assume) sanitizer failures are meant to be treated as program side effects and should not be optimized out. That is, if a program has an integer overflow and the computation can be discarded, UBSan should still perform the computation and check for overflow because the program is still UB.

However, `-fsanitize=array-bounds` seems to not quite achieve this. If the program contains `(void)a[i]`, the result is bounds-checked according to the rules for `a + i` (where the condition is `i <= N`) instead of `a[i]` (where the condition is `i < N`)

See this godbolt. Note how the check is different between the two, and that the program manages to run both print statements.
https://godbolt.org/z/f3P3oaxoa
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJyMk8GO4zYMhp9GvhAJHNlx4oMPmwmCzmVRYLsPQEt0zNaWUolOuvP0hZSkM9OiwF4SwDa_n_pEYox8dkSd2h7U9ljgIqMPncUr255c0Xv7o3t1cAnsDF8mUvoFlN6_ws0vkwWMcZlJ6RYiOhZ-owAD8rQEioCBYCZ0AuKhJ5BAKJSK4BL8OeAMkS0BDQMZiYDOQhwz13lJFf4iPPMbWfCLrOG3EQU4ph54APyHMmIqBnZCZwrgrxSGyd8yUEYC4-fLIijsHRh0iWw5GgyWbIJ9P3xD94yOwtMEFwqDD_N_yhPSjGT-gMF_SOrJ4BIpf_7siuOD9f2wVuVRlV_uv7_4G10pZJNNuRqe5lR1xBDwx6r3i7NRNSVEojkme8nHnwsLAZqR6ZqSOK7hdfgUabwTZBcTWOn91bNVukW1PbDaHvPDl1wQKC5Tcgn3sFU-U7obY3yw7M4pNX-5TBTzYVVTIih9AE6tKb2_jRToYchZzn44ZzOo6kVVR_iaM1tgF4XQgh8y5r2hn-A8IR8dfqO7ATh72_tJ1vDVC8Hob3dOviGOYHkYKJBL0yQ3Ipdfy80nEffxQPmkcEaHZ8rSw-Kg9zLm6ReIgkIzOYmP6xxFLlFVX5Q-KX16duLDWenTm9Knofq18viXx8J2lW2rFgvqNrtNtd2WTVsVY0cG902J2G-1Hvpe17jb7fTO7gxh3ei-4E6Xui7rTbvZb_abat009Wao-52htilNX6q6pBl5Wk_TdU7ZBce4ULdv60oXE_Y0xbzcWju6QX6ptE67HrpUs-qXc1R1OXGU-E4Rlom643NLIE9mmg6K8bHan5YTehp8IPjfcS6WMHX_MsYyLv3a-FnpU0p-_K0uwf9ORpQ-5X6j0qd8nr8DAAD__1XFkZY">