Bug 60760 - arithmetic on null pointers should not be allowed in constant expressions
Summary: arithmetic on null pointers should not be allowed in constant expressions
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 4.9.0
: P3 normal
Target Milestone: 7.0
Assignee: Martin Sebor
URL:
Keywords: accepts-invalid
Depends on:
Blocks: constexpr
  Show dependency treegraph
 
Reported: 2014-04-04 14:53 UTC by Jonathan Wakely
Modified: 2016-07-30 22:38 UTC (History)
4 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail: 4.8.2, 4.9.1, 5.3.0, 6.1.0
Last reconfirmed: 2016-03-08 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Jonathan Wakely 2014-04-04 14:53:23 UTC
G++ should not compile this:

constexpr int* ptr = nullptr;
constexpr int* ptr2 = ptr + 1;
Comment 1 Daniel Krügler 2014-04-04 17:43:28 UTC
Agreed. But it should still be possible to add the additive identity (The core language drafted to support this):

constexpr int* ptr = nullptr;
constexpr int x = 0;
constexpr int* ptr2 = ptr + x;
Comment 2 Jonathan Wakely 2014-04-04 17:53:56 UTC
Yes, we rely on that in initializer_list<>::end(), and clang rejects that.

I discussed that case with Jason and we think [expr.add] could be clarified - do you have an existing issue number related to that?
Comment 3 Daniel Krügler 2014-04-04 18:08:03 UTC
(In reply to Jonathan Wakely from comment #2)
> Yes, we rely on that in initializer_list<>::end(), and clang rejects that.

<nod>

> I discussed that case with Jason and we think [expr.add] could be clarified
> - do you have an existing issue number related to that?

I haven't - I myself found the existing wording always clear in that regard.
Comment 4 Martin Sebor 2016-03-09 03:42:16 UTC
Reconfirming with today's trunk of 6.0.  Bug 70151 is related.
Comment 5 Martin Sebor 2016-03-10 20:28:57 UTC
I'm up to my ears in constexpr so I might as well try to fix this too.
Comment 6 Martin Sebor 2016-03-14 21:31:27 UTC
Patch posted for review:
https://gcc.gnu.org/ml/gcc-patches/2016-03/msg00796.html
Comment 7 Martin Sebor 2016-05-12 15:28:21 UTC
This part of the patch wasn't accepted for 6.1.0.  I'll resubmit it for 7.0.
Comment 8 Martin Sebor 2016-05-12 22:37:51 UTC
Patch resubmitted for review:
https://gcc.gnu.org/ml/gcc-patches/2016-05/msg00935.html
Comment 9 Martin Sebor 2016-07-30 22:37:27 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 10 Martin Sebor 2016-07-30 22:38:06 UTC
Fixed by r238909.