Module Name: src Committed By: kamil Date: Wed Jan 29 16:03:44 UTC 2020
Modified Files: src/lib/libpthread: pthread.c pthread_getcpuclockid.c Log Message: Chack thread->pt_magic with PT_MAGIC promptly Rearrange some checks to avoid verifying pthread_t after using it. To generate a diff of this commit: cvs rdiff -u -r1.160 -r1.161 src/lib/libpthread/pthread.c cvs rdiff -u -r1.2 -r1.3 src/lib/libpthread/pthread_getcpuclockid.c Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files.
Modified files: Index: src/lib/libpthread/pthread.c diff -u src/lib/libpthread/pthread.c:1.160 src/lib/libpthread/pthread.c:1.161 --- src/lib/libpthread/pthread.c:1.160 Wed Jan 29 15:31:14 2020 +++ src/lib/libpthread/pthread.c Wed Jan 29 16:03:44 2020 @@ -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_suspend_np(pthread_t thread) { 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 @@ pthread_suspend_np(pthread_t thread) 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_join(pthread_t thread, void **va { 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 @@ pthread__reap(pthread_t thread) 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 @@ pthread_detach(pthread_t thread) { 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 @@ int 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 @@ pthread_setname_np(pthread_t thread, con 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 @@ int 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); Index: src/lib/libpthread/pthread_getcpuclockid.c diff -u src/lib/libpthread/pthread_getcpuclockid.c:1.2 src/lib/libpthread/pthread_getcpuclockid.c:1.3 --- src/lib/libpthread/pthread_getcpuclockid.c:1.2 Sat Mar 4 11:16:33 2017 +++ src/lib/libpthread/pthread_getcpuclockid.c Wed Jan 29 16:03:44 2020 @@ -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 @@ pthread_getcpuclockid(pthread_t thread, { 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;