[libcxx-commits] [libcxx] f7345de - [libc++] Use addressof in forward_list.

Mark de Wever via libcxx-commits libcxx-commits at lists.llvm.org
Thu Nov 11 09:47:21 PST 2021


Author: Mark de Wever
Date: 2021-11-11T18:47:15+01:00
New Revision: f7345de64fd2d6c5528e940a8aed1a9b95b46183

URL: https://github.com/llvm/llvm-project/commit/f7345de64fd2d6c5528e940a8aed1a9b95b46183
DIFF: https://github.com/llvm/llvm-project/commit/f7345de64fd2d6c5528e940a8aed1a9b95b46183.diff

LOG: [libc++] Use addressof in forward_list.

This addresses the usage of `operator&` in `<forward_list>`.

(Note there are still more headers with the same issue.)

Reviewed By: #libc, Quuxplusone, ldionne

Differential Revision: https://reviews.llvm.org/D112660

Added: 
    libcxx/test/std/containers/sequences/forwardlist/forwardlist.ops/merge_lvalue.addressof.compile.pass.cpp
    libcxx/test/std/containers/sequences/forwardlist/forwardlist.ops/merge_lvalue_pred.addressof.compile.pass.cpp
    libcxx/test/std/containers/sequences/forwardlist/forwardlist.ops/merge_rvalue.addressof.compile.pass.cpp
    libcxx/test/std/containers/sequences/forwardlist/forwardlist.ops/merge_rvalue_pred.addressof.compile.pass.cpp

Modified: 
    libcxx/include/forward_list

Removed: 
    


################################################################################
diff  --git a/libcxx/include/forward_list b/libcxx/include/forward_list
index 717dc904e3028..41059a58784ea 100644
--- a/libcxx/include/forward_list
+++ b/libcxx/include/forward_list
@@ -1587,7 +1587,7 @@ template <class _Compare>
 void
 forward_list<_Tp, _Alloc>::merge(forward_list& __x, _Compare __comp)
 {
-    if (this != &__x)
+    if (this != _VSTD::addressof(__x))
     {
         base::__before_begin()->__next_ = __merge(base::__before_begin()->__next_,
                                                     __x.__before_begin()->__next_,

diff  --git a/libcxx/test/std/containers/sequences/forwardlist/forwardlist.ops/merge_lvalue.addressof.compile.pass.cpp b/libcxx/test/std/containers/sequences/forwardlist/forwardlist.ops/merge_lvalue.addressof.compile.pass.cpp
new file mode 100644
index 0000000000000..9542a7bf90a6c
--- /dev/null
+++ b/libcxx/test/std/containers/sequences/forwardlist/forwardlist.ops/merge_lvalue.addressof.compile.pass.cpp
@@ -0,0 +1,24 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// <forward_list>
+
+// template <class Compare> void merge(forward_list& x);
+
+// Validate whether the operation properly guards against ADL-hijacking operator&
+
+#include <forward_list>
+
+#include "test_macros.h"
+#include "operator_hijacker.h"
+
+void test() {
+  std::forward_list<operator_hijacker> lo;
+  std::forward_list<operator_hijacker> l;
+  lo.merge(l);
+}

diff  --git a/libcxx/test/std/containers/sequences/forwardlist/forwardlist.ops/merge_lvalue_pred.addressof.compile.pass.cpp b/libcxx/test/std/containers/sequences/forwardlist/forwardlist.ops/merge_lvalue_pred.addressof.compile.pass.cpp
new file mode 100644
index 0000000000000..012bbce9ce117
--- /dev/null
+++ b/libcxx/test/std/containers/sequences/forwardlist/forwardlist.ops/merge_lvalue_pred.addressof.compile.pass.cpp
@@ -0,0 +1,24 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// <forward_list>
+
+// template <class Compare> void merge(forward_list& x, Compare comp);
+
+// Validate whether the operation properly guards against ADL-hijacking operator&
+
+#include <forward_list>
+
+#include "test_macros.h"
+#include "operator_hijacker.h"
+
+void test() {
+  std::forward_list<operator_hijacker> lo;
+  std::forward_list<operator_hijacker> l;
+  lo.merge(l, std::less<operator_hijacker>());
+}

diff  --git a/libcxx/test/std/containers/sequences/forwardlist/forwardlist.ops/merge_rvalue.addressof.compile.pass.cpp b/libcxx/test/std/containers/sequences/forwardlist/forwardlist.ops/merge_rvalue.addressof.compile.pass.cpp
new file mode 100644
index 0000000000000..9955bed6b803e
--- /dev/null
+++ b/libcxx/test/std/containers/sequences/forwardlist/forwardlist.ops/merge_rvalue.addressof.compile.pass.cpp
@@ -0,0 +1,26 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// UNSUPPORTED: c++03
+
+// <forward_list>
+
+// template <class Compare> void merge(forward_list&& x);
+
+// Validate whether the operation properly guards against ADL-hijacking operator&
+
+#include <forward_list>
+
+#include "test_macros.h"
+#include "operator_hijacker.h"
+
+void test() {
+  std::forward_list<operator_hijacker> lo;
+  std::forward_list<operator_hijacker> l;
+  lo.merge(std::move(l));
+}

diff  --git a/libcxx/test/std/containers/sequences/forwardlist/forwardlist.ops/merge_rvalue_pred.addressof.compile.pass.cpp b/libcxx/test/std/containers/sequences/forwardlist/forwardlist.ops/merge_rvalue_pred.addressof.compile.pass.cpp
new file mode 100644
index 0000000000000..6a33cf06eb752
--- /dev/null
+++ b/libcxx/test/std/containers/sequences/forwardlist/forwardlist.ops/merge_rvalue_pred.addressof.compile.pass.cpp
@@ -0,0 +1,26 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// UNSUPPORTED: c++03
+
+// <forward_list>
+
+// template <class Compare> void merge(forward_list&& x, Compare comp);
+
+// Validate whether the operation properly guards against ADL-hijacking operator&
+
+#include <forward_list>
+
+#include "test_macros.h"
+#include "operator_hijacker.h"
+
+void test() {
+  std::forward_list<operator_hijacker> lo;
+  std::forward_list<operator_hijacker> l;
+  lo.merge(std::move(l), std::less<operator_hijacker>());
+}


        


More information about the libcxx-commits mailing list