From owner-freebsd-hackers@FreeBSD.ORG Thu Jun 29 20:57:07 2006 Return-Path: X-Original-To: freebsd-hackers@freeBSD.org Delivered-To: freebsd-hackers@freeBSD.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id CD6F916A407 for ; Thu, 29 Jun 2006 20:57:07 +0000 (UTC) (envelope-from lgusenet@be-well.ilk.org) Received: from mail2.sea5.speakeasy.net (mail2.sea5.speakeasy.net [69.17.117.4]) by mx1.FreeBSD.org (Postfix) with ESMTP id 6818643D5C for ; Thu, 29 Jun 2006 20:57:07 +0000 (GMT) (envelope-from lgusenet@be-well.ilk.org) Received: (qmail 20436 invoked from network); 29 Jun 2006 20:57:07 -0000 Received: from dsl092-078-145.bos1.dsl.speakeasy.net (HELO be-well.ilk.org) ([66.92.78.145]) (envelope-sender ) by mail2.sea5.speakeasy.net (qmail-ldap-1.03) with SMTP for ; 29 Jun 2006 20:57:06 -0000 Received: by be-well.ilk.org (Postfix, from userid 1147) id 8F12B28449; Thu, 29 Jun 2006 16:57:05 -0400 (EDT) To: Pat Lashley To: freebsd-hackers@freeBSD.org References: <20060628181045.GA54915@curry.mchp.siemens.de> <20060628212956.GI822@wombat.fafoe.narf.at> <805AA34B676EDF411B3CF548@Zelazny> <20060629165629.GA6875@britannica.bec.de> From: Lowell Gilbert Date: Thu, 29 Jun 2006 16:57:05 -0400 In-Reply-To: (Pat Lashley's message of "Thu, 29 Jun 2006 15:33:08 -0400") Message-ID: <44odwbu1cu.fsf@be-well.ilk.org> User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.0.50 (berkeley-unix) MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Mailman-Approved-At: Thu, 29 Jun 2006 21:33:46 +0000 Cc: Subject: Re: Return value of malloc(0) X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 29 Jun 2006 20:57:07 -0000 Pat Lashley writes: >> On Thu, Jun 29, 2006 at 11:44:23AM -0400, Pat Lashley wrote: >> > No, our implementation is NOT legal. We always return the SAME value. To >> > be legal, we should not return that value again unless it has been >> > free()-ed. >> >> It is legal due to brain damaged definition of implementation defined >> behaviour, but it violates the spirit of the standard :-) > > Perhaps I'm misunderstanding the 'implementation defined behavior' > choices in the standard. I thought that it could either 1) Return > NULL; or 2) Behave as though it returned a 'minimum allocation' (which > cannot be legally de-referenced). I went wandering through the C Working Group archives for the heck of it, and apparently a lot of people were confused over this, thinking either as you did or that "unique" meant it would a value unique to the usage of malloc(0). It's been clarified recently (and will be in the next revision of the standard) to the meaning you understood. Specifically: If the size of the space requested is zero, the behavior is implementation-defined: either a null pointer is returned, or the behavior is as if the size were some nonzero value, except that the returned pointer shall not be used to access an object. > But if it did actually perform a > minimum allocation'; wouldn't it have to return a different value > every time to maintain the free() semantics? I think that's another way of looking at the same confusion. If "minimum" is zero, then using a sentinel value (as in FreeBSD) works. Our malloc() could be easily fixed to be standards-compliant by removing the special handling for ZEROSIZEPTR in malloc.c; then allocations of 0 bytes will be rounded up to 16, just like all other alloations of less than 16 bytes. However, that would lose much of the bug-finding advantage of the current behaviour. This is wandering into -standards territory, though. In any case, the answer to thread's original question is "mozilla should fix its code to not assume malloc(0)==NULL".