Source-Changes-HG archive

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

[src/trunk]: src/lib/libpthread Chack thread->pt_magic with PT_MAGIC promptly



details:   https://anonhg.NetBSD.org/src/rev/51a32902275c
branches:  trunk
changeset: 744320:51a32902275c
user:      kamil <kamil%NetBSD.org@localhost>
date:      Wed Jan 29 16:03:44 2020 +0000

description:
Chack thread->pt_magic with PT_MAGIC promptly

Rearrange some checks to avoid verifying pthread_t after using it.

diffstat:

 lib/libpthread/pthread.c               |  44 +++++++++++++++++++++++----------
 lib/libpthread/pthread_getcpuclockid.c |   7 +++-
 2 files changed, 35 insertions(+), 16 deletions(-)

diffs (159 lines):

diff -r 83748c726ae1 -r 51a32902275c lib/libpthread/pthread.c
--- a/lib/libpthread/pthread.c  Wed Jan 29 15:47:51 2020 +0000
+++ b/lib/libpthread/pthread.c  Wed Jan 29 16:03:44 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: pthread.c,v 1.160 2020/01/29 15:31:14 kamil Exp $      */
+/*     $NetBSD: pthread.c,v 1.161 2020/01/29 16:03:44 kamil Exp $      */
 
 /*-
  * Copyright (c) 2001, 2002, 2003, 2006, 2007, 2008, 2020
@@ -31,7 +31,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: pthread.c,v 1.160 2020/01/29 15:31:14 kamil Exp $");
+__RCSID("$NetBSD: pthread.c,v 1.161 2020/01/29 16:03:44 kamil Exp $");
 
 #define        __EXPOSE_STACK  1
 
@@ -597,6 +597,9 @@
 {
        pthread_t self;
 
+       pthread__error(EINVAL, "Invalid thread",
+           thread->pt_magic == PT_MAGIC);
+
        self = pthread__self();
        if (self == thread) {
                return EDEADLK;
@@ -611,6 +614,9 @@
 int
 pthread_resume_np(pthread_t thread)
 {
+
+       pthread__error(EINVAL, "Invalid thread",
+           thread->pt_magic == PT_MAGIC);
  
        if (pthread__find(thread) != 0)
                return ESRCH;
@@ -702,14 +708,14 @@
 {
        pthread_t self;
 
+       pthread__error(EINVAL, "Invalid thread",
+           thread->pt_magic == PT_MAGIC);
+
        self = pthread__self();
 
        if (pthread__find(thread) != 0)
                return ESRCH;
 
-       if (thread->pt_magic != PT_MAGIC)
-               return EINVAL;
-
        if (thread == self)
                return EDEADLK;
 
@@ -764,9 +770,16 @@
 int
 pthread_equal(pthread_t t1, pthread_t t2)
 {
+
        if (__predict_false(__uselibcstub))
                return __libc_thr_equal_stub(t1, t2);
 
+       pthread__error(EINVAL, "Invalid thread",
+           t1->pt_magic == PT_MAGIC);
+
+       pthread__error(EINVAL, "Invalid thread",
+           t2->pt_magic == PT_MAGIC);
+
        /* Nothing special here. */
        return (t1 == t2);
 }
@@ -777,12 +790,12 @@
 {
        int error;
 
+       pthread__error(EINVAL, "Invalid thread",
+           thread->pt_magic == PT_MAGIC);
+
        if (pthread__find(thread) != 0)
                return ESRCH;
 
-       if (thread->pt_magic != PT_MAGIC)
-               return EINVAL;
-
        pthread_mutex_lock(&thread->pt_lock);
        if ((thread->pt_flags & PT_FLAG_DETACHED) != 0) {
                error = EINVAL;
@@ -806,12 +819,12 @@
 pthread_getname_np(pthread_t thread, char *name, size_t len)
 {
 
+       pthread__error(EINVAL, "Invalid thread",
+           thread->pt_magic == PT_MAGIC);
+
        if (pthread__find(thread) != 0)
                return ESRCH;
 
-       if (thread->pt_magic != PT_MAGIC)
-               return EINVAL;
-
        pthread_mutex_lock(&thread->pt_lock);
        if (thread->pt_name == NULL)
                name[0] = '\0';
@@ -829,12 +842,12 @@
        char *oldname, *cp, newname[PTHREAD_MAX_NAMELEN_NP];
        int namelen;
 
+       pthread__error(EINVAL, "Invalid thread",
+           thread->pt_magic == PT_MAGIC);
+
        if (pthread__find(thread) != 0)
                return ESRCH;
 
-       if (thread->pt_magic != PT_MAGIC)
-               return EINVAL;
-
        namelen = snprintf(newname, sizeof(newname), name, arg);
        if (namelen >= PTHREAD_MAX_NAMELEN_NP)
                return EINVAL;
@@ -870,6 +883,9 @@
 pthread_cancel(pthread_t thread)
 {
 
+       pthread__error(EINVAL, "Invalid thread",
+           thread->pt_magic == PT_MAGIC);
+
        if (pthread__find(thread) != 0)
                return ESRCH;
        pthread_mutex_lock(&thread->pt_lock);
diff -r 83748c726ae1 -r 51a32902275c lib/libpthread/pthread_getcpuclockid.c
--- a/lib/libpthread/pthread_getcpuclockid.c    Wed Jan 29 15:47:51 2020 +0000
+++ b/lib/libpthread/pthread_getcpuclockid.c    Wed Jan 29 16:03:44 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: pthread_getcpuclockid.c,v 1.2 2017/03/04 11:16:33 njoly Exp $  */
+/*     $NetBSD: pthread_getcpuclockid.c,v 1.3 2020/01/29 16:03:44 kamil Exp $  */
 
 /*-
  * Copyright (c) 2016 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 #include <sys/cdefs.h>
 #if defined(LIBC_SCCS) && !defined(lint)
-__RCSID("$NetBSD: pthread_getcpuclockid.c,v 1.2 2017/03/04 11:16:33 njoly Exp $");
+__RCSID("$NetBSD: pthread_getcpuclockid.c,v 1.3 2020/01/29 16:03:44 kamil Exp $");
 #endif /* LIBC_SCCS and not lint */
 
 #include <sys/types.h>
@@ -45,6 +45,9 @@
 {
        int error = 0, saved_errno;
 
+       pthread__error(EINVAL, "Invalid thread",
+           thread->pt_magic == PT_MAGIC);
+
        saved_errno = errno;
        if (clock_getcpuclockid2(P_LWPID, (id_t)thread->pt_lid, clock_id) == -1)
                error = errno;



Home | Main Index | Thread Index | Old Index