1.  check that they key is not less than 0.
2.  we can simplify several of the if/else chains.

Index: uthread/uthread_spec.c
===================================================================
RCS file: /cvs/src/lib/libpthread/uthread/uthread_spec.c,v
retrieving revision 1.10
diff -u -r1.10 uthread_spec.c
--- uthread/uthread_spec.c      2 Jul 2011 17:12:02 -0000       1.10
+++ uthread/uthread_spec.c      2 Jul 2011 17:17:50 -0000
@@ -80,7 +80,7 @@
        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 @@
 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,9 @@
                                }
                                pthread->specific_data[key] = value;
                                ret = 0;
-                       } else
-                               ret = EINVAL;
-               } else
-                       ret = EINVAL;
-       } else
-               ret = ENOMEM;
+                       }
+               }
+       }
        return (ret);
 }
 
@@ -193,27 +190,21 @@
 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

Reply via email to