From: Ralf Habacker Date: Mon, 20 Dec 2021 10:52:45 +0000 (+0100) Subject: Add assertions to the [c|r]mutex related functions on Windows X-Git-Tag: dbus-1.15.0~62^2~2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=72a7758e38be9b7b373155e39ee72b5ab1db9567;p=thirdparty%2Fdbus.git Add assertions to the [c|r]mutex related functions on Windows This detects some error conditions that can only occur as a result of a programming error, such as attempting to unlock a mutex that is not locked. Fixes #369 Signed-off-by: Ralf Habacker --- diff --git a/dbus/dbus-sysdeps-thread-win.c b/dbus/dbus-sysdeps-thread-win.c index 3b600fa91..ca4a3db59 100644 --- a/dbus/dbus-sysdeps-thread-win.c +++ b/dbus/dbus-sysdeps-thread-win.c @@ -32,6 +32,18 @@ #include +#ifdef DBUS_DISABLE_ASSERT +#define THREAD_CHECK_TRUE(func_name, result_or_call) \ + do { if (!(result_or_call)) { /* ignore */ } } while (0) +#else +#define THREAD_CHECK_TRUE(func_name, result_or_call) do { \ + if (!(result_or_call)) { \ + _dbus_warn_check_failed ("thread function %s failed (windows error code=%ld) in %s", \ + func_name, GetLastError (), _DBUS_FUNCTION_NAME); \ + } \ +} while (0) +#endif /* !DBUS_DISABLE_ASSERT */ + /* Protected by DllMain lock, effectively */ static dbus_bool_t global_init_done = FALSE; static CRITICAL_SECTION init_lock; @@ -113,6 +125,7 @@ _dbus_platform_cmutex_new (void) { HANDLE handle; handle = CreateMutex (NULL, FALSE, NULL); + THREAD_CHECK_TRUE ("CreateMutex", handle); return (DBusCMutex *) handle; } @@ -121,43 +134,44 @@ _dbus_platform_rmutex_new (void) { HANDLE handle; handle = CreateMutex (NULL, FALSE, NULL); + THREAD_CHECK_TRUE ("CreateMutex", handle); return (DBusRMutex *) handle; } void _dbus_platform_cmutex_free (DBusCMutex *mutex) { - CloseHandle ((HANDLE *) mutex); + THREAD_CHECK_TRUE ("CloseHandle", CloseHandle ((HANDLE *) mutex)); } void _dbus_platform_rmutex_free (DBusRMutex *mutex) { - CloseHandle ((HANDLE *) mutex); + THREAD_CHECK_TRUE ("CloseHandle", CloseHandle ((HANDLE *) mutex)); } void _dbus_platform_cmutex_lock (DBusCMutex *mutex) { - WaitForSingleObject ((HANDLE *) mutex, INFINITE); + THREAD_CHECK_TRUE ("WaitForSingleObject", WaitForSingleObject ((HANDLE *) mutex, INFINITE) == WAIT_OBJECT_0); } void _dbus_platform_rmutex_lock (DBusRMutex *mutex) { - WaitForSingleObject ((HANDLE *) mutex, INFINITE); + THREAD_CHECK_TRUE ("WaitForSingleObject", WaitForSingleObject ((HANDLE *) mutex, INFINITE) == WAIT_OBJECT_0); } void _dbus_platform_cmutex_unlock (DBusCMutex *mutex) { - ReleaseMutex ((HANDLE *) mutex); + THREAD_CHECK_TRUE ("ReleaseMutex", ReleaseMutex ((HANDLE *) mutex)); } void _dbus_platform_rmutex_unlock (DBusRMutex *mutex) { - ReleaseMutex ((HANDLE *) mutex); + THREAD_CHECK_TRUE ("ReleaseMutex", ReleaseMutex ((HANDLE *) mutex)); } DBusCondVar *