Skip to content

Commit b271b44

Browse files
authored
[libc++] Guard call_once against operator hijacking. (#128054)
1 parent 6c7c660 commit b271b44

File tree

3 files changed

+21
-4
lines changed

3 files changed

+21
-4
lines changed

libcxx/include/__mutex/once_flag.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
#include <__config>
1313
#include <__functional/invoke.h>
14+
#include <__memory/addressof.h>
1415
#include <__memory/shared_count.h> // __libcpp_acquire_load
1516
#include <__tuple/tuple_indices.h>
1617
#include <__tuple/tuple_size.h>
@@ -128,7 +129,7 @@ inline _LIBCPP_HIDE_FROM_ABI void call_once(once_flag& __flag, _Callable&& __fun
128129
typedef tuple<_Callable&&, _Args&&...> _Gp;
129130
_Gp __f(std::forward<_Callable>(__func), std::forward<_Args>(__args)...);
130131
__call_once_param<_Gp> __p(__f);
131-
std::__call_once(__flag.__state_, &__p, &__call_once_proxy<_Gp>);
132+
std::__call_once(__flag.__state_, std::addressof(__p), std::addressof(__call_once_proxy<_Gp>));
132133
}
133134
}
134135

@@ -138,15 +139,15 @@ template <class _Callable>
138139
inline _LIBCPP_HIDE_FROM_ABI void call_once(once_flag& __flag, _Callable& __func) {
139140
if (__libcpp_acquire_load(&__flag.__state_) != once_flag::_Complete) {
140141
__call_once_param<_Callable> __p(__func);
141-
std::__call_once(__flag.__state_, &__p, &__call_once_proxy<_Callable>);
142+
std::__call_once(__flag.__state_, std::addressof(__p), std::addressof(__call_once_proxy<_Callable>));
142143
}
143144
}
144145

145146
template <class _Callable>
146147
inline _LIBCPP_HIDE_FROM_ABI void call_once(once_flag& __flag, const _Callable& __func) {
147148
if (__libcpp_acquire_load(&__flag.__state_) != once_flag::_Complete) {
148149
__call_once_param<const _Callable> __p(__func);
149-
std::__call_once(__flag.__state_, &__p, &__call_once_proxy<const _Callable>);
150+
std::__call_once(__flag.__state_, std::addressof(__p), std::addressof(__call_once_proxy<const _Callable>));
150151
}
151152
}
152153

libcxx/test/std/thread/thread.mutex/thread.once/thread.once.callonce/call_once.pass.cpp

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
// UNSUPPORTED: no-threads
1010

11+
// XFAIL: FROZEN-CXX03-HEADERS-FIXME
12+
1113
// <mutex>
1214

1315
// struct once_flag;
@@ -21,6 +23,7 @@
2123

2224
#include "make_test_thread.h"
2325
#include "test_macros.h"
26+
#include "operator_hijacker.h"
2427

2528
typedef std::chrono::milliseconds ms;
2629

@@ -253,7 +256,19 @@ int main(int, char**)
253256
std::call_once(f2, std::move(rq));
254257
assert(rq.rv_called == 1);
255258
}
259+
{
260+
std::once_flag flag;
261+
auto f = [](const operator_hijacker&) {};
262+
std::call_once(flag, f, operator_hijacker{});
263+
}
264+
256265
#endif // TEST_STD_VER >= 11
257266

258-
return 0;
267+
{
268+
std::once_flag flag;
269+
operator_hijacker f;
270+
std::call_once(flag, f);
271+
}
272+
273+
return 0;
259274
}

libcxx/test/support/operator_hijacker.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
struct operator_hijacker {
2424
TEST_CONSTEXPR bool operator<(const operator_hijacker&) const { return true; }
2525
TEST_CONSTEXPR bool operator==(const operator_hijacker&) const { return true; }
26+
TEST_CONSTEXPR int operator()() const { return 42; }
2627

2728
template <typename T>
2829
friend void operator&(T&&) = delete;

0 commit comments

Comments
 (0)