Bug 71091 - constexpr reference bound to a null pointer dereference accepted
Summary: constexpr reference bound to a null pointer dereference accepted
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 7.0
: P3 normal
Target Milestone: ---
Assignee: Martin Sebor
URL:
Keywords: accepts-invalid
Depends on:
Blocks: constexpr
  Show dependency treegraph
 
Reported: 2016-05-12 21:33 UTC by Martin Sebor
Modified: 2016-07-30 22:38 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail: 4.8.3, 4.9.3, 5.3.0, 6.1.0
Last reconfirmed: 2016-05-12 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-05-12 21:33:33 UTC
All released versions of GCC fail to reject definitions of constexpr variables initialized by dereferencing a null pointer, creating a dangling reference.  The initialization is invalid and should be rejected.

$ cat xx.cpp && /build/gcc-trunk-svn/gcc/xgcc -B /build/gcc-trunk-svn/gcc -S -Wall -Wextra -Wpedantic -o/dev/null xx.cpp
constexpr int *p = 0;
constexpr int &r = *p;
static_assert (&r != 0, "");
xx.cpp:3:19: warning: the compiler can assume that the address of ‘r’ will never be NULL [-Waddress]
 static_assert (&r != 0, "");
                ~~~^~~~
xx.cpp:3:1: error: static assertion failed
 static_assert (&r != 0, "");
 ^~~~~~~~~~~~~
Comment 1 Martin Sebor 2016-05-12 21:45:15 UTC
This is fixed by my patch for bug 60760 (to be resubmitted for 7.0).
Comment 2 Martin Sebor 2016-07-30 22:37:28 UTC
Author: msebor
Date: Sat Jul 30 22:36:56 2016
New Revision: 238909

URL: https://gcc.gnu.org/viewcvs?rev=238909&root=gcc&view=rev
Log:
PR c++/60760 - arithmetic on null pointers should not be allowed in constant
PR c++/71091 - constexpr reference bound to a null pointer dereference

gcc/cp/ChangeLog:

	PR c++/60760
	PR c++/71091
        * constexpr.c (cxx_eval_binary_expression): Reject invalid expressions
        involving null pointers.
        (cxx_eval_component_reference): Reject null pointer dereferences.
        (cxx_eval_indirect_ref): Reject indirecting through null pointers.
        (cxx_eval_constant_expression): Reject invalid expressions involving
        null pointers.

gcc/testsuite/ChangeLog:

	PR c++/60760
	PR c++/71091
	* g++.dg/cpp0x/constexpr-cast.C: New test.
        * g++.dg/cpp0x/constexpr-nullptr-2.C: New test.
        * g++.dg/cpp1y/constexpr-sfinae.C: Correct.
        * g++.dg/ubsan/pr63956.C: Correct.


Added:
    trunk/gcc/testsuite/g++.dg/cpp0x/constexpr-cast.C
    trunk/gcc/testsuite/g++.dg/cpp0x/constexpr-nullptr-2.C
Modified:
    trunk/gcc/cp/ChangeLog
    trunk/gcc/cp/constexpr.c
    trunk/gcc/testsuite/ChangeLog
    trunk/gcc/testsuite/g++.dg/cpp1y/constexpr-sfinae.C
    trunk/gcc/testsuite/g++.dg/ubsan/pr63956.C
Comment 3 Martin Sebor 2016-07-30 22:38:38 UTC
Fixed by r238909.