From owner-freebsd-hackers@FreeBSD.ORG Thu Jun 29 19:27:29 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 D413616A416 for ; Thu, 29 Jun 2006 19:27:29 +0000 (UTC) (envelope-from erikt@midgard.homeip.net) Received: from pne-smtpout2-sn1.fre.skanova.net (pne-smtpout2-sn1.fre.skanova.net [81.228.11.159]) by mx1.FreeBSD.org (Postfix) with ESMTP id 3EF67447A8 for ; Thu, 29 Jun 2006 19:27:20 +0000 (GMT) (envelope-from erikt@midgard.homeip.net) Received: from falcon.midgard.homeip.net (83.253.29.241) by pne-smtpout2-sn1.fre.skanova.net (7.2.075) id 44A135F1000A482C for freebsd-hackers@freebsd.org; Thu, 29 Jun 2006 21:27:19 +0200 Received: (qmail 44403 invoked from network); 29 Jun 2006 21:27:18 +0200 Received: from owl.midgard.homeip.net (10.1.5.7) by falcon.midgard.homeip.net with SMTP; 29 Jun 2006 21:27:18 +0200 Received: (qmail 65723 invoked by uid 1001); 29 Jun 2006 21:27:18 +0200 Date: Thu, 29 Jun 2006 21:27:18 +0200 From: Erik Trulsson To: Pat Lashley Message-ID: <20060629192718.GA65675@owl.midgard.homeip.net> Mail-Followup-To: Pat Lashley , Stefan Farfeleder , Andre Albsmeier , freebsd-hackers@freebsd.org References: <20060628181045.GA54915@curry.mchp.siemens.de> <20060628212956.GI822@wombat.fafoe.narf.at> <805AA34B676EDF411B3CF548@Zelazny> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <805AA34B676EDF411B3CF548@Zelazny> User-Agent: Mutt/1.5.11 Cc: Stefan Farfeleder , freebsd-hackers@freebsd.org, Andre Albsmeier 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 19:27:29 -0000 On Thu, Jun 29, 2006 at 11:44:23AM -0400, Pat Lashley wrote: > >The C Standard says the following about malloc(0): > > > > 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. > > > >So our default behaviour to crash if a pointer returned by malloc(0) is > >dereferenced is legal and a good one because it catches errors like the > >above one. > > 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. > > first = malloc(0) ; > second = malloc(0) ; > > if ( first == second ) ERROR( "C standards violation" ) ; Almost. The test should be if ( first != NULL && first == second) ERROR( "C standards violation" ) ; It is after all legal for malloc(0) to return NULL. Otherwise you are correct. Having malloc(0) always returning the same (non-NULL) value is not legal according to the C standard. C99 says: 7.20.3 Memory management functions [...] Each such allocation shall yield a pointer to an object disjoint from any other object. [...] 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. > > > Firefox, or the extension, has a bug in the code. It should not be > attempting to de-reference the result of a 'malloc(0)' call. They probably > depend on having it return NULL, which is checked elsewhere. (The fix is > for them to test for the size == zero case and just set the pointer to NULL > instead of calling malloc(0). But that's their problem, not ours.) > > > > -Pat > _______________________________________________ > freebsd-hackers@freebsd.org mailing list > http://lists.freebsd.org/mailman/listinfo/freebsd-hackers > To unsubscribe, send any mail to "freebsd-hackers-unsubscribe@freebsd.org" -- Erik Trulsson ertr1013@student.uu.se