[Bug c++/82877] negative array index accepted in a pointer difference expression in constexpr context

msebor at gcc dot gnu.org gcc-bugzilla@gcc.gnu.org
Thu Feb 7 21:17:00 GMT 2019


https://gcc.gnu.org/bugzilla/show_bug.cgi?id=82877

--- Comment #2 from Martin Sebor <msebor at gcc dot gnu.org> ---
Clang issues the expected errors, as does ICC:

$ clang -S pr82877.C
pr82877.C:1:15: error: constexpr function never produces a constant expression
      [-Winvalid-constexpr]
constexpr int f ()
              ^
pr82877.C:6:21: note: cannot refer to element -1 of array of 1 element in a
      constant expression
  return &s.a[0] - &s.a[-1];   // undefined, should be rejected
                    ^
pr82877.C:6:21: warning: array index -1 is before the beginning of the array
      [-Warray-bounds]
  return &s.a[0] - &s.a[-1];   // undefined, should be rejected
                    ^   ~~
pr82877.C:3:14: note: array 'a' declared here
  struct S { int a[1]; };
             ^
pr82877.C:9:15: error: constexpr variable 'i' must be initialized by a constant
      expression
constexpr int i = f ();
              ^   ~~~~
pr82877.C:6:21: note: cannot refer to element -1 of array of 1 element in a
      constant expression
  return &s.a[0] - &s.a[-1];   // undefined, should be rejected
                    ^
pr82877.C:9:19: note: in call to 'f()'
constexpr int i = f ();
                  ^
1 warning and 2 errors generated.


More information about the Gcc-bugs mailing list