Bug 70151 - forming out of bounds constexpr pointer accepted
Summary: forming out of bounds constexpr pointer accepted
Status: NEW
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 6.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: accepts-invalid
: 103297 (view as bug list)
Depends on:
Blocks: constexpr 82874 82876 82877
  Show dependency treegraph
 
Reported: 2016-03-09 03:19 UTC by Martin Sebor
Modified: 2023-01-25 16:34 UTC (History)
3 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail: 4.9.3, 5.3.0, 6.0
Last reconfirmed: 2016-07-11 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Martin Sebor 2016-03-09 03:19:45 UTC
In the following program, the initializer of the constexpr pointer p has undefined behavior.  As a result, the initializer is not a /core constant expression/ which makes the program ill-formed and subject to a requirement for a diagnostic.  However, GCC 6.0 and all supported versions before it that implement constexpr silently accept it.

$ cat z.c && /home/msebor/build/gcc-trunk-svn/gcc/xg++ -B/home/msebor/build/gcc-trunk-svn/gcc -S z.c
constexpr int a [] = { 1 };
constexpr const int *p = a - 1;

The EDG eccp front end rejects the program with the following output:

"z.c", line 2: warning: pointer points outside of underlying object
  constexpr const int *p = a - 1;
                             ^

"z.c", line 2: error: expression must have a constant value
  constexpr const int *p = a - 1;
                             ^

"z.c", line 2: warning: variable "p" was declared but never referenced
  constexpr const int *p = a - 1;
                       ^

1 error detected in the compilation of "z.c".
Comment 1 Drea Pinski 2016-07-11 04:59:09 UTC
Confirmed.
Comment 2 Jonathan Wakely 2021-11-17 11:57:08 UTC
*** Bug 103297 has been marked as a duplicate of this bug. ***