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

    <tr>
        <th>Summary</th>
        <td>
            Undefined Behavior sanitizer bounds check off by 1 for std::array
        </td>
    </tr>

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

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

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

<pre>
    When trying to understand #54360 I found that the problem seems simpler than I presented before.
I think this bug should be caught by the bounds check in undefined behavior sanitizer, not by the address sanitizer.
More than that the bug is caught if I go 2 past the end of the array instead of just one.

A even more minimal example can be found below.

Things to note that is that when using an std::array it doesn't catch the index out of bounds when accesses array[size], but it does catch the same issue when using a c array, even though it seems to recognize that these are effectively the same type.

```
// compile with
// ../llvm-project/build/bin/clang++ -Wall  -Wextra -O3 -g  -std=c++20
// -fsanitize=undefined,local-bounds -fno-omit-frame-pointer  test.cpp  -o basic

/* version
../llvm-project/build/bin/clang++ --version
clang version 14.0.0 (https://github.com/llvm/llvm-project.git
92ccb8cc50f5098ca4b5642bcfaf7b78761bd892) Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: ../llvm-project/build/bin
*/

#include <array>
#include <string>

int main(int argc, char *argv[]) {
  if (argc != 2) {
    return -1;
  }
  int test = std::stoi(argv[1]);
  std::array<int, 10> array = {};
  // does not trigger anything at when 10 is passed in, triggers the following
  // error when 11 is passed in
  /*
/usr/lib/gcc/x86_64-linux-gnu/11/../../../../include/c++/11/array:57:33: runtime error: index 11 out of bounds for type 'const std::__array_traits<int, 10>::_Type' (aka 'const int[10]')
    #0 0x42d99c  (/home/wheatman/test/basic+0x42d99c)
    #1 0x42d87e  (/home/wheatman/test/basic+0x42d87e)
    #2 0x7fb65aa65c86  (/lib/x86_64-linux-gnu/libc.so.6+0x21c86) (BuildId: 71f0f3074a929e519e85f6a5c03a7d1fd976bfe4)
    #3 0x403699  (/home/wheatman/test/basic+0x403699)

SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior /usr/lib/gcc/x86_64-linux-gnu/11/../../../../include/c++/11/array:57:33 in

  */
  const int array[10] = {0};
  // correctly starts triggering the following error starting at 10
/*
test.cpp:46:10: runtime error: index 10 out of bounds for type 'const int[10]'
    #0 0x42d912  (/home/wheatman/test/basic+0x42d912)
    #1 0x7f516e9eec86  (/lib/x86_64-linux-gnu/libc.so.6+0x21c86) (BuildId: 71f0f3074a929e519e85f6a5c03a7d1fd976bfe4)
    #2 0x403699  (/home/wheatman/test/basic+0x403699)

SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior test.cpp:46:10 in
*/

  return array[test];
}
``` 

I made a new issue since the last one reference the constexpr which I now think may have been the wrong issue to focus on.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzNV8tu4zYU_Rp7Q8iQKOu18CKZNEAWgwKdGRRdBRRFyZxIpEFSidOv7yEly7Eng0E2bQGDtsXL-zg896FaN6-7P_dCEWdepeqI02RUjTDWMdWQFU2zbZrH5IG0Gs-J2zOHRZCD0XUvBmKFGCyxcjj0wvhtBdmDEVYoJxpSi1YbsVnFd6v45gH7Uj351ZJ67Ijd67H3QoSzsds7Ur8G5bW3ZQnfC_5EpAoetVIFfXv2LLUhlinp5N_CrOgnovRylDUNjNvz_mz7M9yY3FtC8B7Akdm0bOF4pwklB2YnAYGAdTupNYa9whXrBAsPv48Q0uoU2rTeEPEMKAdva5BKDqwn4sg8NjCjfKATjLXo9cvF0a9AprMefgQjJifhXPh-8fczWn89UGJds0pv8Jl9cqTRwqoVLRyMOL4PDktAdiR6dN7ZGc-gh3EOfISdQlpltxYorbI7j2MN8VnfG1WWDdBn7SguPCF8VoGDIWyH2-z2XsFECsRiBNedgoEFdeuxBLRtK7iTz6J_Pdtwr4dLPFd5PH-mv_QeH8L1cJBA9EW6_cXGZoO175-HCOz8Dv34W4-yb_y3BEL3v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