Skip to content

Commit

Permalink
[libc++] Use addressof in list.
Browse files Browse the repository at this point in the history
This addresses the usage of `operator&` in `<list>`.

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

Reviewed By: #libc, Quuxplusone, ldionne

Differential Revision: https://reviews.llvm.org/D112654
  • Loading branch information
mordante committed Nov 11, 2021
1 parent 82de586 commit 4732dd3
Show file tree
Hide file tree
Showing 13 changed files with 258 additions and 27 deletions.
50 changes: 25 additions & 25 deletions libcxx/include/list
Expand Up @@ -470,9 +470,9 @@ public:
_LIBCPP_INLINE_VISIBILITY
__list_const_iterator& operator=(const __list_const_iterator& __p)
{
if (this != &__p)
if (this != _VSTD::addressof(__p))
{
__get_db()->__iterator_copy(this, &__p);
__get_db()->__iterator_copy(this, _VSTD::addressof(__p));
__ptr_ = __p.__ptr_;
}
return *this;
Expand Down Expand Up @@ -797,7 +797,7 @@ __list_imp<_Tp, _Alloc>::swap(__list_imp& __c)
#if _LIBCPP_DEBUG_LEVEL == 2
__libcpp_db* __db = __get_db();
__c_node* __cn1 = __db->__find_c_and_lock(this);
__c_node* __cn2 = __db->__find_c(&__c);
__c_node* __cn2 = __db->__find_c(_VSTD::addressof(__c));
_VSTD::swap(__cn1->beg_, __cn2->beg_);
_VSTD::swap(__cn1->end_, __cn2->end_);
_VSTD::swap(__cn1->cap_, __cn2->cap_);
Expand Down Expand Up @@ -1451,7 +1451,7 @@ typename list<_Tp, _Alloc>::iterator
list<_Tp, _Alloc>::insert(const_iterator __p, const value_type& __x)
{
#if _LIBCPP_DEBUG_LEVEL == 2
_LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
_LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(_VSTD::addressof(__p)) == this,
"list::insert(iterator, x) called with an iterator not"
" referring to this list");
#endif
Expand All @@ -1472,7 +1472,7 @@ typename list<_Tp, _Alloc>::iterator
list<_Tp, _Alloc>::insert(const_iterator __p, size_type __n, const value_type& __x)
{
#if _LIBCPP_DEBUG_LEVEL == 2
_LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
_LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(_VSTD::addressof(__p)) == this,
"list::insert(iterator, n, x) called with an iterator not"
" referring to this list");
iterator __r(__p.__ptr_, this);
Expand Down Expand Up @@ -1697,7 +1697,7 @@ typename list<_Tp, _Alloc>::iterator
list<_Tp, _Alloc>::emplace(const_iterator __p, _Args&&... __args)
{
#if _LIBCPP_DEBUG_LEVEL == 2
_LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
_LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(_VSTD::addressof(__p)) == this,
"list::emplace(iterator, args...) called with an iterator not"
" referring to this list");
#endif
Expand All @@ -1720,7 +1720,7 @@ typename list<_Tp, _Alloc>::iterator
list<_Tp, _Alloc>::insert(const_iterator __p, value_type&& __x)
{
#if _LIBCPP_DEBUG_LEVEL == 2
_LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
_LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(_VSTD::addressof(__p)) == this,
"list::insert(iterator, x) called with an iterator not"
" referring to this list");
#endif
Expand Down Expand Up @@ -1803,7 +1803,7 @@ typename list<_Tp, _Alloc>::iterator
list<_Tp, _Alloc>::erase(const_iterator __p)
{
#if _LIBCPP_DEBUG_LEVEL == 2
_LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
_LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(_VSTD::addressof(__p)) == this,
"list::erase(iterator) called with an iterator not"
" referring to this list");
#endif
Expand Down Expand Up @@ -2006,10 +2006,10 @@ template <class _Tp, class _Alloc>
void
list<_Tp, _Alloc>::splice(const_iterator __p, list& __c)
{
_LIBCPP_ASSERT(this != &__c,
_LIBCPP_ASSERT(this != _VSTD::addressof(__c),
"list::splice(iterator, list) called with this == &list");
#if _LIBCPP_DEBUG_LEVEL == 2
_LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
_LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(_VSTD::addressof(__p)) == this,
"list::splice(iterator, list) called with an iterator not"
" referring to this list");
#endif
Expand All @@ -2022,10 +2022,10 @@ list<_Tp, _Alloc>::splice(const_iterator __p, list& __c)
base::__sz() += __c.__sz();
__c.__sz() = 0;
#if _LIBCPP_DEBUG_LEVEL == 2
if (&__c != this) {
if (_VSTD::addressof(__c) != this) {
__libcpp_db* __db = __get_db();
__c_node* __cn1 = __db->__find_c_and_lock(this);
__c_node* __cn2 = __db->__find_c(&__c);
__c_node* __cn2 = __db->__find_c(_VSTD::addressof(__c));
for (__i_node** __ip = __cn2->end_; __ip != __cn2->beg_;)
{
--__ip;
Expand All @@ -2049,13 +2049,13 @@ void
list<_Tp, _Alloc>::splice(const_iterator __p, list& __c, const_iterator __i)
{
#if _LIBCPP_DEBUG_LEVEL == 2
_LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
_LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(_VSTD::addressof(__p)) == this,
"list::splice(iterator, list, iterator) called with the first iterator"
" not referring to this list");
_LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__i) == &__c,
_LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(_VSTD::addressof(__i)) == _VSTD::addressof(__c),
"list::splice(iterator, list, iterator) called with the second iterator"
" not referring to the list argument");
_LIBCPP_ASSERT(__get_const_db()->__dereferenceable(&__i),
_LIBCPP_ASSERT(__get_const_db()->__dereferenceable(_VSTD::addressof(__i)),
"list::splice(iterator, list, iterator) called with the second iterator"
" not dereferenceable");
#endif
Expand All @@ -2067,10 +2067,10 @@ list<_Tp, _Alloc>::splice(const_iterator __p, list& __c, const_iterator __i)
--__c.__sz();
++base::__sz();
#if _LIBCPP_DEBUG_LEVEL == 2
if (&__c != this) {
if (_VSTD::addressof(__c) != this) {
__libcpp_db* __db = __get_db();
__c_node* __cn1 = __db->__find_c_and_lock(this);
__c_node* __cn2 = __db->__find_c(&__c);
__c_node* __cn2 = __db->__find_c(_VSTD::addressof(__c));
for (__i_node** __ip = __cn2->end_; __ip != __cn2->beg_;)
{
--__ip;
Expand All @@ -2094,16 +2094,16 @@ void
list<_Tp, _Alloc>::splice(const_iterator __p, list& __c, const_iterator __f, const_iterator __l)
{
#if _LIBCPP_DEBUG_LEVEL == 2
_LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
_LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(_VSTD::addressof(__p)) == this,
"list::splice(iterator, list, iterator, iterator) called with first iterator not"
" referring to this list");
_LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__f) == &__c,
_LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(_VSTD::addressof(__f)) == _VSTD::addressof(__c),
"list::splice(iterator, list, iterator, iterator) called with second iterator not"
" referring to the list argument");
_LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__l) == &__c,
_LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(_VSTD::addressof(__l)) == _VSTD::addressof(__c),
"list::splice(iterator, list, iterator, iterator) called with third iterator not"
" referring to the list argument");
if (this == &__c)
if (this == _VSTD::addressof(__c))
{
for (const_iterator __i = __f; __i != __l; ++__i)
_LIBCPP_ASSERT(__i != __p,
Expand All @@ -2117,7 +2117,7 @@ list<_Tp, _Alloc>::splice(const_iterator __p, list& __c, const_iterator __f, con
__link_pointer __first = __f.__ptr_;
--__l;
__link_pointer __last = __l.__ptr_;
if (this != &__c)
if (this != _VSTD::addressof(__c))
{
size_type __s = _VSTD::distance(__f, __l) + 1;
__c.__sz() -= __s;
Expand All @@ -2126,10 +2126,10 @@ list<_Tp, _Alloc>::splice(const_iterator __p, list& __c, const_iterator __f, con
base::__unlink_nodes(__first, __last);
__link_nodes(__p.__ptr_, __first, __last);
#if _LIBCPP_DEBUG_LEVEL == 2
if (&__c != this) {
if (_VSTD::addressof(__c) != this) {
__libcpp_db* __db = __get_db();
__c_node* __cn1 = __db->__find_c_and_lock(this);
__c_node* __cn2 = __db->__find_c(&__c);
__c_node* __cn2 = __db->__find_c(_VSTD::addressof(__c));
for (__i_node** __ip = __cn2->end_; __ip != __cn2->beg_;)
{
--__ip;
Expand Down Expand Up @@ -2265,7 +2265,7 @@ list<_Tp, _Alloc>::merge(list& __c, _Comp __comp)
#if _LIBCPP_DEBUG_LEVEL == 2
__libcpp_db* __db = __get_db();
__c_node* __cn1 = __db->__find_c_and_lock(this);
__c_node* __cn2 = __db->__find_c(&__c);
__c_node* __cn2 = __db->__find_c(_VSTD::addressof(__c));
for (__i_node** __p = __cn2->end_; __p != __cn2->beg_;)
{
--__p;
Expand Down
@@ -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

// <list>

// list& operator=(list&& c);

// Validate whether the operation properly guards against ADL-hijacking operator&

#include <list>

#include "test_macros.h"
#include "operator_hijacker.h"

void test() {
std::list<operator_hijacker> lo;
std::list<operator_hijacker> l;
l = std::move(lo);
}
@@ -0,0 +1,22 @@
//===----------------------------------------------------------------------===//
//
// 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

// <list>

// template <class... Args> void emplace(const_iterator p, Args&&... args);

// Validate whether the operation properly guards against ADL-hijacking operator&

#include <list>

#include "test_macros.h"
#include "operator_hijacker.h"

void test(std::list<operator_hijacker>& l) { l.emplace(l.begin(), l.front()); }
@@ -0,0 +1,23 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//

// <list>

// iterator erase(const_iterator position);

// Validate whether the operation properly guards against ADL-hijacking operator&

#include <list>

#include "test_macros.h"
#include "operator_hijacker.h"

void test() {
std::list<operator_hijacker> l;
l.erase(l.begin());
}
@@ -0,0 +1,25 @@
//===----------------------------------------------------------------------===//
//
// 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

// <list>

// iterator insert(const_iterator position, value_type&& x);

// Validate whether the operation properly guards against ADL-hijacking operator&

#include <list>

#include "test_macros.h"
#include "operator_hijacker.h"

void test() {
std::list<operator_hijacker> l;
l.insert(l.begin(), operator_hijacker{});
}
@@ -0,0 +1,20 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//

// <list>

// iterator insert(const_iterator position, size_type n, const value_type& x);

// Validate whether the operation properly guards against ADL-hijacking operator&

#include <list>

#include "test_macros.h"
#include "operator_hijacker.h"

void test(std::list<operator_hijacker>& l) { l.insert(l.begin(), l.size(), l.front()); }
@@ -0,0 +1,20 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//

// <list>

// iterator insert(const_iterator position, const value_type& x);

// Validate whether the operation properly guards against ADL-hijacking operator&

#include <list>

#include "test_macros.h"
#include "operator_hijacker.h"

void test(std::list<operator_hijacker>& l) { l.insert(l.begin(), l.front()); }
Expand Up @@ -9,7 +9,7 @@
// <list>

// void merge(list& x);
// If (&addressof(x) == this) does nothing; otherwise ...
// If (addressof(x) == this) does nothing; otherwise ...

#include <list>
#include <cassert>
Expand Down
@@ -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
//
//===----------------------------------------------------------------------===//

// <list>

// template <class Compare> void merge(list& x, Compare comp);
// If (addressof(x) == this) does nothing; otherwise ...

// Validate whether the operation properly guards against ADL-hijacking operator&

#include <list>

#include "test_macros.h"
#include "operator_hijacker.h"

void test() {
std::list<operator_hijacker> l;
l.merge(l, std::less<operator_hijacker>());
}
Expand Up @@ -9,7 +9,7 @@
// <list>

// template <class Compare> void merge(list& x, Compare comp);
// If (&addressof(x) == this) does nothing; otherwise ...
// If (addressof(x) == this) does nothing; otherwise ...

#include <list>
#include <functional>
Expand Down
@@ -0,0 +1,23 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//

// <list>

// void splice(const_iterator position, list<T,Allocator>& x, iterator i);

// Validate whether the operation properly guards against ADL-hijacking operator&

#include <list>

#include "test_macros.h"
#include "operator_hijacker.h"

void test() {
std::list<operator_hijacker> l;
l.splice(l.end(), l, l.begin());
}

0 comments on commit 4732dd3

Please sign in to comment.