Hi,

attached there is a patch to do validity checks for the pthread key 
passed to pthread_getspecific and pthread_setspecific (more details in 
the commit log), with two very simple tests for the new checks.

Thanks,
-- 
Pino Toscano
From e64c1c3f36168593eccb577218971d4fa0448ee0 Mon Sep 17 00:00:00 2001
From: Pino Toscano <toscano.p...@tiscali.it>
Date: Sun, 6 Nov 2011 12:39:04 +0100
Subject: [PATCH] pthread_getspecific, pthread_setspecific: check the key
 validity

When getting a TSD, handle gracefully the case of an invalid key.

When setting a TSD, check for the validity of the key as recommended
(although not required) by POSIX. This also avoids potentially
filling the `thread_specifics' hash of threads with TSD of invalid
keys.

Add two simple checks in test-7.c for the two situations above.

* sysdeps/hurd/pt-getspecific.c (pthread_getspecific): Check the
validity of the specified key.
* sysdeps/hurd/pt-setspecific.c (pthread_setspecific): Likewise.
* tests/test-7.c (main): Add two assertions.
---
 sysdeps/hurd/pt-getspecific.c |    4 +++-
 sysdeps/hurd/pt-setspecific.c |    4 ++++
 tests/test-7.c                |    3 +++
 3 files changed, 10 insertions(+), 1 deletions(-)

diff --git a/sysdeps/hurd/pt-getspecific.c b/sysdeps/hurd/pt-getspecific.c
index 3060598..71ec63c 100644
--- a/sysdeps/hurd/pt-getspecific.c
+++ b/sysdeps/hurd/pt-getspecific.c
@@ -27,7 +27,9 @@ pthread_getspecific (pthread_key_t key)
 {
   struct __pthread *self;
 
-  assert (key < __pthread_key_count);
+  if (key < 0 || key >= __pthread_key_count
+      || __pthread_key_destructors[key] == PTHREAD_KEY_INVALID)
+    return NULL;
 
   self = _pthread_self ();
   if (! self->thread_specifics)
diff --git a/sysdeps/hurd/pt-setspecific.c b/sysdeps/hurd/pt-setspecific.c
index 89ca4d7..d0b7302 100644
--- a/sysdeps/hurd/pt-setspecific.c
+++ b/sysdeps/hurd/pt-setspecific.c
@@ -28,6 +28,10 @@ pthread_setspecific (pthread_key_t key, const void *value)
   error_t err;
   struct __pthread *self = _pthread_self ();
 
+  if (key < 0 || key >= __pthread_key_count
+      || __pthread_key_destructors[key] == PTHREAD_KEY_INVALID)
+    return EINVAL;
+
   if (! self->thread_specifics)
     {
       err = hurd_ihash_create (&self->thread_specifics, HURD_IHASH_NO_LOCP);
diff --git a/tests/test-7.c b/tests/test-7.c
index 8159be3..22fb1ca 100644
--- a/tests/test-7.c
+++ b/tests/test-7.c
@@ -42,6 +42,9 @@ main (int argc, char **argv)
       assert ((pthread_t) val == pthread_self ());
     }
 
+  assert (pthread_getspecific ((pthread_key_t) 0) == NULL);
+  assert (pthread_setspecific ((pthread_key_t) 0, (void *) 0x1) == EINVAL);
+
   for (i = 0; i < KEYS; i ++)
     err = pthread_key_create (&key[i], des);
 
-- 
1.7.7.1

Attachment: signature.asc
Description: This is a digitally signed message part.

Reply via email to