Skip to content

Commit dcd0aaa

Browse files
authored
Don't cast HANDLE to usize and back (#635)
1 parent 72265be commit dcd0aaa

File tree

1 file changed

+9
-10
lines changed

1 file changed

+9
-10
lines changed

src/dbghelp.rs

+9-10
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ pub struct Init {
287287
/// synchronization. Also note that it is safe to call this function multiple
288288
/// times recursively.
289289
pub fn init() -> Result<Init, ()> {
290-
use core::sync::atomic::{AtomicUsize, Ordering::SeqCst};
290+
use core::sync::atomic::{AtomicPtr, Ordering::SeqCst};
291291

292292
// Helper function for generating a name that's unique to the process.
293293
fn mutex_name() -> [u8; 33] {
@@ -341,22 +341,21 @@ pub fn init() -> Result<Init, ()> {
341341
//
342342
// After we've actually go the lock we simply acquire it, and our `Init`
343343
// handle we hand out will be responsible for dropping it eventually.
344-
static LOCK: AtomicUsize = AtomicUsize::new(0);
344+
static LOCK: AtomicPtr<c_void> = AtomicPtr::new(ptr::null_mut());
345345
let mut lock = LOCK.load(SeqCst);
346-
if lock == 0 {
346+
if lock.is_null() {
347347
let name = mutex_name();
348-
lock = CreateMutexA(ptr::null_mut(), 0, name.as_ptr().cast::<i8>()) as usize;
349-
if lock == 0 {
348+
lock = CreateMutexA(ptr::null_mut(), 0, name.as_ptr().cast::<i8>());
349+
if lock.is_null() {
350350
return Err(());
351351
}
352-
if let Err(other) = LOCK.compare_exchange(0, lock, SeqCst, SeqCst) {
353-
debug_assert!(other != 0);
354-
CloseHandle(lock as HANDLE);
352+
if let Err(other) = LOCK.compare_exchange(ptr::null_mut(), lock, SeqCst, SeqCst) {
353+
debug_assert!(!other.is_null());
354+
CloseHandle(lock);
355355
lock = other;
356356
}
357357
}
358-
debug_assert!(lock != 0);
359-
let lock = lock as HANDLE;
358+
debug_assert!(!lock.is_null());
360359
let r = WaitForSingleObjectEx(lock, INFINITE, FALSE);
361360
debug_assert_eq!(r, 0);
362361
let ret = Init { lock };

0 commit comments

Comments
 (0)