Make pthread_*_destroy() more standards compliant
authorMichael Neumann <mneumann@ntecs.de>
Thu, 8 Jan 2015 23:40:06 +0000 (00:40 +0100)
committerMichael Neumann <mneumann@ntecs.de>
Thu, 8 Jan 2015 23:40:06 +0000 (00:40 +0100)
Function pthread_{mutex,cond,rwlock}_destroy() returned EINVAL when the
mutex/cond/rwlock was initialized statically via one of the
PTHREAD_*_INITIALIZER macros and not being used before destruction. We now
return success (0) instead, as it would have been the case when the *_init()
function were used for initialization. This is also the behaviour Linux
exhibits.

Note that we now can no longer detect multiple calls to *_destroy(). Multiple
calls will do no harm, but return success.

While there, fix some potential null pointer derefs in cond and rwlock.

Fixes: #2763

lib/libthread_xu/thread/thr_cond.c
lib/libthread_xu/thread/thr_mutex.c
lib/libthread_xu/thread/thr_rwlock.c

index 0dbda34..da403c4 100644 (file)
@@ -111,8 +111,10 @@ _pthread_cond_destroy(pthread_cond_t *cond)
        struct pthread          *curthread = tls_get_curthread();
        int                     rval = 0;
 
-       if (*cond == NULL)
+       if (cond == NULL)
                rval = EINVAL;
+       else if (*cond == NULL)
+               rval = 0;
        else {
                /* Lock the condition variable structure: */
                THR_LOCK_ACQUIRE(curthread, &(*cond)->c_lock);
index 3ddfacc..617d821 100644 (file)
@@ -213,8 +213,10 @@ _pthread_mutex_destroy(pthread_mutex_t *mutex)
        pthread_mutex_t m;
        int ret = 0;
 
-       if (mutex == NULL || *mutex == NULL)
+       if (mutex == NULL)
                ret = EINVAL;
+       else if (*mutex == NULL)
+               ret = 0;
        else {
                /*
                 * Try to lock the mutex structure, we only need to
index 385174d..6761ef7 100644 (file)
@@ -92,6 +92,8 @@ _pthread_rwlock_destroy (pthread_rwlock_t *rwlock)
 
        if (rwlock == NULL)
                ret = EINVAL;
+       else if (*rwlock == NULL)
+               ret = 0;
        else {
                pthread_rwlock_t prwlock;