From owner-freebsd-bugs@FreeBSD.ORG Thu Jul 14 14:10:10 2005 Return-Path: X-Original-To: freebsd-bugs@hub.freebsd.org Delivered-To: freebsd-bugs@hub.freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 6E93416A41C for ; Thu, 14 Jul 2005 14:10:10 +0000 (GMT) (envelope-from gnats@FreeBSD.org) Received: from freefall.freebsd.org (freefall.freebsd.org [216.136.204.21]) by mx1.FreeBSD.org (Postfix) with ESMTP id DAE1C43D45 for ; Thu, 14 Jul 2005 14:10:09 +0000 (GMT) (envelope-from gnats@FreeBSD.org) Received: from freefall.freebsd.org (gnats@localhost [127.0.0.1]) by freefall.freebsd.org (8.13.3/8.13.3) with ESMTP id j6EEA9WW014498 for ; Thu, 14 Jul 2005 14:10:09 GMT (envelope-from gnats@freefall.freebsd.org) Received: (from gnats@localhost) by freefall.freebsd.org (8.13.3/8.13.1/Submit) id j6EEA9i7014495; Thu, 14 Jul 2005 14:10:09 GMT (envelope-from gnats) Resent-Date: Thu, 14 Jul 2005 14:10:09 GMT Resent-Message-Id: <200507141410.j6EEA9i7014495@freefall.freebsd.org> Resent-From: FreeBSD-gnats-submit@FreeBSD.org (GNATS Filer) Resent-To: freebsd-bugs@FreeBSD.org Resent-Reply-To: FreeBSD-gnats-submit@FreeBSD.org, Dan Lukes Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 86D8516A41C for ; Thu, 14 Jul 2005 14:09:49 +0000 (GMT) (envelope-from dan@kulesh.obluda.cz) Received: from kulesh.obluda.cz (kulesh.obluda.cz [193.179.22.243]) by mx1.FreeBSD.org (Postfix) with ESMTP id 331AF43D45 for ; Thu, 14 Jul 2005 14:09:47 +0000 (GMT) (envelope-from dan@kulesh.obluda.cz) Received: from kulesh.obluda.cz (localhost.eunet.cz [127.0.0.1]) by kulesh.obluda.cz (8.13.3/8.13.3) with ESMTP id j6EE9jA5033305 for ; Thu, 14 Jul 2005 16:09:46 +0200 (CEST) (envelope-from dan@kulesh.obluda.cz) Received: (from root@localhost) by kulesh.obluda.cz (8.13.3/8.13.1/Submit) id j6EE9jSC033304; Thu, 14 Jul 2005 16:09:45 +0200 (CEST) (envelope-from dan) Message-Id: <200507141409.j6EE9jSC033304@kulesh.obluda.cz> Date: Thu, 14 Jul 2005 16:09:45 +0200 (CEST) From: Dan Lukes To: FreeBSD-gnats-submit@FreeBSD.org X-Send-Pr-Version: 3.113 Cc: Subject: bin/83457: [ PATCH ] Unhandled malloc failure within libpthread's _thr_alloc() X-BeenThere: freebsd-bugs@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list Reply-To: Dan Lukes List-Id: Bug reports List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 14 Jul 2005 14:10:10 -0000 >Number: 83457 >Category: bin >Synopsis: [ PATCH ] Unhandled malloc failure within libpthread's _thr_alloc() >Confidential: no >Severity: serious >Priority: low >Responsible: freebsd-bugs >State: open >Quarter: >Keywords: >Date-Required: >Class: sw-bug >Submitter-Id: current-users >Arrival-Date: Thu Jul 14 14:10:09 GMT 2005 >Closed-Date: >Last-Modified: >Originator: Dan Lukes >Release: FreeBSD 5.4-STABLE i386 >Organization: Obludarium >Environment: System: FreeBSD kulesh.obluda.cz 5.4-STABLE FreeBSD 5.4-STABLE #8: Sat Jul 9 16:31:08 CEST 2005 dan@kulesh.obluda.cz:/usr/obj/usr/src/sys/KULESH i386 lib/libpthread/thread/thr_kern.c,v 1.112.2.2 2005/02/04 16:17:55 jhb >Description: Unhandled malloc failure within libpthread's _thr_alloc() >How-To-Repeat: >Fix: The order of initialization of thread->tcb and thread->siginfo has been swapped. It simplify cleanup after siginfo alloc failure (tcb is not initialized yet, so it's not necesarry to destroy it) Over-nested if-else structures has been rearanged to improve readability of code. Use 'goto' for exception handlings is not violation of 'structured programming rule' --- patch begins here --- --- lib/libpthread/thread/thr_kern.c.ORIG Mon Feb 14 12:33:31 2005 +++ lib/libpthread/thread/thr_kern.c Thu Jul 14 16:03:24 2005 @@ -2363,37 +2363,43 @@ } } if ((thread == NULL) && - ((thread = malloc(sizeof(struct pthread))) != NULL)) { - bzero(thread, sizeof(struct pthread)); - if (curthread) { - _pthread_mutex_lock(&_tcb_mutex); - thread->tcb = _tcb_ctor(thread, 0 /* not initial tls */); - _pthread_mutex_unlock(&_tcb_mutex); - } else { - thread->tcb = _tcb_ctor(thread, 1 /* initial tls */); - } - if (thread->tcb == NULL) { - free(thread); - thread = NULL; - } else { - thread->siginfo = calloc(_SIG_MAXSIG, - sizeof(siginfo_t)); - /* - * Initialize thread locking. - * Lock initializing needs malloc, so don't - * enter critical region before doing this! - */ - if (_lock_init(&thread->lock, LCK_ADAPTIVE, - _thr_lock_wait, _thr_lock_wakeup) != 0) - PANIC("Cannot initialize thread lock"); - for (i = 0; i < MAX_THR_LOCKLEVEL; i++) { - _lockuser_init(&thread->lockusers[i], - (void *)thread); - _LCK_SET_PRIVATE2(&thread->lockusers[i], - (void *)thread); - } - } + ((thread = calloc(1, sizeof(struct pthread))) == NULL)) + goto done; + + thread->siginfo = calloc(_SIG_MAXSIG, sizeof(siginfo_t)); + if (thread->siginfo == NULL) { + free(thread); + thread = NULL; + goto done; + } + + if (curthread) { + _pthread_mutex_lock(&_tcb_mutex); + thread->tcb = _tcb_ctor(thread, 0 /* not initial tls */); + _pthread_mutex_unlock(&_tcb_mutex); + } else { + thread->tcb = _tcb_ctor(thread, 1 /* initial tls */); + } + if (thread->tcb == NULL) { + free(thread); + thread = NULL; + goto done; + } + + /* + * Initialize thread locking. + * Lock initializing needs malloc, so don't + * enter critical region before doing this! + */ + if (_lock_init(&thread->lock, LCK_ADAPTIVE, + _thr_lock_wait, _thr_lock_wakeup) != 0) + PANIC("Cannot initialize thread lock"); + for (i = 0; i < MAX_THR_LOCKLEVEL; i++) { + _lockuser_init(&thread->lockusers[i], (void *)thread); + _LCK_SET_PRIVATE2(&thread->lockusers[i], (void *)thread); } + +done: return (thread); } @@ -2180,7 +2180,7 @@ { struct kse *kse = NULL; char *stack; - kse_critical_t crit; + kse_critical_t crit = NULL; int i; if ((curthread != NULL) && (free_kse_count > 0)) { --- patch ends here --- >Release-Note: >Audit-Trail: >Unformatted: