Bug 111242 - Out of bounds pointer arithmetic for dynamic allocated arrays not caught in constexpr
Summary: Out of bounds pointer arithmetic for dynamic allocated arrays not caught in c...
Status: NEW
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: unknown
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: accepts-invalid
Depends on:
Blocks: constexpr
  Show dependency treegraph
 
Reported: 2023-08-30 16:51 UTC by Jeremy R.
Modified: 2023-08-31 12:08 UTC (History)
3 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2023-08-30 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
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
.