Skip to content

Commit df7fa52

Browse files
committed
shims: fix a subtle bug in semaphore initialisation on Windows
This function is the initializer for the semaphore. The seamphore storage itself may be stack allocated (or heap allocated) but without guarantee of 0-initialisation. As a result, the subsequent CAS for the atomic replacement will fail silently, leaving the previously non-zero value in place, indicating that the value is a valid handle. This would fail randomly and would ultimately result in a crash in the `CloseHandle` call associated with the clean up. This issue was identified by SwiftLint on Windows.
1 parent 0180b4e commit df7fa52

File tree

1 file changed

+1
-0
lines changed

1 file changed

+1
-0
lines changed

src/shims/lock.c

+1
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,7 @@ void _dispatch_sema4_init(_dispatch_sema4_t *sema, int policy DISPATCH_UNUSED)
266266

267267
// lazily allocate the semaphore port
268268

269+
os_atomic_cmpxchg(sema, *sema, 0, relaxed);
269270
while (!dispatch_assume(tmp = CreateSemaphore(NULL, 0, LONG_MAX, NULL))) {
270271
_dispatch_temporary_resource_shortage();
271272
}

0 commit comments

Comments
 (0)