Skip to content

Commit 687de63

Browse files
committed
[NFC][libc++] Guard against operator& hijacking.
1 parent be54261 commit 687de63

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
@@ -3110,7 +3110,7 @@ public:
31103110
}
31113111
_LIBCPP_HIDE_FROM_ABI wide_string from_bytes(const char* __first, const char* __last);
31123112

3113-
_LIBCPP_HIDE_FROM_ABI byte_string to_bytes(_Elem __wchar) { return to_bytes(&__wchar, &__wchar + 1); }
3113+
_LIBCPP_HIDE_FROM_ABI byte_string to_bytes(_Elem __wchar) { return to_bytes(std::addressof(__wchar), std::addressof(__wchar) + 1); }
31143114
_LIBCPP_HIDE_FROM_ABI byte_string to_bytes(const _Elem* __wptr) {
31153115
return to_bytes(__wptr, __wptr + char_traits<_Elem>::length(__wptr));
31163116
}
@@ -3172,7 +3172,7 @@ wstring_convert<_Codecvt, _Elem, _WideAlloc, _ByteAlloc>::from_bytes(const char*
31723172
codecvt_base::result __r = codecvt_base::ok;
31733173
state_type __st = __cvtstate_;
31743174
if (__frm != __frm_end) {
3175-
_Elem* __to = &__ws[0];
3175+
_Elem* __to = std::addressof(__ws[0]);
31763176
_Elem* __to_end = __to + __ws.size();
31773177
const char* __frm_nxt;
31783178
do {
@@ -3182,19 +3182,19 @@ wstring_convert<_Codecvt, _Elem, _WideAlloc, _ByteAlloc>::from_bytes(const char*
31823182
if (__frm_nxt == __frm) {
31833183
__r = codecvt_base::error;
31843184
} else if (__r == codecvt_base::noconv) {
3185-
__ws.resize(__to - &__ws[0]);
3185+
__ws.resize(__to - std::addressof(__ws[0]));
31863186
// This only gets executed if _Elem is char
31873187
__ws.append((const _Elem*)__frm, (const _Elem*)__frm_end);
31883188
__frm = __frm_nxt;
31893189
__r = codecvt_base::ok;
31903190
} else if (__r == codecvt_base::ok) {
3191-
__ws.resize(__to_nxt - &__ws[0]);
3191+
__ws.resize(__to_nxt - std::addressof(__ws[0]));
31923192
__frm = __frm_nxt;
31933193
} else if (__r == codecvt_base::partial) {
3194-
ptrdiff_t __s = __to_nxt - &__ws[0];
3194+
ptrdiff_t __s = __to_nxt - std::addressof(__ws[0]);
31953195
__ws.resize(2 * __s);
3196-
__to = &__ws[0] + __s;
3197-
__to_end = &__ws[0] + __ws.size();
3196+
__to = std::addressof(__ws[0]) + __s;
3197+
__to_end = std::addressof(__ws[0]) + __ws.size();
31983198
__frm = __frm_nxt;
31993199
}
32003200
} while (__r == codecvt_base::partial && __frm_nxt < __frm_end);
@@ -3220,7 +3220,7 @@ wstring_convert<_Codecvt, _Elem, _WideAlloc, _ByteAlloc>::to_bytes(const _Elem*
32203220
codecvt_base::result __r = codecvt_base::ok;
32213221
state_type __st = __cvtstate_;
32223222
if (__frm != __frm_end) {
3223-
char* __to = &__bs[0];
3223+
char* __to = std::addressof(__bs[0]);
32243224
char* __to_end = __to + __bs.size();
32253225
const _Elem* __frm_nxt;
32263226
do {
@@ -3230,41 +3230,41 @@ wstring_convert<_Codecvt, _Elem, _WideAlloc, _ByteAlloc>::to_bytes(const _Elem*
32303230
if (__frm_nxt == __frm) {
32313231
__r = codecvt_base::error;
32323232
} else if (__r == codecvt_base::noconv) {
3233-
__bs.resize(__to - &__bs[0]);
3233+
__bs.resize(__to - std::addressof(__bs[0]));
32343234
// This only gets executed if _Elem is char
32353235
__bs.append((const char*)__frm, (const char*)__frm_end);
32363236
__frm = __frm_nxt;
32373237
__r = codecvt_base::ok;
32383238
} else if (__r == codecvt_base::ok) {
3239-
__bs.resize(__to_nxt - &__bs[0]);
3239+
__bs.resize(__to_nxt - std::addressof(__bs[0]));
32403240
__frm = __frm_nxt;
32413241
} else if (__r == codecvt_base::partial) {
3242-
ptrdiff_t __s = __to_nxt - &__bs[0];
3242+
ptrdiff_t __s = __to_nxt - std::addressof(__bs[0]);
32433243
__bs.resize(2 * __s);
3244-
__to = &__bs[0] + __s;
3245-
__to_end = &__bs[0] + __bs.size();
3244+
__to = std::addressof(__bs[0]) + __s;
3245+
__to_end = std::addressof(__bs[0]) + __bs.size();
32463246
__frm = __frm_nxt;
32473247
}
32483248
} while (__r == codecvt_base::partial && __frm_nxt < __frm_end);
32493249
}
32503250
if (__r == codecvt_base::ok) {
32513251
size_t __s = __bs.size();
32523252
__bs.resize(__bs.capacity());
3253-
char* __to = &__bs[0] + __s;
3253+
char* __to = std::addressof(__bs[0]) + __s;
32543254
char* __to_end = __to + __bs.size();
32553255
do {
32563256
char* __to_nxt;
32573257
__r = __cvtptr_->unshift(__st, __to, __to_end, __to_nxt);
32583258
if (__r == codecvt_base::noconv) {
3259-
__bs.resize(__to - &__bs[0]);
3259+
__bs.resize(__to - std::addressof(__bs[0]));
32603260
__r = codecvt_base::ok;
32613261
} else if (__r == codecvt_base::ok) {
3262-
__bs.resize(__to_nxt - &__bs[0]);
3262+
__bs.resize(__to_nxt - std::addressof(__bs[0]));
32633263
} else if (__r == codecvt_base::partial) {
3264-
ptrdiff_t __sp = __to_nxt - &__bs[0];
3264+
ptrdiff_t __sp = __to_nxt - std::addressof(__bs[0]);
32653265
__bs.resize(2 * __sp);
3266-
__to = &__bs[0] + __sp;
3267-
__to_end = &__bs[0] + __bs.size();
3266+
__to = std::addressof(__bs[0]) + __sp;
3267+
__to_end = std::addressof(__bs[0]) + __bs.size();
32683268
}
32693269
} while (__r == codecvt_base::partial);
32703270
if (__r == codecvt_base::ok)
@@ -3383,7 +3383,7 @@ typename wbuffer_convert<_Codecvt, _Elem, _Tr>::int_type wbuffer_convert<_Codecv
33833383
bool __initial = __read_mode();
33843384
char_type __1buf;
33853385
if (this->gptr() == 0)
3386-
this->setg(&__1buf, &__1buf + 1, &__1buf + 1);
3386+
this->setg(std::addressof(__1buf), std::addressof(__1buf) + 1, std::addressof(__1buf) + 1);
33873387
const size_t __unget_sz = __initial ? 0 : std::min<size_t>((this->egptr() - this->eback()) / 2, 4);
33883388
int_type __c = traits_type::eof();
33893389
if (this->gptr() == this->egptr()) {
@@ -3425,7 +3425,7 @@ typename wbuffer_convert<_Codecvt, _Elem, _Tr>::int_type wbuffer_convert<_Codecv
34253425
}
34263426
} else
34273427
__c = *this->gptr();
3428-
if (this->eback() == &__1buf)
3428+
if (this->eback() == std::addressof(__1buf))
34293429
this->setg(0, 0, 0);
34303430
return __c;
34313431
}
@@ -3461,7 +3461,7 @@ typename wbuffer_convert<_Codecvt, _Elem, _Tr>::int_type wbuffer_convert<_Codecv
34613461
char_type* __epb_save = this->epptr();
34623462
if (!traits_type::eq_int_type(__c, traits_type::eof())) {
34633463
if (this->pptr() == 0)
3464-
this->setp(&__1buf, &__1buf + 1);
3464+
this->setp(std::addressof(__1buf), std::addressof(__1buf) + 1);
34653465
*this->pptr() = traits_type::to_char_type(__c);
34663466
this->pbump(1);
34673467
}

0 commit comments

Comments
 (0)