From owner-freebsd-threads@FreeBSD.ORG Tue May 14 09:52:16 2013 Return-Path: Delivered-To: threads@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by hub.freebsd.org (Postfix) with ESMTP id D8B40CC9 for ; Tue, 14 May 2013 09:52:16 +0000 (UTC) (envelope-from phk@freebsd.org) Received: from phk.freebsd.dk (phk.freebsd.dk [130.225.244.222]) by mx1.freebsd.org (Postfix) with ESMTP id A52B3E84 for ; Tue, 14 May 2013 09:52:16 +0000 (UTC) Received: from critter.freebsd.dk (critter.freebsd.dk [192.168.61.3]) by phk.freebsd.dk (Postfix) with ESMTP id 36E1B89FC9 for ; Tue, 14 May 2013 09:52:10 +0000 (UTC) Received: from critter.freebsd.dk (localhost [127.0.0.1]) by critter.freebsd.dk (8.14.6/8.14.6) with ESMTP id r4E9q94x079160 for ; Tue, 14 May 2013 09:52:10 GMT (envelope-from phk@freebsd.org) To: threads@freebsd.org Subject: pthread_key_create() should avoid key#0 From: Poul-Henning Kamp Content-Type: text/plain; charset=ISO-8859-1 Date: Tue, 14 May 2013 09:52:09 +0000 Message-ID: <79159.1368525129@critter.freebsd.dk> X-BeenThere: freebsd-threads@freebsd.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: Threading on FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 14 May 2013 09:52:16 -0000 I think this patch would be a good idea, it avoids allocing a thread specific key with numeric value zero, which helps expose cases where a key hasn't been allocated in the first place. Index: libkse/thread/thr_spec.c =================================================================== --- libkse/thread/thr_spec.c (revision 248293) +++ libkse/thread/thr_spec.c (working copy) @@ -59,7 +59,7 @@ /* Lock the key table: */ THR_LOCK_ACQUIRE(curthread, &_keytable_lock); - for (i = 0; i < PTHREAD_KEYS_MAX; i++) { + for (i = 1; i < PTHREAD_KEYS_MAX; i++) { if (_thread_keytable[i].allocated == 0) { _thread_keytable[i].allocated = 1; @@ -84,7 +84,7 @@ struct pthread *curthread = _get_curthread(); int ret = 0; - if ((unsigned int)key < PTHREAD_KEYS_MAX) { + if (key > 0 && (unsigned int)key < PTHREAD_KEYS_MAX) { /* Lock the key table: */ THR_LOCK_ACQUIRE(curthread, &_keytable_lock); Index: libthr/thread/thr_spec.c =================================================================== --- libthr/thread/thr_spec.c (revision 248293) +++ libthr/thread/thr_spec.c (working copy) @@ -61,7 +61,7 @@ /* Lock the key table: */ THR_LOCK_ACQUIRE(curthread, &_keytable_lock); - for (i = 0; i < PTHREAD_KEYS_MAX; i++) { + for (i = 1; i < PTHREAD_KEYS_MAX; i++) { if (_thread_keytable[i].allocated == 0) { _thread_keytable[i].allocated = 1; @@ -86,7 +86,7 @@ struct pthread *curthread = _get_curthread(); int ret = 0; - if ((unsigned int)key < PTHREAD_KEYS_MAX) { + if (key > 0 && (unsigned int)key < PTHREAD_KEYS_MAX) { /* Lock the key table: */ THR_LOCK_ACQUIRE(curthread, &_keytable_lock); -- Poul-Henning Kamp | UNIX since Zilog Zeus 3.20 phk@FreeBSD.ORG | TCP/IP since RFC 956 FreeBSD committer | BSD since 4.3-tahoe Never attribute to malice what can adequately be explained by incompetence.