From: Michael Neumann Date: Thu, 8 Jan 2015 23:40:06 +0000 (+0100) Subject: Make pthread_*_destroy() more standards compliant X-Git-Tag: v4.2.0rc~1129 X-Git-Url: https://gitweb.dragonflybsd.org/~tuxillo/dragonfly.git/commitdiff_plain/146da5fcd4be3d6b3e74514ab38418637b600540 Make pthread_*_destroy() more standards compliant 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 --- diff --git a/lib/libthread_xu/thread/thr_cond.c b/lib/libthread_xu/thread/thr_cond.c index 0dbda34ba2..da403c4cf1 100644 --- a/lib/libthread_xu/thread/thr_cond.c +++ b/lib/libthread_xu/thread/thr_cond.c @@ -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); diff --git a/lib/libthread_xu/thread/thr_mutex.c b/lib/libthread_xu/thread/thr_mutex.c index 3ddfacc115..617d8219a1 100644 --- a/lib/libthread_xu/thread/thr_mutex.c +++ b/lib/libthread_xu/thread/thr_mutex.c @@ -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 diff --git a/lib/libthread_xu/thread/thr_rwlock.c b/lib/libthread_xu/thread/thr_rwlock.c index 385174d346..6761ef735a 100644 --- a/lib/libthread_xu/thread/thr_rwlock.c +++ b/lib/libthread_xu/thread/thr_rwlock.c @@ -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;