]> gcc.gnu.org Git - gcc.git/commitdiff
c++: Reject ordered comparison of null pointers [PR99701]
authorMarek Polacek <polacek@redhat.com>
Fri, 16 Jul 2021 19:58:01 +0000 (15:58 -0400)
committerMarek Polacek <polacek@redhat.com>
Tue, 27 Jul 2021 15:38:59 +0000 (11:38 -0400)
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.

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/cpp2a/spaceship-err7.C: New test.
* g++.dg/expr/ptr-comp4.C: New test.

libstdc++-v3/ChangeLog:

* testsuite/20_util/tuple/comparison_operators/overloaded.cc:
Move a line...
* testsuite/20_util/tuple/comparison_operators/overloaded2.cc:
...here.  New test.

gcc/cp/cp-gimplify.c
gcc/cp/typeck.c
gcc/testsuite/g++.dg/cpp0x/nullptr11.C
gcc/testsuite/g++.dg/cpp0x/nullptr46.C
gcc/testsuite/g++.dg/cpp2a/spaceship-err7.C [new file with mode: 0644]
gcc/testsuite/g++.dg/expr/ptr-comp4.C [new file with mode: 0644]
libstdc++-v3/testsuite/20_util/tuple/comparison_operators/overloaded.cc
libstdc++-v3/testsuite/20_util/tuple/comparison_operators/overloaded2.cc [new file with mode: 0644]

index ff0bff771df69fa2092ea63571e6f45537bd7c7e..0520fa45b9113343b68c4dfe6cef0043f060b056 100644 (file)
@@ -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:
index a483e1f988d49d0858544b7f0137e7393726b3f2..738e69a0440f2a13136fe86508a6f9145d855ce2 100644 (file)
@@ -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;
index f81f0c3c1c8f9715094ffb9c899f7241a4328789..b8bc682f2eac5ae06b397fff18856bc2e4e09cdc 100644 (file)
@@ -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);
 }
index 1514cee3c3bbf9e48e4a1472a3daa2b96cc9ce77..6c08eaa4d8fe170e17a26baefea4c79f25f26772 100644 (file)
@@ -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/cpp2a/spaceship-err7.C b/gcc/testsuite/g++.dg/cpp2a/spaceship-err7.C
new file mode 100644 (file)
index 0000000..bc30923
--- /dev/null
@@ -0,0 +1,14 @@
+// PR c++/99701
+// { dg-do compile { target c++20 } }
+
+#include <compare>
+
+decltype(nullptr) foo ();
+
+auto b0 = nullptr <=> nullptr; // { dg-error "ordered comparison" }
+auto b1 = 0 <=> nullptr; // { dg-error "ordered comparison" }
+auto b2 = nullptr <=> 0; // { dg-error "ordered comparison" }
+auto b3 = foo () <=> 0; // { dg-error "ordered comparison" }
+auto b4 = 0 <=> foo (); // { dg-error "ordered comparison" }
+auto b5 = foo () <=> nullptr; // { dg-error "ordered comparison" }
+auto b6 = nullptr <=> foo (); // { 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 (file)
index 0000000..da49518
--- /dev/null
@@ -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" }
index a25c374a9325d79478aad9a33b12f47bb10b6292..ef90b6b5b73b21086474b32bb440dce7d2eb26f0 100644 (file)
@@ -48,4 +48,3 @@ 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;
diff --git a/libstdc++-v3/testsuite/20_util/tuple/comparison_operators/overloaded2.cc b/libstdc++-v3/testsuite/20_util/tuple/comparison_operators/overloaded2.cc
new file mode 100644 (file)
index 0000000..fc3118f
--- /dev/null
@@ -0,0 +1,52 @@
+// { dg-do compile { target c++11 } }
+
+// Copyright (C) 2014-2021 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library.  This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 3, or (at your option)
+// any later version.
+
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License along
+// with this library; see the file COPYING3.  If not see
+// <http://www.gnu.org/licenses/>.
+
+#include <tuple>
+
+// A type that is contextually convertible to bool but cannot be used with
+// the usual logical operators, and/or/not.
+struct TwistedLogic {
+  bool value;
+
+  explicit operator bool() const noexcept { return value; }
+};
+
+template<typename T>
+bool operator&&(const T&, TwistedLogic) = delete;
+
+template<typename T>
+bool operator&&(TwistedLogic, const T&) = delete;
+
+template<typename T>
+bool operator||(const T&, TwistedLogic) = delete;
+
+template<typename T>
+bool operator||(TwistedLogic, const T&) = delete;
+
+bool operator!(TwistedLogic) noexcept = delete;
+
+struct Compares {};
+
+TwistedLogic operator==(const Compares&, const Compares&) { return {true}; }
+TwistedLogic operator<(const Compares&, const Compares&) { return {false}; }
+
+auto a = std::make_tuple(nullptr, Compares{}, 2, 'U');
+auto b = a < a;
+
+// { dg-error "ordered comparison" "" { target *-*-* } 0 }
This page took 0.085198 seconds and 5 git commands to generate.