From owner-freebsd-threads@FreeBSD.ORG Thu Jan 31 20:19:21 2008 Return-Path: Delivered-To: threads@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 5E79216A417 for ; Thu, 31 Jan 2008 20:19:21 +0000 (UTC) (envelope-from jin@george.lbl.gov) Received: from smtp119.sbc.mail.sp1.yahoo.com (smtp119.sbc.mail.sp1.yahoo.com [69.147.64.92]) by mx1.freebsd.org (Postfix) with SMTP id 3FD3213C44B for ; Thu, 31 Jan 2008 20:19:21 +0000 (UTC) (envelope-from jin@george.lbl.gov) Received: (qmail 77155 invoked from network); 31 Jan 2008 19:52:41 -0000 Received: from unknown (HELO ?192.168.1.238?) (jinmtb@sbcglobal.net@67.111.218.125 with plain) by smtp119.sbc.mail.sp1.yahoo.com with SMTP; 31 Jan 2008 19:52:41 -0000 X-YMail-OSG: iRYsG4kVM1nawf7BByiMThIZ0dkkt7TWKzGwz7XKdx2eiRPh.lwUwEPd07rcL.QA6DgOGKqmyg-- X-Yahoo-Newman-Property: ymail-3 Message-ID: <47A22703.7030903@george.lbl.gov> Date: Thu, 31 Jan 2008 11:52:35 -0800 From: Jin Guojun User-Agent: Mozilla/5.0 (X11; U; FreeBSD i386; en-US; rv:1.7.13) Gecko/20061027 X-Accept-Language: zh, zh-CN, en MIME-Version: 1.0 To: threads@freebsd.org Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Cc: Subject: improper order to check NULL pointer in libthr X-BeenThere: freebsd-threads@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Threading on FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 31 Jan 2008 20:19:21 -0000 Most pthread_mutex functions and some pthread_ functions in linbthr do not check NULL pointer at the function entry, but they dereference the pointer to see if its content is NULL. The NULL pointer is ASSERTed at last stage (in xxxxx_common routines) somehow, which is meaningless. So, what is the correct design pattern? -- (should be like libpthread, which looks correct) Return EFAULT / EINVAL at the entry point as +++ below if the pointer is NULL? or ASSERT at the entry point if pointer or its content is NULL? Please CC me since I am not on thread list. Thanks, -Jin int _pthread_mutex_trylock(pthread_mutex_t *mutex) { + if (!mutex) + return EFAULT; + struct pthread *curthread = _get_curthread(); int ret = 0; /* * If the mutex is statically initialized, perform the dynamic * initialization marking the mutex private (delete safe): */ if ((*mutex != NULL) || ((ret = init_static_private(curthread, mutex)) == 0)) ret = mutex_trylock_common(curthread, mutex); return (ret); } static int mutex_lock_common(struct pthread *curthread, pthread_mutex_t *m, const struct timespec * abstime) { struct timespec ts, ts2; long cycle; int ret = 0; THR_ASSERT((m != NULL) && (*m != NULL), "Uninitialized mutex in mutex_lock_common");