diff options
author | Ted Unangst <tedu@cvs.openbsd.org> | 2011-07-05 00:42:47 +0000 |
---|---|---|
committer | Ted Unangst <tedu@cvs.openbsd.org> | 2011-07-05 00:42:47 +0000 |
commit | 87f98e28c34a2ae86af8f58d4809ac8976f2c703 (patch) | |
tree | 574dbe9a1edc8ccd8e3093b3e260f544bc40a193 | |
parent | da436b0eb24917121d4e345abcd90b6dfd568d5b (diff) |
check that thread specific keys are valid, and cleanup code a little.
ok guenther
-rw-r--r-- | lib/libpthread/uthread/uthread_spec.c | 35 |
1 files changed, 14 insertions, 21 deletions
diff --git a/lib/libpthread/uthread/uthread_spec.c b/lib/libpthread/uthread/uthread_spec.c index f01e91bbf0e..2e2e9c96303 100644 --- a/lib/libpthread/uthread/uthread_spec.c +++ b/lib/libpthread/uthread/uthread_spec.c @@ -1,4 +1,4 @@ -/* $OpenBSD: uthread_spec.c,v 1.11 2011/07/03 15:26:44 guenther Exp $ */ +/* $OpenBSD: uthread_spec.c,v 1.12 2011/07/05 00:42:46 tedu Exp $ */ /* * Copyright (c) 1995 John Birrell <jb@cimlogic.com.au>. * All rights reserved. @@ -80,7 +80,7 @@ pthread_key_delete(pthread_key_t key) int ret = 0; pthread_t pthread; - if (key < PTHREAD_KEYS_MAX) { + if (key >= 0 && key < PTHREAD_KEYS_MAX) { /* Lock the key table entry: */ _SPINLOCK(&key_table[key].lock); @@ -162,14 +162,14 @@ int pthread_setspecific(pthread_key_t key, const void *value) { struct pthread *pthread; - int ret = 0; + int ret = EINVAL; /* Point to the running thread: */ pthread = _get_curthread(); if ((pthread->specific_data) || (pthread->specific_data = pthread_key_allocate_data())) { - if (key < PTHREAD_KEYS_MAX) { + if (key >= 0 && key < PTHREAD_KEYS_MAX) { if (key_table[key].allocated) { if (pthread->specific_data[key] == NULL) { if (value != NULL) @@ -180,12 +180,11 @@ pthread_setspecific(pthread_key_t key, const void *value) } pthread->specific_data[key] = value; ret = 0; - } else - ret = EINVAL; - } else - ret = EINVAL; - } else + } + } + } else { ret = ENOMEM; + } return (ret); } @@ -193,27 +192,21 @@ void * pthread_getspecific(pthread_key_t key) { struct pthread *pthread; - void *data; + void *data = NULL; /* Point to the running thread: */ pthread = _get_curthread(); /* Check if there is specific data: */ - if (pthread->specific_data != NULL && key < PTHREAD_KEYS_MAX) { + if (pthread->specific_data != NULL && + key >= 0 && key < PTHREAD_KEYS_MAX) { /* Check if this key has been used before: */ if (key_table[key].allocated) { /* Return the value: */ - data = (void *) pthread->specific_data[key]; - } else { - /* - * This key has not been used before, so return NULL - * instead: - */ - data = NULL; + data = (void *)pthread->specific_data[key]; } - } else - /* No specific data has been created, so just return NULL: */ - data = NULL; + } + return (data); } #endif |