From dff7203ee99ccac446b9a1c4371753a5216c6db4 Mon Sep 17 00:00:00 2001 From: Elliott Hughes Date: Wed, 11 Dec 2013 14:54:00 -0800 Subject: [PATCH] Remove harmful attempts to be helpful in pthread_mutex functions. Most callers won't check for EINVAL, so it's best to fail early. GCC takes the nonnull attribute as a guarantee that an argument won't be NULL, so these hacks were already ineffective, which is how we found that at least one commercial game was using NULL as if it's a mutex, but actually getting no-op behavior. Bug: 11971278 Change-Id: I89646e043d931778805a8b692e07a34d076ee6bf --- libc/bionic/pthread_mutex.cpp | 25 ++++--------------------- 1 file changed, 4 insertions(+), 21 deletions(-) diff --git a/libc/bionic/pthread_mutex.cpp b/libc/bionic/pthread_mutex.cpp index 1010f11d4..8eaf95fc3 100644 --- a/libc/bionic/pthread_mutex.cpp +++ b/libc/bionic/pthread_mutex.cpp @@ -282,21 +282,16 @@ int pthread_mutexattr_getpshared(const pthread_mutexattr_t* attr, int* pshared) return 0; } -int pthread_mutex_init(pthread_mutex_t *mutex, - const pthread_mutexattr_t *attr) -{ - int value = 0; - - if (mutex == NULL) - return EINVAL; - +int pthread_mutex_init(pthread_mutex_t* mutex, const pthread_mutexattr_t* attr) { if (__predict_true(attr == NULL)) { mutex->value = MUTEX_TYPE_BITS_NORMAL; return 0; } - if ((*attr & MUTEXATTR_SHARED_MASK) != 0) + int value = 0; + if ((*attr & MUTEXATTR_SHARED_MASK) != 0) { value |= MUTEX_SHARED_MASK; + } switch (*attr & MUTEXATTR_TYPE_MASK) { case PTHREAD_MUTEX_NORMAL: @@ -473,9 +468,6 @@ int pthread_mutex_lock_impl(pthread_mutex_t *mutex) { int mvalue, mtype, tid, shared; - if (__predict_false(mutex == NULL)) - return EINVAL; - mvalue = mutex->value; mtype = (mvalue & MUTEX_TYPE_MASK); shared = (mvalue & MUTEX_SHARED_MASK); @@ -565,9 +557,6 @@ int pthread_mutex_unlock_impl(pthread_mutex_t *mutex) { int mvalue, mtype, tid, shared; - if (__predict_false(mutex == NULL)) - return EINVAL; - mvalue = mutex->value; mtype = (mvalue & MUTEX_TYPE_MASK); shared = (mvalue & MUTEX_SHARED_MASK); @@ -630,9 +619,6 @@ int pthread_mutex_trylock_impl(pthread_mutex_t *mutex) { int mvalue, mtype, tid, shared; - if (__predict_false(mutex == NULL)) - return EINVAL; - mvalue = mutex->value; mtype = (mvalue & MUTEX_TYPE_MASK); shared = (mvalue & MUTEX_SHARED_MASK); @@ -705,9 +691,6 @@ int pthread_mutex_lock_timeout_np_impl(pthread_mutex_t *mutex, unsigned msecs) /* compute absolute expiration time */ __timespec_to_relative_msec(&abstime, msecs, clock); - if (__predict_false(mutex == NULL)) - return EINVAL; - mvalue = mutex->value; mtype = (mvalue & MUTEX_TYPE_MASK); shared = (mvalue & MUTEX_SHARED_MASK);