Bug 111242

Summary: Out of bounds pointer arithmetic for dynamic allocated arrays not caught in constexpr
Product: gcc Reporter: Jeremy R. <llvm>
Component: c++Assignee: Not yet assigned to anyone <unassigned>
Status: NEW ---    
Severity: normal CC: daniel.kruegler, mpolacek, webrown.cpp
Priority: P3 Keywords: accepts-invalid
Version: unknown   
Target Milestone: ---   
See Also: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=97230
Host: Target:
Build: Known to work:
Known to fail: Last reconfirmed: 2023-08-30 00:00:00
Bug Depends on:    
Bug Blocks: 55004    

Description Jeremy R. 2023-08-30 16:51:25 UTC
The following UB is not caught by gcc:

#include <vector>

constexpr auto tester() {
  std::vector<int> v = {1, 2, 3};
  auto p = &v[v.size() + 1];
  return &v[0] == p;
}

static_assert(!tester());


https://godbolt.org/z/Y3ofcb1b1
Comment 1 Andrew Pinski 2023-08-30 17:07:15 UTC
Confirmed reduced testcase:
```

#define size 3
constexpr auto tester() {
  int *v = new int[size];
  auto p = &v[size+1];
  bool t = &v[0] == p;
  delete[] v;
  return t;
}

static_assert(!tester());

#if 0
constexpr auto tester1() {
  int v[size];
  auto p = &v[size+1];
  bool t = &v[0] == p;
  return t;
}
static_assert(!tester1());
#endif
```

GCC does catch the static allocated one; just not the dynamic allocated one.
Comment 2 Andrew Pinski 2023-08-30 17:07:27 UTC
.