From 8d6ff6fa9403c6da79cdb1e1d350208a4dc7cc45 Mon Sep 17 00:00:00 2001 From: Aleksey Kravchenko Date: Tue, 17 Nov 2026 07:26:36 +0300 Subject: [PATCH] Fix #310: Correctly use InterlockedCompareExchange() --- librhash/algorithms.c | 5 +++++ librhash/util.h | 3 ++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/librhash/algorithms.c b/librhash/algorithms.c index 22f87f0d..243a5da9 100644 --- a/librhash/algorithms.c +++ b/librhash/algorithms.c @@ -181,6 +181,11 @@ void rhash_init_algorithms(void) rhash_gost94_init_table(); #endif table_init_sha_ext(); + +#if defined(_MSC_VER) + /* ensure that (LONG*)&algorithms_initialized_flag is valid */ + RHASH_ASSERT(sizeof(LONG) == sizeof(unsigned)); +#endif atomic_compare_and_swap(&algorithms_initialized_flag, 0, 1); } diff --git a/librhash/util.h b/librhash/util.h index 1b38e8e5..f2ac4bc2 100644 --- a/librhash/util.h +++ b/librhash/util.h @@ -22,7 +22,8 @@ extern "C" { #elif defined(_MSC_VER) # define WIN32_LEAN_AND_MEAN # include -# define atomic_compare_and_swap(ptr, oldval, newval) InterlockedCompareExchange(ptr, newval, oldval) +# define atomic_compare_and_swap(ptr, oldval, newval) \ + InterlockedCompareExchange((LONG volatile*)(ptr), (LONG)(newval), (LONG)(oldval)) #elif defined(__sun) # include # define atomic_compare_and_swap(ptr, oldval, newval) atomic_cas_32(ptr, oldval, newval)