[krbdev.mit.edu #6090] k5_mutex_destroy calls pthread_mutex_destroy with mutex locked

Ezra Peisach via RT rt-comment at krbdev.mit.edu
Wed Aug 20 15:38:49 EDT 2008


Detected with the development version of valgrind in which helgrind
actually works.

It complains that calls to pthread_mutex_destroy were made w/ the mutex
locked - which is a no-no. It returns EBUSY which according to the man
page means" The implementation has detected an attempt to destroy the
object referenced by mutex while it is locked or referenced"

Sure enough there is a logic bug in the macro k5_mutex_destroy.  The use
of "&&" in the following:

k5_mutex_lock(M) && ((M)->loc_last = K5_DEBUG_LOC, k5_mutex_unlock(M)),  \
k5_os_mutex_destroy(&(M)->os))


k5_mutex_lock will return 0 on success. Therefore the rest of the
expression is not evaluated - and the mutex will not be unlocked!!

! before the lock request solves the problem:

===================================================================
--- k5-thread.h (revision 20676)
+++ k5-thread.h (working copy)
@@ -693,7 +694,7 @@
 #define k5_mutex_destroy(M)                    \
        (k5_os_mutex_assert_unlocked(&(M)->os), \
         krb5int_mutex_report_stats(M),         \
-        k5_mutex_lock(M) && ((M)->loc_last = K5_DEBUG_LOC,
k5_mutex_unlock(M)),
 \
+        !k5_mutex_lock(M) && ((M)->loc_last = K5_DEBUG_LOC,
k5_mutex_unlock(M))
, \
         k5_os_mutex_destroy(&(M)->os))

 #if __GNUC__ >= 4




More information about the krb5-bugs mailing list