From 5803bfa3d4febdcf32494e2c2c8f4731cb8be7cd Mon Sep 17 00:00:00 2001 From: Daiki Ueno Date: Wed, 9 Mar 2022 08:07:58 +0100 Subject: [PATCH] locks: define lock functions as a macro When threads are not supported, glthread_* functions are defined as no-op and thus dereferencing lock variables in inline functions will cause compilation error. This change fixes it by redefining our lock functions as a macro so it will also be compiled out. Reported by Fabrice Fontaine in: https://gitlab.com/gnutls/gnutls/-/issues/1330 Signed-off-by: Daiki Ueno [Retrieved from: https://gitlab.com/gnutls/gnutls/-/commit/5803bfa3d4febdcf32494e2c2c8f4731cb8be7cd] Signed-off-by: Fabrice Fontaine --- lib/locks.c | 54 ----------------------------------------------- lib/locks.h | 28 ++++++++++++++++++------ lib/pkcs11.c | 2 +- lib/priority.c | 2 +- lib/verify-tofu.c | 2 +- 5 files changed, 25 insertions(+), 63 deletions(-) diff --git a/lib/locks.c b/lib/locks.c index d61504e077..8888ed6b12 100644 --- a/lib/locks.c +++ b/lib/locks.c @@ -63,57 +63,3 @@ gnutls_global_set_mutex(mutex_init_func init, mutex_deinit_func deinit, gnutls_mutex_lock = lock; gnutls_mutex_unlock = unlock; } - -int -gnutls_static_mutex_lock(gnutls_static_mutex_t lock) -{ - if (unlikely(glthread_lock_lock(lock))) { - return gnutls_assert_val(GNUTLS_E_LOCKING_ERROR); - } - return 0; -} - -int -gnutls_static_mutex_unlock(gnutls_static_mutex_t lock) -{ - if (unlikely(glthread_lock_unlock(lock))) { - return gnutls_assert_val(GNUTLS_E_LOCKING_ERROR); - } - return 0; -} - -int -gnutls_rwlock_rdlock(gnutls_rwlock_t rwlock) -{ - if (unlikely(glthread_rwlock_rdlock(rwlock))) { - return gnutls_assert_val(GNUTLS_E_LOCKING_ERROR); - } - return 0; -} - -int -gnutls_rwlock_wrlock(gnutls_rwlock_t rwlock) -{ - if (unlikely(glthread_rwlock_wrlock(rwlock))) { - return gnutls_assert_val(GNUTLS_E_LOCKING_ERROR); - } - return 0; -} - -int -gnutls_rwlock_unlock(gnutls_rwlock_t rwlock) -{ - if (unlikely(glthread_rwlock_unlock(rwlock))) { - return gnutls_assert_val(GNUTLS_E_LOCKING_ERROR); - } - return 0; -} - -int -gnutls_once(gnutls_once_t once, void (*init_func) (void)) -{ - if (unlikely(glthread_once(once, init_func))) { - return gnutls_assert_val(GNUTLS_E_LOCKING_ERROR); - } - return 0; -} diff --git a/lib/locks.h b/lib/locks.h index a1ff0fa9d7..907adfc134 100644 --- a/lib/locks.h +++ b/lib/locks.h @@ -40,8 +40,14 @@ extern mutex_unlock_func gnutls_mutex_unlock; */ #define GNUTLS_STATIC_MUTEX(lock) gl_lock_define_initialized(static, lock) typedef gl_lock_t *gnutls_static_mutex_t; -int gnutls_static_mutex_lock(gnutls_static_mutex_t lock); -int gnutls_static_mutex_unlock(gnutls_static_mutex_t lock); + +#define gnutls_static_mutex_lock(LOCK) \ + (unlikely(glthread_lock_lock(LOCK)) ? \ + gnutls_assert_val(GNUTLS_E_LOCKING_ERROR) : 0) + +#define gnutls_static_mutex_unlock(LOCK) \ + (unlikely(glthread_lock_unlock(LOCK)) ? \ + gnutls_assert_val(GNUTLS_E_LOCKING_ERROR) : 0) /* Unlike static mutexes, static rwlocks can be locked/unlocked with * the functions defined below, because there is no way to replace @@ -50,13 +56,23 @@ int gnutls_static_mutex_unlock(gnutls_static_mutex_t lock); #define GNUTLS_RWLOCK(rwlock) gl_rwlock_define_initialized(static, rwlock) typedef gl_rwlock_t *gnutls_rwlock_t; -int gnutls_rwlock_rdlock(gnutls_rwlock_t rwlock); -int gnutls_rwlock_wrlock(gnutls_rwlock_t rwlock); -int gnutls_rwlock_unlock(gnutls_rwlock_t rwlock); +#define gnutls_rwlock_rdlock(RWLOCK) \ + (unlikely(glthread_rwlock_rdlock(RWLOCK)) ? \ + gnutls_assert_val(GNUTLS_E_LOCKING_ERROR) : 0) + +#define gnutls_rwlock_wrlock(RWLOCK) \ + (unlikely(glthread_rwlock_wrlock(RWLOCK)) ? \ + gnutls_assert_val(GNUTLS_E_LOCKING_ERROR) : 0) + +#define gnutls_rwlock_unlock(RWLOCK) \ + (unlikely(glthread_rwlock_unlock(RWLOCK)) ? \ + gnutls_assert_val(GNUTLS_E_LOCKING_ERROR) : 0) #define GNUTLS_ONCE(once) gl_once_define(static, once) typedef gl_once_t *gnutls_once_t; -int gnutls_once(gnutls_once_t once, void (*init_func) (void)); +#define gnutls_once(ONCE, INIT_FUNC) \ + (unlikely(glthread_once(ONCE, INIT_FUNC)) ? \ + gnutls_assert_val(GNUTLS_E_LOCKING_ERROR) : 0) #endif /* GNUTLS_LIB_LOCKS_H */ diff --git a/lib/pkcs11.c b/lib/pkcs11.c index 8dda0f07c9..ba8ac0c375 100644 --- a/lib/pkcs11.c +++ b/lib/pkcs11.c @@ -351,7 +351,7 @@ int _gnutls_pkcs11_check_init(init_level_t req_level, void *priv, pkcs11_reinit_ ret = sret; cleanup: - gnutls_static_mutex_unlock(&pkcs11_mutex); + (void)gnutls_static_mutex_unlock(&pkcs11_mutex); return ret; } diff --git a/lib/priority.c b/lib/priority.c index d17a923318..55ae185c1f 100644 --- a/lib/priority.c +++ b/lib/priority.c @@ -2505,7 +2505,7 @@ static int set_ciphersuite_list(gnutls_priority_t priority_cache) } out: - gnutls_rwlock_unlock(&system_wide_config_rwlock); + (void)gnutls_rwlock_unlock(&system_wide_config_rwlock); return ret; } diff --git a/lib/verify-tofu.c b/lib/verify-tofu.c index 40b7acdc8a..97f47385e6 100644 --- a/lib/verify-tofu.c +++ b/lib/verify-tofu.c @@ -434,7 +434,7 @@ int store_pubkey(const char *db_name, const char *host, if (fp != NULL) fclose(fp); - gnutls_static_mutex_unlock(&file_mutex); + (void)gnutls_static_mutex_unlock(&file_mutex); gnutls_free(b64key.data); return ret; -- GitLab