]> git.ipfire.org Git - thirdparty/gnutls.git/commitdiff
locks: define lock functions as a macro
authorDaiki Ueno <ueno@gnu.org>
Wed, 9 Mar 2022 07:07:58 +0000 (08:07 +0100)
committerDaiki Ueno <ueno@gnu.org>
Wed, 9 Mar 2022 07:35:41 +0000 (08:35 +0100)
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 <ueno@gnu.org>
lib/locks.c
lib/locks.h
lib/pkcs11.c
lib/priority.c
lib/verify-tofu.c

index d61504e077d67aec3433ac651d7f9227ec5da9ec..8888ed6b129a8ee407dee64cdb0f629440623f7f 100644 (file)
@@ -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;
-}
index a1ff0fa9d7782f054d8a76cd3020cc2c837527b2..907adfc134253bc1e5191212777084a7b1aab7b6 100644 (file)
@@ -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 */
index 8dda0f07c9014f650d056e160df7c5821771aa68..ba8ac0c37527489868cc1a32ed934407a37e13ec 100644 (file)
@@ -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;
 }
index d17a9233184d3c33b8f85039be187097c804b619..55ae185c1fdaf8dd480d28ccf85e6f546f44894f 100644 (file)
@@ -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;
 }
 
index 40b7acdc8ad494e703359166d2e27bb2cbbe0558..97f47385e610add34f3b46e4cd68abe3ae013649 100644 (file)
@@ -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;