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

            Bug ID: 69638
           Summary: array out of bounds access accepted in constexpr
                    function invocation
           Product: gcc
           Version: 6.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: ---

GCC silently accepts the following invalid definitions and invocations of the
constexpr functions.  I believe the program is invalid/ill-formed (and requires
a diagnostic) because the constexpr functions would have undefined behavior and
thus do not produce core constant expressions.

The same functions are rejected as expected when A is replaced with int. 
(Clang rejects them either way.)

$ cat t.c && /build/gcc-trunk/gcc/xgcc -B /build/gcc-trunk/gcc -S -Wall -Wextra
-Wpedantic -std=c++14 -xc++ t.c
struct A {
  constexpr A (int = 0) { }
  constexpr operator int () const { return 0; }
};

constexpr int foo () {
  A a [3] = { 1, 2, 3 };
  return a [99];
}

struct B { unsigned b: foo () + 1; };

#if __GNUG__ > 5
  // GCC 5 fails on this because of either 69509 or 69516
constexpr int bar (int n) {
  A a [n] = { 1, 2, 3 };
  return a [99];
}

struct C {
    unsigned c: bar (1) + 1;
};

#endif
t.c: In function ‘constexpr int bar(int)’:
t.c:16:9: warning: ISO C++ forbids variable length array ‘a’ [-Wvla]
   A a [n] = { 1, 2, 3 };
         ^
$

Reply via email to