Skip to content

Commit da618cf

Browse files
authored
[NFC][libc++] Guard against operator& hijacking. (#128351)
This set usage of operator& instead of std::addressof seems not be easy to "abuse". Some seem easy to misuse, like basic_ostream::operator<<, trying to do that results in compilation errors since the `widen` function is not specialized for the hijacking character type. Hence there are no tests.
1 parent 69effe0 commit da618cf

File tree

23 files changed

+87
-70
lines changed

23 files changed

+87
-70
lines changed

libcxx/include/__atomic/atomic.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,7 @@ struct atomic<_Tp> : __atomic_base<_Tp> {
363363
// https://github.com/llvm/llvm-project/issues/47978
364364
// clang bug: __old is not updated on failure for atomic<long double>::compare_exchange_weak
365365
// Note __old = __self.load(memory_order_relaxed) will not work
366-
std::__cxx_atomic_load_inplace(std::addressof(__self.__a_), &__old, memory_order_relaxed);
366+
std::__cxx_atomic_load_inplace(std::addressof(__self.__a_), std::addressof(__old), memory_order_relaxed);
367367
}
368368
# endif
369369
__new = __operation(__old, __operand);

libcxx/include/__atomic/atomic_ref.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ struct __atomic_ref_base {
119119
// that the pointer is going to be aligned properly at runtime because that is a (checked) precondition
120120
// of atomic_ref's constructor.
121121
static constexpr bool is_always_lock_free =
122-
__atomic_always_lock_free(sizeof(_Tp), &__get_aligner_instance<required_alignment>::__instance);
122+
__atomic_always_lock_free(sizeof(_Tp), std::addressof(__get_aligner_instance<required_alignment>::__instance));
123123

124124
_LIBCPP_HIDE_FROM_ABI bool is_lock_free() const noexcept { return __atomic_is_lock_free(sizeof(_Tp), __ptr_); }
125125

libcxx/include/__charconv/traits.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include <__charconv/tables.h>
1616
#include <__charconv/to_chars_base_10.h>
1717
#include <__config>
18+
#include <__memory/addressof.h>
1819
#include <__type_traits/enable_if.h>
1920
#include <__type_traits/is_unsigned.h>
2021
#include <cstdint>
@@ -142,7 +143,7 @@ __mul_overflowed(unsigned short __a, _Tp __b, unsigned short& __r) {
142143
template <typename _Tp>
143144
inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI bool __mul_overflowed(_Tp __a, _Tp __b, _Tp& __r) {
144145
static_assert(is_unsigned<_Tp>::value, "");
145-
return __builtin_mul_overflow(__a, __b, &__r);
146+
return __builtin_mul_overflow(__a, __b, std::addressof(__r));
146147
}
147148

148149
template <typename _Tp, typename _Up>

libcxx/include/__filesystem/path.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <__fwd/functional.h>
1818
#include <__iterator/back_insert_iterator.h>
1919
#include <__iterator/iterator_traits.h>
20+
#include <__memory/addressof.h>
2021
#include <__type_traits/decay.h>
2122
#include <__type_traits/enable_if.h>
2223
#include <__type_traits/is_pointer.h>
@@ -584,7 +585,7 @@ class _LIBCPP_EXPORTED_FROM_ABI path {
584585

585586
template <class _ECharT, __enable_if_t<__can_convert_char<_ECharT>::value, int> = 0>
586587
_LIBCPP_HIDE_FROM_ABI path& operator+=(_ECharT __x) {
587-
_PathCVT<_ECharT>::__append_source(__pn_, basic_string_view<_ECharT>(&__x, 1));
588+
_PathCVT<_ECharT>::__append_source(__pn_, basic_string_view<_ECharT>(std::addressof(__x), 1));
588589
return *this;
589590
}
590591

libcxx/include/__functional/hash.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <__cstddef/nullptr_t.h>
1414
#include <__functional/unary_function.h>
1515
#include <__fwd/functional.h>
16+
#include <__memory/addressof.h>
1617
#include <__type_traits/conjunction.h>
1718
#include <__type_traits/enable_if.h>
1819
#include <__type_traits/invoke.h>
@@ -33,7 +34,7 @@ _LIBCPP_BEGIN_NAMESPACE_STD
3334
template <class _Size>
3435
inline _LIBCPP_HIDE_FROM_ABI _Size __loadword(const void* __p) {
3536
_Size __r;
36-
std::memcpy(&__r, __p, sizeof(__r));
37+
std::memcpy(std::addressof(__r), __p, sizeof(__r));
3738
return __r;
3839
}
3940

@@ -276,7 +277,7 @@ struct __scalar_hash<_Tp, 2> : public __unary_function<_Tp, size_t> {
276277
} __s;
277278
} __u;
278279
__u.__t = __v;
279-
return __murmur2_or_cityhash<size_t>()(&__u, sizeof(__u));
280+
return __murmur2_or_cityhash<size_t>()(std::addressof(__u), sizeof(__u));
280281
}
281282
};
282283

@@ -292,7 +293,7 @@ struct __scalar_hash<_Tp, 3> : public __unary_function<_Tp, size_t> {
292293
} __s;
293294
} __u;
294295
__u.__t = __v;
295-
return __murmur2_or_cityhash<size_t>()(&__u, sizeof(__u));
296+
return __murmur2_or_cityhash<size_t>()(std::addressof(__u), sizeof(__u));
296297
}
297298
};
298299

@@ -309,7 +310,7 @@ struct __scalar_hash<_Tp, 4> : public __unary_function<_Tp, size_t> {
309310
} __s;
310311
} __u;
311312
__u.__t = __v;
312-
return __murmur2_or_cityhash<size_t>()(&__u, sizeof(__u));
313+
return __murmur2_or_cityhash<size_t>()(std::addressof(__u), sizeof(__u));
313314
}
314315
};
315316

@@ -332,7 +333,7 @@ struct _LIBCPP_TEMPLATE_VIS hash<_Tp*> : public __unary_function<_Tp*, size_t> {
332333
size_t __a;
333334
} __u;
334335
__u.__t = __v;
335-
return __murmur2_or_cityhash<size_t>()(&__u, sizeof(__u));
336+
return __murmur2_or_cityhash<size_t>()(std::addressof(__u), sizeof(__u));
336337
}
337338
};
338339

libcxx/include/__iterator/aliasing_iterator.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <__config>
1313
#include <__cstddef/ptrdiff_t.h>
1414
#include <__iterator/iterator_traits.h>
15+
#include <__memory/addressof.h>
1516
#include <__memory/pointer_traits.h>
1617
#include <__type_traits/is_trivial.h>
1718

@@ -102,7 +103,7 @@ struct __aliasing_iterator_wrapper {
102103

103104
_LIBCPP_HIDE_FROM_ABI _Alias operator*() const _NOEXCEPT {
104105
_Alias __val;
105-
__builtin_memcpy(&__val, std::__to_address(__base_), sizeof(value_type));
106+
__builtin_memcpy(std::addressof(__val), std::__to_address(__base_), sizeof(value_type));
106107
return __val;
107108
}
108109

libcxx/include/__locale

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
#include <__config>
1414
#include <__locale_dir/locale_base_api.h>
15+
#include <__memory/addressof.h>
1516
#include <__memory/shared_count.h>
1617
#include <__mutex/once_flag.h>
1718
#include <__type_traits/make_unsigned.h>
@@ -156,7 +157,7 @@ locale locale::combine(const locale& __other) const {
156157
if (!std::has_facet<_Facet>(__other))
157158
std::__throw_runtime_error("locale::combine: locale missing facet");
158159

159-
return locale(*this, &const_cast<_Facet&>(std::use_facet<_Facet>(__other)));
160+
return locale(*this, std::addressof(const_cast<_Facet&>(std::use_facet<_Facet>(__other))));
160161
}
161162

162163
template <class _Facet>

libcxx/include/__mdspan/layout_left.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <__config>
2222
#include <__fwd/mdspan.h>
2323
#include <__mdspan/extents.h>
24+
#include <__memory/addressof.h>
2425
#include <__type_traits/common_type.h>
2526
#include <__type_traits/is_constructible.h>
2627
#include <__type_traits/is_convertible.h>
@@ -58,7 +59,7 @@ class layout_left::mapping {
5859

5960
index_type __prod = __ext.extent(0);
6061
for (rank_type __r = 1; __r < extents_type::rank(); __r++) {
61-
bool __overflowed = __builtin_mul_overflow(__prod, __ext.extent(__r), &__prod);
62+
bool __overflowed = __builtin_mul_overflow(__prod, __ext.extent(__r), std::addressof(__prod));
6263
if (__overflowed)
6364
return false;
6465
}

libcxx/include/__mdspan/layout_right.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include <__cstddef/size_t.h>
2323
#include <__fwd/mdspan.h>
2424
#include <__mdspan/extents.h>
25+
#include <__memory/addressof.h>
2526
#include <__type_traits/common_type.h>
2627
#include <__type_traits/is_constructible.h>
2728
#include <__type_traits/is_convertible.h>
@@ -58,7 +59,7 @@ class layout_right::mapping {
5859

5960
index_type __prod = __ext.extent(0);
6061
for (rank_type __r = 1; __r < extents_type::rank(); __r++) {
61-
bool __overflowed = __builtin_mul_overflow(__prod, __ext.extent(__r), &__prod);
62+
bool __overflowed = __builtin_mul_overflow(__prod, __ext.extent(__r), std::addressof(__prod));
6263
if (__overflowed)
6364
return false;
6465
}

libcxx/include/__mdspan/layout_stride.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include <__config>
2323
#include <__fwd/mdspan.h>
2424
#include <__mdspan/extents.h>
25+
#include <__memory/addressof.h>
2526
#include <__type_traits/common_type.h>
2627
#include <__type_traits/is_constructible.h>
2728
#include <__type_traits/is_convertible.h>
@@ -86,7 +87,7 @@ class layout_stride::mapping {
8687

8788
index_type __prod = __ext.extent(0);
8889
for (rank_type __r = 1; __r < __rank_; __r++) {
89-
bool __overflowed = __builtin_mul_overflow(__prod, __ext.extent(__r), &__prod);
90+
bool __overflowed = __builtin_mul_overflow(__prod, __ext.extent(__r), std::addressof(__prod));
9091
if (__overflowed)
9192
return false;
9293
}
@@ -109,11 +110,12 @@ class layout_stride::mapping {
109110
}
110111
if (__ext.extent(__r) == static_cast<index_type>(0))
111112
return true;
112-
index_type __prod = (__ext.extent(__r) - 1);
113-
bool __overflowed_mul = __builtin_mul_overflow(__prod, static_cast<index_type>(__strides[__r]), &__prod);
113+
index_type __prod = (__ext.extent(__r) - 1);
114+
bool __overflowed_mul =
115+
__builtin_mul_overflow(__prod, static_cast<index_type>(__strides[__r]), std::addressof(__prod));
114116
if (__overflowed_mul)
115117
return false;
116-
bool __overflowed_add = __builtin_add_overflow(__size, __prod, &__size);
118+
bool __overflowed_add = __builtin_add_overflow(__size, __prod, std::addressof(__size));
117119
if (__overflowed_add)
118120
return false;
119121
}

0 commit comments

Comments
 (0)