summaryrefslogtreecommitdiff
path: root/src/posix-lock.c
diff options
context:
space:
mode:
authorWerner Koch <wk@gnupg.org>2014-01-09 19:14:09 +0100
committerWerner Koch <wk@gnupg.org>2014-01-24 16:29:33 +0100
commit4e8a88e7632b1f1d2b31ae738a159802549882c3 (patch)
tree606798aee980c1ee1f250b271177039c75d35902 /src/posix-lock.c
parente07538c0ed3c5cb3d870a490a4c12bef4375278a (diff)
downloadlibgpg-error-4e8a88e7632b1f1d2b31ae738a159802549882c3.tar.gz
Allow using gpgrt_lock_init on an unitialized variable.
* src/posix-lock.c (gpgrt_lock_init): Detect unitialized lock var. * src/w32-lock.c (gpgrt_lock_init): Ditto. -- gpgrt_lock_init may be used for dynamically allocated locks. For example gpgrt_lock_t *lock = xcalloc (1, sizeof *lock); gpgrt_lock_init (lock); gpgrt_lock_lock (lock); foo (); gpgrt_lock_unlock (lock); gpgrt_lock_destroy (lock); free (lock); This patch actually allows for this. Signed-off-by: Werner Koch <wk@gnupg.org>
Diffstat (limited to 'src/posix-lock.c')
-rw-r--r--src/posix-lock.c16
1 files changed, 14 insertions, 2 deletions
diff --git a/src/posix-lock.c b/src/posix-lock.c
index 363cc09..5b0cab5 100644
--- a/src/posix-lock.c
+++ b/src/posix-lock.c
@@ -116,9 +116,21 @@ get_lock_object (gpgrt_lock_t *lockhd)
gpg_err_code_t
gpgrt_lock_init (gpgrt_lock_t *lockhd)
{
- _gpgrt_lock_t *lock = get_lock_object (lockhd);
+ _gpgrt_lock_t *lock = (_gpgrt_lock_t*)lockhd;
int rc;
+ /* If VERS is zero we assume that no static initialization has been
+ done, so we setup our ABI version right here. The caller might
+ have called us to test whether lock support is at all available. */
+ if (!lock->vers)
+ {
+ if (sizeof (gpgrt_lock_t) < sizeof (_gpgrt_lock_t))
+ abort ();
+ lock->vers = LOCK_ABI_VERSION;
+ }
+ else /* Run the usual check. */
+ lock = get_lock_object (lockhd);
+
#if USE_POSIX_THREADS
if (use_pthread_p())
{
@@ -198,7 +210,7 @@ gpgrt_lock_destroy (gpgrt_lock_t *lockhd)
rc = gpg_err_code_from_errno (rc);
else
{
- /* Re-init the the mutex so that it can be re-used. */
+ /* Re-init the mutex so that it can be re-used. */
gpgrt_lock_t tmp = GPGRT_LOCK_INITIALIZER;
memcpy (lockhd, &tmp, sizeof tmp);
}