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

            Bug ID: 82876
           Summary: out-of-bounds pointer offset silently accepted in
                    constexpr context
           Product: gcc
           Version: 8.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: msebor at gcc dot gnu.org
  Target Milestone: ---

The following invalid test case is required to be diagnosed by C++ because the
pointer expression overflows.  However, it is silently accepted by G++.  Other
compilers such as Clang 5.0 and ICC 17 reject it with an error as expected.

$ cat t.C && gcc -S -Wall -Wextra t.C
constexpr int f ()
{
  struct S { int a[1]; } s = { 0 };

  int *p = s.a + __SIZE_MAX__ / 4 + 1;

  return *p;
}

constexpr int i = f ();


For comparison, ICC 17 and 18 issues the following error:

error: expression must have a constant value
  constexpr int i = f ();
                    ^
note: cannot access position -1 in array of 1 elements
    int *p = s.a + __SIZE_MAX__ / 4 + 1;
                 ^
compilation aborted for <source> (code 2)
Compiler exited with result code 2

Reply via email to