Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/lib/libpthread Refactor libpthread checks for invalid arguments



details:   https://anonhg.NetBSD.org/src/rev/a629cd7f0b58
branches:  trunk
changeset: 744401:a629cd7f0b58
user:      kamil <kamil%NetBSD.org@localhost>
date:      Fri Jan 31 17:52:14 2020 +0000

description:
Refactor libpthread checks for invalid arguments

Switch from manual functions to pthread__error().

diffstat:

 lib/libpthread/pthread_mutex.c  |   7 ++++-
 lib/libpthread/pthread_rwlock.c |  48 ++++++++++++++++++++++++----------------
 lib/libpthread/pthread_spin.c   |  27 +++++++++++-----------
 3 files changed, 48 insertions(+), 34 deletions(-)

diffs (236 lines):

diff -r c58c043a915d -r a629cd7f0b58 lib/libpthread/pthread_mutex.c
--- a/lib/libpthread/pthread_mutex.c    Fri Jan 31 14:01:36 2020 +0000
+++ b/lib/libpthread/pthread_mutex.c    Fri Jan 31 17:52:14 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: pthread_mutex.c,v 1.71 2020/01/31 02:37:46 christos Exp $      */
+/*     $NetBSD: pthread_mutex.c,v 1.72 2020/01/31 17:52:14 kamil Exp $ */
 
 /*-
  * Copyright (c) 2001, 2003, 2006, 2007, 2008 The NetBSD Foundation, Inc.
@@ -47,7 +47,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: pthread_mutex.c,v 1.71 2020/01/31 02:37:46 christos Exp $");
+__RCSID("$NetBSD: pthread_mutex.c,v 1.72 2020/01/31 17:52:14 kamil Exp $");
 
 #include <sys/types.h>
 #include <sys/lwpctl.h>
@@ -131,6 +131,9 @@
                return __libc_mutex_init_stub(ptm, attr);
 #endif
 
+       pthread__error(EINVAL, "Invalid mutes attribute",
+           attr == NULL || attr->ptma_magic == _PT_MUTEXATTR_MAGIC);
+
        if (attr == NULL) {
                type = PTHREAD_MUTEX_NORMAL;
                proto = PTHREAD_PRIO_NONE;
diff -r c58c043a915d -r a629cd7f0b58 lib/libpthread/pthread_rwlock.c
--- a/lib/libpthread/pthread_rwlock.c   Fri Jan 31 14:01:36 2020 +0000
+++ b/lib/libpthread/pthread_rwlock.c   Fri Jan 31 17:52:14 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: pthread_rwlock.c,v 1.37 2020/01/13 18:22:56 ad Exp $ */
+/*     $NetBSD: pthread_rwlock.c,v 1.38 2020/01/31 17:52:14 kamil Exp $ */
 
 /*-
  * Copyright (c) 2002, 2006, 2007, 2008 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: pthread_rwlock.c,v 1.37 2020/01/13 18:22:56 ad Exp $");
+__RCSID("$NetBSD: pthread_rwlock.c,v 1.38 2020/01/31 17:52:14 kamil Exp $");
 
 #include <sys/types.h>
 #include <sys/lwpctl.h>
@@ -91,8 +91,9 @@
        if (__predict_false(__uselibcstub))
                return __libc_rwlock_init_stub(ptr, attr);
 
-       if (attr && (attr->ptra_magic != _PT_RWLOCKATTR_MAGIC))
-               return EINVAL;
+       pthread__error(EINVAL, "Invalid rwlock attribute",
+           attr == NULL || attr->ptra_magic == _PT_RWLOCKATTR_MAGIC);
+
        ptr->ptr_magic = _PT_RWLOCK_MAGIC;
        PTQ_INIT(&ptr->ptr_rblocked);
        PTQ_INIT(&ptr->ptr_wblocked);
@@ -109,8 +110,10 @@
        if (__predict_false(__uselibcstub))
                return __libc_rwlock_destroy_stub(ptr);
 
-       if ((ptr->ptr_magic != _PT_RWLOCK_MAGIC) ||
-           (!PTQ_EMPTY(&ptr->ptr_rblocked)) ||
+       pthread__error(EINVAL, "Invalid rwlock",
+           ptr->ptr_magic == _PT_RWLOCK_MAGIC);
+
+       if ((!PTQ_EMPTY(&ptr->ptr_rblocked)) ||
            (!PTQ_EMPTY(&ptr->ptr_wblocked)) ||
            (ptr->ptr_nreaders != 0) ||
            (ptr->ptr_owner != NULL))
@@ -156,8 +159,8 @@
        int error;
 
 #ifdef ERRORCHECK
-       if (ptr->ptr_magic != _PT_RWLOCK_MAGIC)
-               return EINVAL;
+       pthread__error(EINVAL, "Invalid rwlock",
+           ptr->ptr_magic == _PT_RWLOCK_MAGIC);
 #endif
 
        for (owner = (uintptr_t)ptr->ptr_owner;; owner = next) {
@@ -246,8 +249,8 @@
                return __libc_rwlock_tryrdlock_stub(ptr);
 
 #ifdef ERRORCHECK
-       if (ptr->ptr_magic != _PT_RWLOCK_MAGIC)
-               return EINVAL;
+       pthread__error(EINVAL, "Invalid rwlock",
+           ptr->ptr_magic == _PT_RWLOCK_MAGIC);
 #endif
 
        /*
@@ -281,8 +284,8 @@
        _DIAGASSERT(((uintptr_t)self & RW_FLAGMASK) == 0);
 
 #ifdef ERRORCHECK
-       if (ptr->ptr_magic != _PT_RWLOCK_MAGIC)
-               return EINVAL;
+       pthread__error(EINVAL, "Invalid rwlock",
+           ptr->ptr_magic == _PT_RWLOCK_MAGIC);
 #endif
 
        for (owner = (uintptr_t)ptr->ptr_owner;; owner = next) {
@@ -372,8 +375,8 @@
                return __libc_rwlock_trywrlock_stub(ptr);
 
 #ifdef ERRORCHECK
-       if (ptr->ptr_magic != _PT_RWLOCK_MAGIC)
-               return EINVAL;
+       pthread__error(EINVAL, "Invalid rwlock",
+           ptr->ptr_magic == _PT_RWLOCK_MAGIC);
 #endif
 
        self = pthread__self();
@@ -451,8 +454,8 @@
                return __libc_rwlock_unlock_stub(ptr);
 
 #ifdef ERRORCHECK
-       if ((ptr == NULL) || (ptr->ptr_magic != _PT_RWLOCK_MAGIC))
-               return EINVAL;
+       pthread__error(EINVAL, "Invalid rwlock",
+           ptr->ptr_magic == _PT_RWLOCK_MAGIC);
 #endif
 
 #ifndef PTHREAD__ATOMIC_IS_MEMBAR
@@ -650,6 +653,10 @@
 pthread_rwlockattr_getpshared(const pthread_rwlockattr_t * __restrict attr,
     int * __restrict pshared)
 {
+
+       pthread__error(EINVAL, "Invalid rwlock attribute",
+           ptr->ptra_magic == _PT_RWLOCKATTR_MAGIC);
+
        *pshared = PTHREAD_PROCESS_PRIVATE;
        return 0;
 }
@@ -658,6 +665,9 @@
 pthread_rwlockattr_setpshared(pthread_rwlockattr_t *attr, int pshared)
 {
 
+       pthread__error(EINVAL, "Invalid rwlock attribute",
+           ptr->ptra_magic == _PT_RWLOCKATTR_MAGIC);
+
        switch(pshared) {
        case PTHREAD_PROCESS_PRIVATE:
                return 0;
@@ -684,9 +694,9 @@
 pthread_rwlockattr_destroy(pthread_rwlockattr_t *attr)
 {
 
-       if ((attr == NULL) ||
-           (attr->ptra_magic != _PT_RWLOCKATTR_MAGIC))
-               return EINVAL;
+       pthread__error(EINVAL, "Invalid rwlock attribute",
+           attr->ptra_magic == _PT_RWLOCKATTR_MAGIC);
+
        attr->ptra_magic = _PT_RWLOCKATTR_DEAD;
 
        return 0;
diff -r c58c043a915d -r a629cd7f0b58 lib/libpthread/pthread_spin.c
--- a/lib/libpthread/pthread_spin.c     Fri Jan 31 14:01:36 2020 +0000
+++ b/lib/libpthread/pthread_spin.c     Fri Jan 31 17:52:14 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: pthread_spin.c,v 1.6 2012/08/16 04:49:47 matt Exp $    */
+/*     $NetBSD: pthread_spin.c,v 1.7 2020/01/31 17:52:14 kamil Exp $   */
 
 /*-
  * Copyright (c) 2001, 2006, 2007 The NetBSD Foundation, Inc.
@@ -34,7 +34,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: pthread_spin.c,v 1.6 2012/08/16 04:49:47 matt Exp $");
+__RCSID("$NetBSD: pthread_spin.c,v 1.7 2020/01/31 17:52:14 kamil Exp $");
 
 #include <sys/types.h>
 #include <sys/ras.h>
@@ -54,9 +54,9 @@
 {
 
 #ifdef ERRORCHECK
-       if (lock == NULL || (pshared != PTHREAD_PROCESS_PRIVATE &&
-           pshared != PTHREAD_PROCESS_SHARED))
-               return EINVAL;
+       pthread__error(EINVAL, "Invalid pshared",
+           pshared == PTHREAD_PROCESS_PRIVATE ||
+           pshared == PTHREAD_PROCESS_SHARED);
 #endif
        lock->pts_magic = _PT_SPINLOCK_MAGIC;
 
@@ -76,8 +76,9 @@
 {
 
 #ifdef ERRORCHECK
-       if (lock == NULL || lock->pts_magic != _PT_SPINLOCK_MAGIC)
-               return EINVAL;
+       pthread__error(EINVAL, "Invalid spinlock",
+           lock->pts_magic == _PT_SPINLOCK_MAGIC);
+
        if (!__SIMPLELOCK_UNLOCKED_P(&lock->pts_spin))
                return EBUSY;
 #endif
@@ -93,8 +94,8 @@
        pthread_t self;
 
 #ifdef ERRORCHECK
-       if (lock == NULL || lock->pts_magic != _PT_SPINLOCK_MAGIC)
-               return EINVAL;
+       pthread__error(EINVAL, "Invalid spinlock",
+           lock->pts_magic == _PT_SPINLOCK_MAGIC);
 #endif
 
        self = pthread__self();
@@ -111,8 +112,8 @@
        pthread_t self;
 
 #ifdef ERRORCHECK
-       if (lock == NULL || lock->pts_magic != _PT_SPINLOCK_MAGIC)
-               return EINVAL;
+       pthread__error(EINVAL, "Invalid spinlock",
+           lock->pts_magic == _PT_SPINLOCK_MAGIC);
 #endif
 
        self = pthread__self();
@@ -127,8 +128,8 @@
        pthread_t self;
 
 #ifdef ERRORCHECK
-       if (lock == NULL || lock->pts_magic != _PT_SPINLOCK_MAGIC)
-               return EINVAL;
+       pthread__error(EINVAL, "Invalid spinlock",
+           lock->pts_magic == _PT_SPINLOCK_MAGIC);
 #endif
 
        self = pthread__self();



Home | Main Index | Thread Index | Old Index