Date: Sun, 2 Jul 2006 16:34:23 +0200 From: Andre Albsmeier <Andre.Albsmeier@siemens.com> To: Matthias Andree <matthias.andree@gmx.de> Cc: freebsd-hackers@freebsd.org, Pat Lashley <patl+freebsd@volant.org>, Johannes Weiner <hnazfoo@googlemail.com>, Andre.Albsmeier@siemens.com Subject: Re: Return value of malloc(0) Message-ID: <20060702143423.GA1108@curry.mchp.siemens.de> In-Reply-To: <m3hd21g1ss.fsf@merlin.emma.line.org> References: <20060628181045.GA54915@curry.mchp.siemens.de> <20060629054222.GA92895@leiferikson.flosken.lan> <m3bqsceyf2.fsf@merlin.emma.line.org> <20060629162319.GA94921@leiferikson.flosken.lan> <m33bdnhnv7.fsf@merlin.emma.line.org> <20060630045937.GB97868@leiferikson.flosken.lan> <417C9B11412FF8C17A1AD483@Zelazny> <m3hd21g1ss.fsf@merlin.emma.line.org>
next in thread | previous in thread | raw e-mail | index | archive | help
On Sat, 01-Jul-2006 at 10:35:47 +0200, Matthias Andree wrote: > Pat Lashley <patl+freebsd@volant.org> writes: > > > BUT, that said, the safest and most portable coding practice would be: > > > > // The C standard does not require malloc(0) to return NULL; > > // but whatever it returns MUST NOT be dereferenced. > > ptr = ( size == 0 ) ? NULL : malloc( size ) ; > > Safest (avoiding null derefence) would instead be: > > ptr = malloc(size ? size : 1); I hacked malloc.c to do exactly this automatically, just for testing. 15 Minutes after rebooting (and after doing a lot of desktop switching and opening and closing of windows) the X-server ran out of memory :-). I assume there are lots of programs out there which do malloc(0) but only firefox (and apparently seamonkey) dereference the returned non-NULL pointer and crash therefore. -Andre
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20060702143423.GA1108>