Skip to content

Commit 76a9d79

Browse files
authored
[NFC][libc++] Guard against operator& hijacking. (#129453)
1 parent b271b44 commit 76a9d79

File tree

4 files changed

+32
-30
lines changed

4 files changed

+32
-30
lines changed

libcxx/include/__functional/function.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,7 @@ class __value_func<_Rp(_ArgTypes...)> {
437437
}
438438

439439
_LIBCPP_HIDE_FROM_ABI void swap(__value_func& __f) _NOEXCEPT {
440-
if (&__f == this)
440+
if (std::addressof(__f) == this)
441441
return;
442442
if ((void*)__f_ == &__buf_ && (void*)__f.__f_ == &__f.__buf_) {
443443
_LIBCPP_SUPPRESS_DEPRECATED_PUSH
@@ -550,8 +550,8 @@ struct __policy {
550550
template <typename _Fun>
551551
_LIBCPP_HIDE_FROM_ABI static const __policy* __choose_policy(/* is_small = */ false_type) {
552552
static constexpr __policy __policy = {
553-
&__large_clone<_Fun>,
554-
&__large_destroy<_Fun>,
553+
std::addressof(__large_clone<_Fun>),
554+
std::addressof(__large_destroy<_Fun>),
555555
false,
556556
# if _LIBCPP_HAS_RTTI
557557
&typeid(typename _Fun::_Target)
@@ -600,7 +600,7 @@ struct __policy_invoker<_Rp(_ArgTypes...)> {
600600
// Creates an invoker that calls the given instance of __func.
601601
template <typename _Fun>
602602
_LIBCPP_HIDE_FROM_ABI static __policy_invoker __create() {
603-
return __policy_invoker(&__call_impl<_Fun>);
603+
return __policy_invoker(std::addressof(__call_impl<_Fun>));
604604
}
605605

606606
private:

libcxx/include/__numeric/gcd_lcm.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <__assert>
1515
#include <__bit/countr.h>
1616
#include <__config>
17+
#include <__memory/addressof.h>
1718
#include <__type_traits/common_type.h>
1819
#include <__type_traits/is_integral.h>
1920
#include <__type_traits/is_same.h>
@@ -115,7 +116,7 @@ constexpr _LIBCPP_HIDE_FROM_ABI common_type_t<_Tp, _Up> lcm(_Tp __m, _Up __n) {
115116
_Rp __val1 = __ct_abs<_Rp, _Tp>()(__m) / std::gcd(__m, __n);
116117
_Rp __val2 = __ct_abs<_Rp, _Up>()(__n);
117118
_Rp __res;
118-
[[maybe_unused]] bool __overflow = __builtin_mul_overflow(__val1, __val2, &__res);
119+
[[maybe_unused]] bool __overflow = __builtin_mul_overflow(__val1, __val2, std::addressof(__res));
119120
_LIBCPP_ASSERT_ARGUMENT_WITHIN_DOMAIN(!__overflow, "Overflow in lcm");
120121
return __res;
121122
}

libcxx/include/__numeric/saturation_arithmetic.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <__assert>
1414
#include <__concepts/arithmetic.h>
1515
#include <__config>
16+
#include <__memory/addressof.h>
1617
#include <__utility/cmp.h>
1718
#include <limits>
1819

@@ -29,7 +30,7 @@ _LIBCPP_BEGIN_NAMESPACE_STD
2930

3031
template <__libcpp_integer _Tp>
3132
_LIBCPP_HIDE_FROM_ABI constexpr _Tp __add_sat(_Tp __x, _Tp __y) noexcept {
32-
if (_Tp __sum; !__builtin_add_overflow(__x, __y, &__sum))
33+
if (_Tp __sum; !__builtin_add_overflow(__x, __y, std::addressof(__sum)))
3334
return __sum;
3435
// Handle overflow
3536
if constexpr (__libcpp_unsigned_integer<_Tp>) {
@@ -47,7 +48,7 @@ _LIBCPP_HIDE_FROM_ABI constexpr _Tp __add_sat(_Tp __x, _Tp __y) noexcept {
4748

4849
template <__libcpp_integer _Tp>
4950
_LIBCPP_HIDE_FROM_ABI constexpr _Tp __sub_sat(_Tp __x, _Tp __y) noexcept {
50-
if (_Tp __sub; !__builtin_sub_overflow(__x, __y, &__sub))
51+
if (_Tp __sub; !__builtin_sub_overflow(__x, __y, std::addressof(__sub)))
5152
return __sub;
5253
// Handle overflow
5354
if constexpr (__libcpp_unsigned_integer<_Tp>) {
@@ -66,7 +67,7 @@ _LIBCPP_HIDE_FROM_ABI constexpr _Tp __sub_sat(_Tp __x, _Tp __y) noexcept {
6667

6768
template <__libcpp_integer _Tp>
6869
_LIBCPP_HIDE_FROM_ABI constexpr _Tp __mul_sat(_Tp __x, _Tp __y) noexcept {
69-
if (_Tp __mul; !__builtin_mul_overflow(__x, __y, &__mul))
70+
if (_Tp __mul; !__builtin_mul_overflow(__x, __y, std::addressof(__mul)))
7071
return __mul;
7172
// Handle overflow
7273
if constexpr (__libcpp_unsigned_integer<_Tp>) {

libcxx/include/locale

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3114,7 +3114,7 @@ public:
31143114
}
31153115
_LIBCPP_HIDE_FROM_ABI wide_string from_bytes(const char* __first, const char* __last);
31163116

3117-
_LIBCPP_HIDE_FROM_ABI byte_string to_bytes(_Elem __wchar) { return to_bytes(&__wchar, &__wchar + 1); }
3117+
_LIBCPP_HIDE_FROM_ABI byte_string to_bytes(_Elem __wchar) { return to_bytes(std::addressof(__wchar), std::addressof(__wchar) + 1); }
31183118
_LIBCPP_HIDE_FROM_ABI byte_string to_bytes(const _Elem* __wptr) {
31193119
return to_bytes(__wptr, __wptr + char_traits<_Elem>::length(__wptr));
31203120
}
@@ -3176,7 +3176,7 @@ wstring_convert<_Codecvt, _Elem, _WideAlloc, _ByteAlloc>::from_bytes(const char*
31763176
codecvt_base::result __r = codecvt_base::ok;
31773177
state_type __st = __cvtstate_;
31783178
if (__frm != __frm_end) {
3179-
_Elem* __to = &__ws[0];
3179+
_Elem* __to = std::addressof(__ws[0]);
31803180
_Elem* __to_end = __to + __ws.size();
31813181
const char* __frm_nxt;
31823182
do {
@@ -3186,19 +3186,19 @@ wstring_convert<_Codecvt, _Elem, _WideAlloc, _ByteAlloc>::from_bytes(const char*
31863186
if (__frm_nxt == __frm) {
31873187
__r = codecvt_base::error;
31883188
} else if (__r == codecvt_base::noconv) {
3189-
__ws.resize(__to - &__ws[0]);
3189+
__ws.resize(__to - std::addressof(__ws[0]));
31903190
// This only gets executed if _Elem is char
31913191
__ws.append((const _Elem*)__frm, (const _Elem*)__frm_end);
31923192
__frm = __frm_nxt;
31933193
__r = codecvt_base::ok;
31943194
} else if (__r == codecvt_base::ok) {
3195-
__ws.resize(__to_nxt - &__ws[0]);
3195+
__ws.resize(__to_nxt - std::addressof(__ws[0]));
31963196
__frm = __frm_nxt;
31973197
} else if (__r == codecvt_base::partial) {
3198-
ptrdiff_t __s = __to_nxt - &__ws[0];
3198+
ptrdiff_t __s = __to_nxt - std::addressof(__ws[0]);
31993199
__ws.resize(2 * __s);
3200-
__to = &__ws[0] + __s;
3201-
__to_end = &__ws[0] + __ws.size();
3200+
__to = std::addressof(__ws[0]) + __s;
3201+
__to_end = std::addressof(__ws[0]) + __ws.size();
32023202
__frm = __frm_nxt;
32033203
}
32043204
} while (__r == codecvt_base::partial && __frm_nxt < __frm_end);
@@ -3224,7 +3224,7 @@ wstring_convert<_Codecvt, _Elem, _WideAlloc, _ByteAlloc>::to_bytes(const _Elem*
32243224
codecvt_base::result __r = codecvt_base::ok;
32253225
state_type __st = __cvtstate_;
32263226
if (__frm != __frm_end) {
3227-
char* __to = &__bs[0];
3227+
char* __to = std::addressof(__bs[0]);
32283228
char* __to_end = __to + __bs.size();
32293229
const _Elem* __frm_nxt;
32303230
do {
@@ -3234,41 +3234,41 @@ wstring_convert<_Codecvt, _Elem, _WideAlloc, _ByteAlloc>::to_bytes(const _Elem*
32343234
if (__frm_nxt == __frm) {
32353235
__r = codecvt_base::error;
32363236
} else if (__r == codecvt_base::noconv) {
3237-
__bs.resize(__to - &__bs[0]);
3237+
__bs.resize(__to - std::addressof(__bs[0]));
32383238
// This only gets executed if _Elem is char
32393239
__bs.append((const char*)__frm, (const char*)__frm_end);
32403240
__frm = __frm_nxt;
32413241
__r = codecvt_base::ok;
32423242
} else if (__r == codecvt_base::ok) {
3243-
__bs.resize(__to_nxt - &__bs[0]);
3243+
__bs.resize(__to_nxt - std::addressof(__bs[0]));
32443244
__frm = __frm_nxt;
32453245
} else if (__r == codecvt_base::partial) {
3246-
ptrdiff_t __s = __to_nxt - &__bs[0];
3246+
ptrdiff_t __s = __to_nxt - std::addressof(__bs[0]);
32473247
__bs.resize(2 * __s);
3248-
__to = &__bs[0] + __s;
3249-
__to_end = &__bs[0] + __bs.size();
3248+
__to = std::addressof(__bs[0]) + __s;
3249+
__to_end = std::addressof(__bs[0]) + __bs.size();
32503250
__frm = __frm_nxt;
32513251
}
32523252
} while (__r == codecvt_base::partial && __frm_nxt < __frm_end);
32533253
}
32543254
if (__r == codecvt_base::ok) {
32553255
size_t __s = __bs.size();
32563256
__bs.resize(__bs.capacity());
3257-
char* __to = &__bs[0] + __s;
3257+
char* __to = std::addressof(__bs[0]) + __s;
32583258
char* __to_end = __to + __bs.size();
32593259
do {
32603260
char* __to_nxt;
32613261
__r = __cvtptr_->unshift(__st, __to, __to_end, __to_nxt);
32623262
if (__r == codecvt_base::noconv) {
3263-
__bs.resize(__to - &__bs[0]);
3263+
__bs.resize(__to - std::addressof(__bs[0]));
32643264
__r = codecvt_base::ok;
32653265
} else if (__r == codecvt_base::ok) {
3266-
__bs.resize(__to_nxt - &__bs[0]);
3266+
__bs.resize(__to_nxt - std::addressof(__bs[0]));
32673267
} else if (__r == codecvt_base::partial) {
3268-
ptrdiff_t __sp = __to_nxt - &__bs[0];
3268+
ptrdiff_t __sp = __to_nxt - std::addressof(__bs[0]);
32693269
__bs.resize(2 * __sp);
3270-
__to = &__bs[0] + __sp;
3271-
__to_end = &__bs[0] + __bs.size();
3270+
__to = std::addressof(__bs[0]) + __sp;
3271+
__to_end = std::addressof(__bs[0]) + __bs.size();
32723272
}
32733273
} while (__r == codecvt_base::partial);
32743274
if (__r == codecvt_base::ok)
@@ -3387,7 +3387,7 @@ typename wbuffer_convert<_Codecvt, _Elem, _Tr>::int_type wbuffer_convert<_Codecv
33873387
bool __initial = __read_mode();
33883388
char_type __1buf;
33893389
if (this->gptr() == 0)
3390-
this->setg(&__1buf, &__1buf + 1, &__1buf + 1);
3390+
this->setg(std::addressof(__1buf), std::addressof(__1buf) + 1, std::addressof(__1buf) + 1);
33913391
const size_t __unget_sz = __initial ? 0 : std::min<size_t>((this->egptr() - this->eback()) / 2, 4);
33923392
int_type __c = traits_type::eof();
33933393
if (this->gptr() == this->egptr()) {
@@ -3429,7 +3429,7 @@ typename wbuffer_convert<_Codecvt, _Elem, _Tr>::int_type wbuffer_convert<_Codecv
34293429
}
34303430
} else
34313431
__c = *this->gptr();
3432-
if (this->eback() == &__1buf)
3432+
if (this->eback() == std::addressof(__1buf))
34333433
this->setg(0, 0, 0);
34343434
return __c;
34353435
}
@@ -3465,7 +3465,7 @@ typename wbuffer_convert<_Codecvt, _Elem, _Tr>::int_type wbuffer_convert<_Codecv
34653465
char_type* __epb_save = this->epptr();
34663466
if (!traits_type::eq_int_type(__c, traits_type::eof())) {
34673467
if (this->pptr() == 0)
3468-
this->setp(&__1buf, &__1buf + 1);
3468+
this->setp(std::addressof(__1buf), std::addressof(__1buf) + 1);
34693469
*this->pptr() = traits_type::to_char_type(__c);
34703470
this->pbump(1);
34713471
}

0 commit comments

Comments
 (0)