Bug 82874 - excessively large array index accepted in constexpr context
Summary: excessively large array index accepted in constexpr context
Status: UNCONFIRMED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 8.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: accepts-invalid
Depends on: 70151
Blocks: constexpr
  Show dependency treegraph
 
Reported: 2017-11-07 02:45 UTC by Martin Sebor
Modified: 2023-01-25 16:34 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Martin Sebor 2017-11-07 02:45:13 UTC
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 ();
                      ^
Comment 1 Martin Sebor 2017-11-07 03:47:38 UTC
See also bug 82877 for another similar problem.