From patchwork Fri Jul 16 21:36:13 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marek Polacek X-Patchwork-Id: 1506289 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.a=rsa-sha256 header.s=default header.b=wnLIiEOF; dkim-atps=neutral Received: from sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4GRPhG73JMz9sW8 for ; Sat, 17 Jul 2021 07:36:45 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id D1C2839ACC10 for ; Fri, 16 Jul 2021 21:36:42 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org D1C2839ACC10 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1626471402; bh=EvCaTXMG8srn2hshRn0EpyuXvuhXXH9FusltzAbuUOY=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=wnLIiEOFfgZ3Jub1AKTaIZUiJqZBFMsKpZ5BUzRjzz6sgoBovjLp9tsm1AIec5P8j a05UiLfg/ZadBy05J2rfTSwGW+aSzKYeNikJyq95Fm9Zh+jq6z+T7RGATxC63kxq4Q zpl654Er2pnt9v42I5qAXnwM3RqG+WCV3xW4SjlQ= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [216.205.24.124]) by sourceware.org (Postfix) with ESMTP id C14F93848407 for ; Fri, 16 Jul 2021 21:36:19 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org C14F93848407 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-155-ZhVKrdjAO3aRzDMr11A7OA-1; Fri, 16 Jul 2021 17:36:18 -0400 X-MC-Unique: ZhVKrdjAO3aRzDMr11A7OA-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 34E1C80292A for ; Fri, 16 Jul 2021 21:36:17 +0000 (UTC) Received: from pdp-11.hsd1.ma.comcast.net (ovpn-114-138.rdu2.redhat.com [10.10.114.138]) by smtp.corp.redhat.com (Postfix) with ESMTP id B87DC27060; Fri, 16 Jul 2021 21:36:16 +0000 (UTC) To: Jason Merrill , GCC Patches Subject: [PATCH] c++: Reject ordered comparison of null pointers [PR99701] Date: Fri, 16 Jul 2021 17:36:13 -0400 Message-Id: <20210716213613.1386368-1-polacek@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-14.4 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Marek Polacek via Gcc-patches From: Marek Polacek Reply-To: Marek Polacek Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" When implementing DR 1512 in r11-467 I neglected to reject ordered comparison of two null pointers, like nullptr < nullptr. This patch fixes that omission. Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk? DR 1512 PR c++/99701 gcc/cp/ChangeLog: * cp-gimplify.c (cp_fold): Remove {LE,LT,GE,GT_EXPR} from a switch. * typeck.c (cp_build_binary_op): Reject ordered comparison of two null pointers. gcc/testsuite/ChangeLog: * g++.dg/cpp0x/nullptr11.C: Remove invalid tests. * g++.dg/cpp0x/nullptr46.C: Add dg-error. * g++.dg/expr/ptr-comp4.C: New test. libstdc++-v3/ChangeLog: * testsuite/20_util/tuple/comparison_operators/overloaded.cc: Add dg-error. --- gcc/cp/cp-gimplify.c | 4 ---- gcc/cp/typeck.c | 15 +++---------- gcc/testsuite/g++.dg/cpp0x/nullptr11.C | 16 -------------- gcc/testsuite/g++.dg/cpp0x/nullptr46.C | 3 ++- gcc/testsuite/g++.dg/expr/ptr-comp4.C | 21 +++++++++++++++++++ .../tuple/comparison_operators/overloaded.cc | 2 ++ 6 files changed, 28 insertions(+), 33 deletions(-) create mode 100644 gcc/testsuite/g++.dg/expr/ptr-comp4.C base-commit: 94ba897be8b59ef5926eed4c77fd53812fb20add diff --git a/gcc/cp/cp-gimplify.c b/gcc/cp/cp-gimplify.c index ff0bff771df..0520fa45b91 100644 --- a/gcc/cp/cp-gimplify.c +++ b/gcc/cp/cp-gimplify.c @@ -2433,13 +2433,9 @@ cp_fold (tree x) switch (code) { case EQ_EXPR: - case LE_EXPR: - case GE_EXPR: x = constant_boolean_node (true, TREE_TYPE (x)); break; case NE_EXPR: - case LT_EXPR: - case GT_EXPR: x = constant_boolean_node (false, TREE_TYPE (x)); break; default: diff --git a/gcc/cp/typeck.c b/gcc/cp/typeck.c index a483e1f988d..738e69a0440 100644 --- a/gcc/cp/typeck.c +++ b/gcc/cp/typeck.c @@ -5483,7 +5483,9 @@ cp_build_binary_op (const op_location_t &location, result_type = composite_pointer_type (location, type0, type1, op0, op1, CPO_COMPARISON, complain); - else if (code0 == POINTER_TYPE && null_ptr_cst_p (orig_op1)) + else if ((code0 == POINTER_TYPE && null_ptr_cst_p (orig_op1)) + || (code1 == POINTER_TYPE && null_ptr_cst_p (orig_op0)) + || (null_ptr_cst_p (orig_op0) && null_ptr_cst_p (orig_op1))) { /* Core Issue 1512 made this ill-formed. */ if (complain & tf_error) @@ -5491,17 +5493,6 @@ cp_build_binary_op (const op_location_t &location, "integer zero (%qT and %qT)", type0, type1); return error_mark_node; } - else if (code1 == POINTER_TYPE && null_ptr_cst_p (orig_op0)) - { - /* Core Issue 1512 made this ill-formed. */ - if (complain & tf_error) - error_at (location, "ordered comparison of pointer with " - "integer zero (%qT and %qT)", type0, type1); - return error_mark_node; - } - else if (null_ptr_cst_p (orig_op0) && null_ptr_cst_p (orig_op1)) - /* One of the operands must be of nullptr_t type. */ - result_type = TREE_TYPE (nullptr_node); else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE) { result_type = type0; diff --git a/gcc/testsuite/g++.dg/cpp0x/nullptr11.C b/gcc/testsuite/g++.dg/cpp0x/nullptr11.C index f81f0c3c1c8..b8bc682f2ea 100644 --- a/gcc/testsuite/g++.dg/cpp0x/nullptr11.C +++ b/gcc/testsuite/g++.dg/cpp0x/nullptr11.C @@ -9,31 +9,15 @@ void fun() { assert_true(nullptr == nullptr); assert_false(nullptr != nullptr); - assert_false(nullptr < nullptr); - assert_false(nullptr > nullptr); - assert_true(nullptr <= nullptr); - assert_true(nullptr >= nullptr); decltype(nullptr) mynull = 0; assert_true(mynull == nullptr); assert_false(mynull != nullptr); - assert_false(mynull < nullptr); - assert_false(mynull > nullptr); - assert_true(mynull <= nullptr); - assert_true(mynull >= nullptr); assert_true(nullptr == mynull); assert_false(nullptr != mynull); - assert_false(nullptr < mynull); - assert_false(nullptr > mynull); - assert_true(nullptr <= mynull); - assert_true(nullptr >= mynull); assert_true(mynull == mynull); assert_false(mynull != mynull); - assert_false(mynull < mynull); - assert_false(mynull > mynull); - assert_true(mynull <= mynull); - assert_true(mynull >= mynull); } diff --git a/gcc/testsuite/g++.dg/cpp0x/nullptr46.C b/gcc/testsuite/g++.dg/cpp0x/nullptr46.C index 1514cee3c3b..6c08eaa4d8f 100644 --- a/gcc/testsuite/g++.dg/cpp0x/nullptr46.C +++ b/gcc/testsuite/g++.dg/cpp0x/nullptr46.C @@ -7,5 +7,6 @@ decltype(nullptr) foo (); bool bar () { - return foo () > nullptr || foo () < nullptr; + return foo () > nullptr // { dg-error "ordered comparison" } + || foo () < nullptr; // { dg-error "ordered comparison" } } diff --git a/gcc/testsuite/g++.dg/expr/ptr-comp4.C b/gcc/testsuite/g++.dg/expr/ptr-comp4.C new file mode 100644 index 00000000000..da4951806c1 --- /dev/null +++ b/gcc/testsuite/g++.dg/expr/ptr-comp4.C @@ -0,0 +1,21 @@ +// PR c++/99701 +// DR 1512 +// { dg-do compile { target c++11 } } + +/* Relational comparisons between null pointer constants and pointers are + ill-formed. */ + +decltype(nullptr) foo (); + +auto lt = nullptr < nullptr; // { dg-error "ordered comparison" } +auto gt = nullptr > nullptr; // { dg-error "ordered comparison" } +auto le = nullptr <= nullptr; // { dg-error "ordered comparison" } +auto ge = nullptr >= nullptr; // { dg-error "ordered comparison" } +auto eq = nullptr == nullptr; +auto ne = nullptr != nullptr; +auto a1 = nullptr > 0; // { dg-error "ordered comparison" } +auto a2 = 0 < nullptr; // { dg-error "ordered comparison" } +auto a3 = foo () > 0; // { dg-error "ordered comparison" } +auto a4 = 0 < foo (); // { dg-error "ordered comparison" } +auto a5 = 0 <= foo (); // { dg-error "ordered comparison" } +auto a6 = foo () >= 0; // { dg-error "ordered comparison" } diff --git a/libstdc++-v3/testsuite/20_util/tuple/comparison_operators/overloaded.cc b/libstdc++-v3/testsuite/20_util/tuple/comparison_operators/overloaded.cc index a25c374a932..25bfdfa00f4 100644 --- a/libstdc++-v3/testsuite/20_util/tuple/comparison_operators/overloaded.cc +++ b/libstdc++-v3/testsuite/20_util/tuple/comparison_operators/overloaded.cc @@ -49,3 +49,5 @@ TwistedLogic operator<(const Compares&, const Compares&) { return {false}; } auto a = std::make_tuple(nullptr, Compares{}, 2, 'U'); auto b = a == a; auto c = a < a; + +// { dg-error "ordered comparison" "" { target *-*-* } 0 }