From patchwork Wed Jul 6 22:20:44 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Martin Sebor X-Patchwork-Id: 645607 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3rlFZr1698z9s9n for ; Thu, 7 Jul 2016 08:21:02 +1000 (AEST) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b=dD9JbW4P; dkim-atps=neutral DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:from :subject:to:references:message-id:date:mime-version:in-reply-to :content-type; q=dns; s=default; b=S0k+UMwEM9PTmTg6SWHyGkaIewO94 v4QApPffEjo7mvRh9RzHPuXKHH8ODzwNHVFLNja4/fm5KNzq/Z5MiQaTVPxoV797 UiiCJ9Ib/XNGaswxJprUZ1/PqvFyKIElihnI7UbgkMw9dE66GN6V7j2zXcCEp6T3 oa5SqFBNRO6F3k= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:from :subject:to:references:message-id:date:mime-version:in-reply-to :content-type; s=default; bh=ebSNggDnj1u/vdkFpbmYQo9PGSo=; b=dD9 JbW4PHmkApGQh+u39PhgTt0e3SRipGPs/XDB/8BIgssUy9oPfit89qh2d8BZM+RZ ts64eLdczVMUH6xhC7wu9Q5rZZR5ZDY08jtCEvGrOtuSwai3OuaV6+U+WltZk46X 1DbkYSoB3c7e2AhWv4ckw5lnf2Ut4/1C08q8g8bc= Received: (qmail 127302 invoked by alias); 6 Jul 2016 22:20:54 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Received: (qmail 122127 invoked by uid 89); 6 Jul 2016 22:20:53 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-2.5 required=5.0 tests=AWL, BAYES_00, FREEMAIL_FROM, RCVD_IN_DNSWL_LOW, SPF_PASS autolearn=ham version=3.3.2 spammy=*reinterpret_cast, ap, p1, sk:msebor@ X-HELO: mail-qk0-f182.google.com Received: from mail-qk0-f182.google.com (HELO mail-qk0-f182.google.com) (209.85.220.182) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES128-GCM-SHA256 encrypted) ESMTPS; Wed, 06 Jul 2016 22:20:49 +0000 Received: by mail-qk0-f182.google.com with SMTP id s126so252800qkh.2 for ; Wed, 06 Jul 2016 15:20:49 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:subject:to:references:message-id:date :user-agent:mime-version:in-reply-to; bh=bk+c21F2DPP3r87yRicJK84ABjSueyUhbHLoXmzxo/k=; b=QDgOCO/V8WGSajuB+8r2g7TkiXB22JjKEi4VDmhhN/CyP3vsIT+FH7VarLgkzW7q3X U5azaxzna8ythm3z2Fd9hgQaP2hvQbN/7477oUJ+HYxa/mcMwXHZKV+eLKz7tWpBlk84 x7imv8V/rFSxUuw7vDK+LRMrkzb6mAiVgfBuvVXwn9kpmnEpXgDAswDXoMW+srjOUqQr aODBORvCUOi0ra2l+Gv8pOvilvMHlhdnq+r/tru+CYTshJKYaUbwhGfaeu5f8Dw4sEHc SQrUvQXU8RwIRfIoFlZD3V22Yl6+8DrNkdgvVS0N7ojpnmRSD+29Pza8N059jipVYbmc 8WdA== X-Gm-Message-State: ALyK8tLxyrBUYTDJ8mbsKYg4WadkLqMtQqjONIOXtxVRVvq8LN7Kvae7Ou0EOZ0/dAWtmQ== X-Received: by 10.55.159.87 with SMTP id i84mr34816584qke.115.1467843647559; Wed, 06 Jul 2016 15:20:47 -0700 (PDT) Received: from [192.168.0.26] (97-124-166-240.hlrn.qwest.net. [97.124.166.240]) by smtp.gmail.com with ESMTPSA id 13sm302462qki.3.2016.07.06.15.20.45 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 06 Jul 2016 15:20:46 -0700 (PDT) From: Martin Sebor Subject: Re: [PATCH] c++/60760 - arithmetic on null pointers should not be allowed in constant expressions To: Jason Merrill , Gcc Patch List References: <573504DD.9040707@gmail.com> <574E0A41.9070204@gmail.com> <574F2CF7.1000800@gmail.com> <57684132.8010303@gmail.com> <1ff3aa18-6629-a291-872c-59fb25d91829@redhat.com> Message-ID: <577D843C.9010608@gmail.com> Date: Wed, 6 Jul 2016 16:20:44 -0600 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Thunderbird/38.1.0 MIME-Version: 1.0 In-Reply-To: <1ff3aa18-6629-a291-872c-59fb25d91829@redhat.com> X-IsSubscribed: yes On 06/23/2016 03:36 PM, Jason Merrill wrote: > On 06/20/2016 10:17 PM, Martin Sebor wrote: >> + && tree_int_cst_equal (lhs, null_pointer_node) >> + && !tree_int_cst_equal (rhs, integer_zero_node)) > > Not integer_zerop? > >> + "invalid conversion involving a null pointer"); > ... >> + "invalid conversion from %qT to %qT", > > The conversion isn't invalid, it just isn't a constant expression. (Sorry for the delay following up on this review. I got busy with something else.) I've adjusted the text of the diagnostics, though the first one is also issued for conversions that are invalid even outside constexpr, such as those that cast away constness, or those that cast to incomplete type. Without -fpermissve those are already diagnosed by this point but I'm not sure how much trouble to go to here to avoid diagnosing them again, or at all with -fpermissve. > For > the null pointer to pointer conversion, does this properly allow > conversion to void* or to base*? It didn't handle either but does now. Thank you for calling it out. Surprisingly, a regression run including libstdc++ didn't catch it. I've added tests to exercise it. > >> + if (integer_zerop (op)) > ... >> + else if (!integer_zerop (op)) > > The second test seems redundant. I have removed it. Martin PR c++/60760 - arithmetic on null pointers should not be allowed in constant expressions PR c++/71091 - constexpr reference bound to a null pointer dereference accepted gcc/cp/ChangeLog: 2016-07-06 Martin Sebor 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: 2016-07-06 Martin Sebor 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. diff --git a/gcc/cp/constexpr.c b/gcc/cp/constexpr.c index ba40435..83954d8 100644 --- a/gcc/cp/constexpr.c +++ b/gcc/cp/constexpr.c @@ -1811,6 +1811,13 @@ cxx_eval_binary_expression (const constexpr_ctx *ctx, tree t, || null_member_pointer_value_p (rhs))) r = constant_boolean_node (!is_code_eq, type); } + if (code == POINTER_PLUS_EXPR && !*non_constant_p + && integer_zerop (lhs) && !integer_zerop (rhs)) + { + if (!ctx->quiet) + error ("arithmetic involving a null pointer in %qE", lhs); + return t; + } if (r == NULL_TREE) r = fold_binary_loc (loc, code, type, lhs, rhs); @@ -2151,6 +2158,11 @@ cxx_eval_component_reference (const constexpr_ctx *ctx, tree t, tree whole = cxx_eval_constant_expression (ctx, orig_whole, lval, non_constant_p, overflow_p); + if (TREE_CODE (whole) == INDIRECT_REF + && integer_zerop (TREE_OPERAND (whole, 0)) + && !ctx->quiet) + error ("dereferencing a null pointer in %qE", orig_whole); + if (TREE_CODE (whole) == PTRMEM_CST) whole = cplus_expand_constant (whole); if (whole == orig_whole) @@ -2911,6 +2923,14 @@ cxx_eval_indirect_ref (const constexpr_ctx *ctx, tree t, if (*non_constant_p) return t; + if (integer_zerop (op0)) + { + if (!ctx->quiet) + error ("dereferencing a null pointer"); + *non_constant_p = true; + return t; + } + r = cxx_fold_indirect_ref (EXPR_LOCATION (t), TREE_TYPE (t), op0, &empty_base); if (r == NULL_TREE) @@ -3559,10 +3579,22 @@ cxx_eval_constant_expression (const constexpr_ctx *ctx, tree t, if (!flag_permissive || ctx->quiet) *overflow_p = true; } + + if (TREE_CODE (t) == INTEGER_CST + && TREE_CODE (TREE_TYPE (t)) == POINTER_TYPE + && !integer_zerop (t)) + { + if (!ctx->quiet) + error ("value %qE of type %qT is not a constant expression", + t, TREE_TYPE (t)); + *non_constant_p = true; + } + return t; } - switch (TREE_CODE (t)) + tree_code tcode = TREE_CODE (t); + switch (tcode) { case RESULT_DECL: if (lval) @@ -3973,7 +4005,6 @@ cxx_eval_constant_expression (const constexpr_ctx *ctx, tree t, case NOP_EXPR: case UNARY_PLUS_EXPR: { - enum tree_code tcode = TREE_CODE (t); tree oldop = TREE_OPERAND (t, 0); tree op = cxx_eval_constant_expression (ctx, oldop, @@ -3999,15 +4030,48 @@ cxx_eval_constant_expression (const constexpr_ctx *ctx, tree t, return t; } } - if (POINTER_TYPE_P (type) - && TREE_CODE (op) == INTEGER_CST - && !integer_zerop (op)) - { - if (!ctx->quiet) - error_at (EXPR_LOC_OR_LOC (t, input_location), - "reinterpret_cast from integer to pointer"); - *non_constant_p = true; - return t; + + if (POINTER_TYPE_P (type) && TREE_CODE (op) == INTEGER_CST) + { + if (integer_zerop (op)) + { + if (TREE_CODE (type) == REFERENCE_TYPE) + { + if (!ctx->quiet) + error_at (EXPR_LOC_OR_LOC (t, input_location), + "dereferencing a null pointer"); + *non_constant_p = true; + return t; + } + else if (TREE_CODE (TREE_TYPE (op)) == POINTER_TYPE) + { + tree from = TREE_TYPE (op); + + if (!can_convert (type, from, tf_none)) + { + if (!ctx->quiet) + error_at (EXPR_LOC_OR_LOC (t, input_location), + "conversion of %qT null pointer to %qT " + "is not a constant expression", + from, type); + *non_constant_p = true; + return t; + } + } + } + else + { + /* This detects for example: + reinterpret_cast(sizeof 0) + */ + if (!ctx->quiet) + error_at (EXPR_LOC_OR_LOC (t, input_location), + "%(%E)%> is not " + "a constant-expression", + type, op); + *non_constant_p = true; + return t; + } } if (op == oldop && tcode != UNARY_PLUS_EXPR) /* We didn't fold at the top so we could check for ptr-int diff --git a/gcc/testsuite/g++.dg/cpp0x/constexpr-cast.C b/gcc/testsuite/g++.dg/cpp0x/constexpr-cast.C new file mode 100644 index 0000000..8e11193 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp0x/constexpr-cast.C @@ -0,0 +1,24 @@ +// Test to verify that evaluating reinterpret_cast is diagnosed in +// constant expressions. +// { dg-do compile { target c++11 } } + +int i; + +// The following is accepted due to bug 49171. +constexpr void *q = reinterpret_cast(&i); // { dg-error "" "bug c++/49171" { xfail *-*-*-* } } + +constexpr void *r0 = reinterpret_cast(1); // { dg-error "not a constant expression" } +constexpr void *r1 = reinterpret_cast(sizeof 'x'); // { dg-error ".reinterpret_cast\\(1ul\\). is not a constant-expression" } + +template +constexpr bool f () +{ +#if __cplusplus > 201103L + T *p = reinterpret_cast(sizeof (T)); + return p; +#else + return *reinterpret_cast(sizeof (T)); +#endif +} + +constexpr bool b = f(); // { dg-error "not a constant expression" } diff --git a/gcc/testsuite/g++.dg/cpp0x/constexpr-nullptr-1.C b/gcc/testsuite/g++.dg/cpp0x/constexpr-nullptr-1.C index 420a04b..fbf1362 100644 --- a/gcc/testsuite/g++.dg/cpp0x/constexpr-nullptr-1.C +++ b/gcc/testsuite/g++.dg/cpp0x/constexpr-nullptr-1.C @@ -6,7 +6,7 @@ // c++/67376 on gcc-patches for additional background. // { dg-do compile { target c++11 } } -// { dg-options "-fdump-tree-optimized" } +// { dg-options "-fdump-tree-optimized -ftrack-macro-expansion=0" } // Runtime assert. Used for potentially invalid expressions. #define RA(e) ((e) ? (void)0 : __builtin_abort ()) diff --git a/gcc/testsuite/g++.dg/cpp0x/constexpr-nullptr-2.C b/gcc/testsuite/g++.dg/cpp0x/constexpr-nullptr-2.C new file mode 100644 index 0000000..4777b53 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp0x/constexpr-nullptr-2.C @@ -0,0 +1,303 @@ +// PR c++/60760 - arithmetic on null pointers should not be allowed +// in constant expressions +// PR c++/71091 - constexpr reference bound to a null pointer dereference +// accepted +// { dg-do compile { target c++11 } } +// { dg-additional-options "-Wno-pointer-arith" } + +// Generate a null poiinter. +constexpr int* null () { return 0; } + +// Test case from comment #0 in c++/60760. +namespace PR_60760_comment_0 { + +constexpr int* ptr = nullptr; +constexpr int* ptr2 = ptr + 1; // { dg-error "null pointer|not a constant" } + +} + +// Test case from comment #1 in c++/60760. +namespace PR_60760_comment_1 { + +constexpr int* ptr = nullptr; + +constexpr int zero = 0; +constexpr int* ptr2 = ptr + zero; // Adding zero is valid. +constexpr int* ptr3 = ptr - zero; // As is subtracting zero. + +} + +// Test case from c++/71091. +namespace PR_71091 { + +constexpr int *p = 0; +constexpr const int &r = *p; // { dg-error "dereferencing a null pointer" } + +} + +// Other test cases. +namespace C { + +struct S { int a, b[1]; } s; + +constexpr S *p0 = &s; +constexpr S *p1 = nullptr; +constexpr int *r0 = p1->b; // { dg-error "null pointer|constant expression" } + +// Adding and subtracting zero from and to a null pointer is valid. +constexpr S* r1 = p1 + 0; +constexpr S* r2 = r1 - 0; + +constexpr int zero = 0; + +constexpr S* r3 = r2 + zero; +constexpr S* r4 = r3 - zero; + +static_assert (r4 == nullptr, "r4 == nullptr"); + +constexpr const S *pcs = p0; +constexpr int d1 = pcs - p0; +constexpr int d2 = p0 - pcs; + +constexpr bool b = !p1 && !pcs; +} + +namespace D { + +struct A { int i; const A *pa1; const A *pa0; }; + +constexpr A a1 = { 0, 0, 0 }; +constexpr A a2 = { 1, &a1, 0 }; + +constexpr const A *pa2 = &a2; +constexpr int i0 = pa2->i; +constexpr int i1 = pa2->pa1->i; +constexpr int i2 = pa2->pa1->pa0->i; // { dg-error "null pointer|not a constant" } + +constexpr const A *pa3 = &*pa2->pa1->pa0; +constexpr const A *pa4 = pa2->pa1->pa0 + 1; // { dg-error "null pointer|not a constant" } + +constexpr const int *pi0 = &pa2->pa1->pa0->i; // { dg-error "null pointer|not a constant" } + +constexpr const A *pa5 = 0; +constexpr const int *pi1 = &pa5->i; // { dg-error "null pointer|not a constant" } + +} + + +namespace SimpleTests { + +constexpr int* p0 = nullptr; +constexpr int* q0 = p0; +constexpr int* r0 = null (); + +// Conversion to cv-qualified void* is valid. +constexpr void* pv0 = p0; +constexpr const void* pv1 = p0; +constexpr volatile void* pv2 = p0; +constexpr const volatile void* pv3 = p0; +constexpr void* pv4 = static_cast(p0); +constexpr const void* pv5 = static_cast(p0); + +// The following should be rejected but isn't because of bug c++/49171 +// - [C++0x][constexpr] Constant expressions support reinterpret_cast +constexpr void* pv6 = reinterpret_cast(p0); // { dg-error "" "bug c++/49171" { xfail *-*-* } } + +// Adding or subtracting zero from a null pointer is valid in C++. +constexpr int* p1 = p0 + 0; +constexpr int* p2 = p0 - 0; +constexpr int* p3 = 0 + p0; + +// While the text of the C++ standard still doesn't allow it, CWG +// issue 232 implies that dererencing a null pointer is intended +// to be permitted in contexts where the result isn't evaluated. +// For compatibility with C that should at a minimum include +// expressions like &*p that are valid there. +constexpr int* p4 = &*p0; +constexpr int* p5 = p0 + 1; // { dg-error "null pointer|not a constant" } +constexpr int* p6 = 1 + p0; // { dg-error "null pointer|not a constant" } +constexpr int* p7 = p0 - 1; // { dg-error "null pointer|not a constant" } +constexpr int* p8 = &p0 [0]; +constexpr int* p9 = &0 [p0]; + +constexpr int* p10 = null () + 2; // { dg-error "null pointer|not a constant" } +constexpr int* p11 = 3 + null (); // { dg-error "null pointer|not a constant" } +constexpr int* p12 = null () - 4; // { dg-error "null pointer|not a constant" } +constexpr int* p13 = &null ()[4]; // { dg-error "null pointer|not a constant" } +constexpr int* p14 = &3[null ()]; // { dg-error "null pointer|not a constant" } + +constexpr int* q1 = q0 + 0; +constexpr int* q2 = q0 - 0; +constexpr int* q3 = q0 + 1; // { dg-error "null pointer|not a constant" } +constexpr int* q4 = q0 + 2; // { dg-error "null pointer|not a constant" } +constexpr int* q5 = &q0 [0]; + +// Subtracting null pointers from one another is valid. +constexpr int i0 = p0 - (int*)0; +constexpr int i1 = p0 - static_cast(0); +constexpr int i2 = p0 - (int*)nullptr; +constexpr int i3 = p0 - static_cast(nullptr); +constexpr int i4 = p0 - p0; +constexpr int i5 = p0 - q0; +constexpr int i6 = p0 - r0; +constexpr int i7 = (int*)0 - p0; +constexpr int i8 = static_cast(0) - p0; +constexpr int i9 = (int*)nullptr - p0; +constexpr int i10 = static_cast(nullptr) - p0; +constexpr int i11 = q0 - p0; +constexpr int i12 = r0 - p0; + +} + +namespace IncompleteTypeTests { + +// The type must be complete. +struct X; +constexpr X *px0 = nullptr; +constexpr X *px1 = px0 + 0; // { dg-error "invalid use of incomplete type" } +constexpr X *px2 = px0 - 0; // { dg-error "invalid use of incomplete type" } +constexpr X *px3 = px0 - px0; // { dg-error "invalid use of incomplete type" } + +constexpr void *pv0 = px0; +constexpr void *pv1 = pv0; +constexpr const void *pv2 = pv0; +constexpr void *pv3 = pv2; // { dg-error "invalid conversion|not a constant expression" } +constexpr const void *pv4 = pv2; + +constexpr X *px4 = pv0; // { dg-error "invalid conversion|not a constant expression" } + +} + +namespace IndirectTests { + +struct S { int i, j; struct SA { struct SB { int *pi; } sb; } sa; }; + +constexpr S* ps = (S*)0; + +// Comparing null pointers is valid. +constexpr bool b0 = ps == ps; +constexpr bool b1 = ps != ps; +constexpr bool b2 = ps < ps; +constexpr bool b3 = ps <= ps; +constexpr bool b4 = ps > ps; +constexpr bool b5 = ps >= ps; + +constexpr bool b6 = ps == (S*)0; +constexpr bool b7 = ps != (S*)0; +constexpr bool b8 = ps < (S*)0; +constexpr bool b9 = ps <= (S*)0; +constexpr bool b10 = ps > (S*)0; +constexpr bool b11 = ps >= (S*)0; + +constexpr S* ps1 = ps; +constexpr S* ps2 = ps1; + +// The following aren't diagnosed due to a bug. +// constexpr int* pi0 = &((S*)0)->i; +// constexpr int* pi1 = &((S*)nullptr)->i; + +constexpr int* pj0 = &((S*)0)->j; // { dg-error "not a constant expression" } +constexpr int* pj1 = &((S*)nullptr)->j; // { dg-error "not a constant expression" } + +constexpr int* psi = &ps->i; // { dg-error "null pointer|not a constant" } +constexpr int* psj = &ps->j; // { dg-error "null pointer|not a constant" } + +constexpr int* ps1i = &ps1->i; // { dg-error "null pointer|not a constant" } +constexpr int* ps2i = &ps1->i; // { dg-error "null pointer|not a constant" } + +constexpr int* ps1j = &ps1->j; // { dg-error "null pointer|not a constant" } +constexpr int* ps2j = &ps1->j; // { dg-error "null pointer|not a constant" } + +} + +namespace BaseAndDerivedTests { + +struct A { }; +struct B: A { }; +struct C: B { }; +struct D: B, C { }; // { dg-warning "inaccessible" } + +constexpr D *pd0 = 0; +constexpr C *pc0 = 0; +constexpr B *pb0 = 0; + +constexpr A *pa0 = pb0; +constexpr A *pa1 = static_cast(pb0); +constexpr A *pa2 = pc0; +constexpr A *pa3 = pd0; // { dg-error "ambiguous base" } +constexpr A *pa4 = static_cast(pd0); // { dg-error "ambiguous base" } + +constexpr B *pb1 = pa0; // { dg-error "invalid conversion|not a constant expression" } +constexpr B *pb2 = static_cast(pa0); // { dg-error "not a constant expression" } + +constexpr C *pc1 = pa0; // { dg-error "invalid conversion|not a constant expression" } +constexpr D *pd1 = pa0; // { dg-error "ambiguous base|invalid conversion" } + +struct E: private A { }; + +constexpr E *pe0 = 0; +constexpr A *pa5 = pe0; // { dg-error "inaccessible base of" } + +struct VA { virtual ~VA (); }; +struct VB: virtual VA { }; +struct VC: virtual VA { }; +struct VD: VB, VC { }; + +constexpr VD *pvd0 = 0; +constexpr VC *pvc0 = 0; +constexpr VB *pvb0 = 0; + +constexpr VA *pva0 = pvb0; +constexpr VA *pva1 = pvc0; +constexpr VA *pva2 = pvd0; + +constexpr VB *pvb1 = pva0; // { dg-error "invalid conversion|cannot convert from pointer to base class" } + +} + +namespace FunctionTests { + +typedef void Func (); + +// Arithmetic on member function pointers is diagnosed with -Wpointer-arith. +// With constexpr, only zero may be added or subtracted. +constexpr Func *pf0 = 0; +constexpr Func *pf1 = pf0 + 0; // triggers -Wpointer-arith +constexpr Func *pf2 = pf0 - 0; // triggers -Wpointer-arith +constexpr Func *pf3 = 0 + pf0; // triggers -Wpointer-arith +constexpr Func *pf4 = pf0 + 1; // { dg-error "null pointer|not a constant" } +constexpr Func *pf5 = 2 + pf0; // { dg-error "null pointer|not a constant" } +constexpr Func *pf6 = pf0 - 3; // { dg-error "null pointer|not a constant" } + +struct S; +typedef void (S::*MemFuncPtr)(); + +// Arithmetic on member function pointers is rejected with a hard error. +constexpr MemFuncPtr pmf0 = nullptr; +constexpr MemFuncPtr pmf1 = pmf0 + 0; // { dg-error "invalid operands" } +constexpr MemFuncPtr pmf2 = 0 + pmf0; // { dg-error "invalid operands" } +constexpr MemFuncPtr pmf3 = pmf0 + 1; // { dg-error "invalid operands" } +constexpr MemFuncPtr pmf4 = 1 + pmf0; // { dg-error "invalid operands" } +constexpr MemFuncPtr pmf5 = pmf0 - 1; // { dg-error "invalid operands" } + +} + +namespace ConversionTest { + +struct A { + int *p; +}; + +constexpr const int* f (const int *p) { return p; } + +void f () +{ + static_assert (!f (0), "f (a.p)"); + static_assert (!f (nullptr), "f (a.p)"); + + constexpr A a = A (); + + static_assert (!f (a.p), "f (a.p)"); +} + +} diff --git a/gcc/testsuite/g++.dg/cpp1y/constexpr-sfinae.C b/gcc/testsuite/g++.dg/cpp1y/constexpr-sfinae.C index a83d7f4..4a7deb8 100644 --- a/gcc/testsuite/g++.dg/cpp1y/constexpr-sfinae.C +++ b/gcc/testsuite/g++.dg/cpp1y/constexpr-sfinae.C @@ -90,22 +90,28 @@ namespace NullPointerArithmetic { constexpr int i = 0; constexpr const int* a[] = { 0, &i }; -// Well-defined core constant expressoons involving null pointers. +// Well-defined core constant expressions involving null pointers. constexpr __PTRDIFF_TYPE__ d00 = a [0] - a [0]; constexpr __PTRDIFF_TYPE__ d11 = a [1] - a [1]; -// Undefined core constant expressoons involving null pointers. +// Undefined core constant expressions involving null pointers. // constexpr __PTRDIFF_TYPE__ d01 = a [0] - a [1]; // constexpr __PTRDIFF_TYPE__ d10 = a [1] - a [0]; -constexpr bool nullptr_sub_0 (int i, int j) { return 1 + a [i != 0] - a [j]; } +// Valid when i == j. +constexpr bool +nullptr_sub_0 (bool i, bool j) { return 1 + a [!i] - a [!j]; } -constexpr bool nullptr_sub_1 (int i, int j) { return 1 + a [i == 0] - a [j]; } +// Valid when i != j. +constexpr bool +nullptr_sub_1 (bool i, bool j) { return 1 + a [i] - a [!j]; } -template +// Selected when I == 0. +template constexpr int f (int (*)[nullptr_sub_0 (I, 0)] = 0) { return 0; } -template +// Selected when I != 0. +template constexpr int f (int (*)[nullptr_sub_1 (I, 0)] = 0) { return 1; } constexpr int n0 = f<0>(); diff --git a/gcc/testsuite/g++.dg/ubsan/pr63956.C b/gcc/testsuite/g++.dg/ubsan/pr63956.C index 25db8a4..ac01fa4 100644 --- a/gcc/testsuite/g++.dg/ubsan/pr63956.C +++ b/gcc/testsuite/g++.dg/ubsan/pr63956.C @@ -92,7 +92,7 @@ constexpr int fn6 (const int &a, int b) { if (b != 2) - b = a; // { dg-error "is not a constant expression" } + b = a; return b; } @@ -106,7 +106,7 @@ fn7 (const int *a, int b) constexpr int n1 = 7; constexpr int n2 = fn7 (&n1, 5); -constexpr int n3 = fn7 ((const int *) 0, 8); +constexpr int n3 = fn7 ((const int *) 0, 8); // { dg-error "null pointer" } constexpr int fn8 (int i)