Skip to content

Commit 2586c83

Browse files
author
Roger Sanders
committed
[libc++] Removed assumption that NULL HMODULE value is safe to use
1 parent 899aed7 commit 2586c83

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

libcxx/src/atomic.cpp

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ static HMODULE win32_get_win_core_synch_api_module() {
113113
// safely use std::unique_ptr here as a wrapper for the handle, with a destructor freeing the handle when this module
114114
// is unloaded.
115115
// https://learn.microsoft.com/en-us/windows/win32/winprog/windows-data-types
116+
// https://devblogs.microsoft.com/oldnewthing/20180307-00/?p=98175
116117
static auto module_handle = std::unique_ptr<std::remove_pointer<HMODULE>::type, decltype(&FreeLibrary)>(
117118
LoadLibraryW(L"api-ms-win-core-synch-l1-2-0.dll"), &FreeLibrary);
118119
return module_handle.get();
@@ -122,7 +123,9 @@ static void
122123
__libcpp_platform_wait_on_address(__cxx_atomic_contention_t const volatile* __ptr, __cxx_contention_t __val) {
123124
// WaitOnAddress was added in Windows 8 (build 9200)
124125
static auto wait_on_address = reinterpret_cast<BOOL(WINAPI*)(volatile void*, PVOID, SIZE_T, DWORD)>(
125-
GetProcAddress(win32_get_win_core_synch_api_module(), "WaitOnAddress"));
126+
(win32_get_win_core_synch_api_module() != NULL
127+
? GetProcAddress(win32_get_win_core_synch_api_module(), "WaitOnAddress")
128+
: nullptr));
126129
if (wait_on_address != nullptr) {
127130
wait_on_address(const_cast<__cxx_atomic_contention_t*>(__ptr), &__val, sizeof(__val), INFINITE);
128131
} else {
@@ -136,7 +139,9 @@ static void __libcpp_platform_wake_by_address(__cxx_atomic_contention_t const vo
136139
if (__notify_one) {
137140
// WakeByAddressSingle was added in Windows 8 (build 9200)
138141
static auto wake_by_address_single = reinterpret_cast<void(WINAPI*)(PVOID)>(
139-
GetProcAddress(win32_get_win_core_synch_api_module(), "WakeByAddressSingle"));
142+
(win32_get_win_core_synch_api_module() != NULL
143+
? GetProcAddress(win32_get_win_core_synch_api_module(), "WakeByAddressSingle")
144+
: nullptr));
140145
if (wake_by_address_single != nullptr) {
141146
wake_by_address_single(const_cast<__cxx_atomic_contention_t*>(__ptr));
142147
} else {
@@ -146,7 +151,9 @@ static void __libcpp_platform_wake_by_address(__cxx_atomic_contention_t const vo
146151
} else {
147152
// WakeByAddressAll was added in Windows 8 (build 9200)
148153
static auto wake_by_address_all = reinterpret_cast<void(WINAPI*)(PVOID)>(
149-
GetProcAddress(win32_get_win_core_synch_api_module(), "WakeByAddressAll"));
154+
(win32_get_win_core_synch_api_module() != NULL
155+
? GetProcAddress(win32_get_win_core_synch_api_module(), "WakeByAddressAll")
156+
: nullptr));
150157
if (wake_by_address_all != nullptr) {
151158
wake_by_address_all(const_cast<__cxx_atomic_contention_t*>(__ptr));
152159
} else {

0 commit comments

Comments
 (0)