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

            Bug ID: 82874
           Summary: invalid array index 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 call to function f() in the test case below has undefined behavior because
the array index is out of bounds.  As a result, it is required to be diagnosed
(according to [expr.const] of C++ 14).  However, G++ silently accepts it.

See also bug 82872 for a problem in a related area.

#include <stddef.h>

struct S { int i, a[1]; };

constexpr int f ()
{
  return offsetof (S, a[__PTRDIFF_MAX__ / 2]);
}

constexpr int i = f ();


In contrast to the above, the following (also undefined) program is diagnosed
as expected:

#include <stddef.h>

constexpr int f ()
{
  struct S { int a[1]; };

  return offsetof (S, a[__PTRDIFF_MAX__]);
}

constexpr int i = f ();
t.C:10:21:   in constexpr expansion of ‘f()’
t.C:10:22: error: overflow in constant expression [-fpermissive]
 constexpr int i = f ();
                      ^

Reply via email to