From owner-freebsd-hackers@FreeBSD.ORG Sun Jul 2 14:34:37 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 133AE16A817 for ; Sun, 2 Jul 2006 14:34:37 +0000 (UTC) (envelope-from Andre.Albsmeier@siemens.com) Received: from david.siemens.de (david.siemens.de [192.35.17.14]) by mx1.FreeBSD.org (Postfix) with ESMTP id B775243D62 for ; Sun, 2 Jul 2006 14:34:27 +0000 (GMT) (envelope-from Andre.Albsmeier@siemens.com) Received: from mail2.siemens.de (localhost [127.0.0.1]) by david.siemens.de (8.12.6/8.12.6) with ESMTP id k62EYPvV022902; Sun, 2 Jul 2006 16:34:25 +0200 Received: from ims.mchp.siemens.de (ims.mchp.siemens.de [139.25.31.39]) by mail2.siemens.de (8.12.6/8.12.6) with ESMTP id k62EYP0c015141; Sun, 2 Jul 2006 16:34:25 +0200 Received: from mail-ct.mchp.siemens.de (mail-ct.mchp.siemens.de [139.25.31.51]) by ims.mchp.siemens.de with ESMTP id k62EYOJQ020611; Sun, 2 Jul 2006 16:34:24 +0200 (MEST) Received: from curry.mchp.siemens.de (curry [139.25.40.130]) by mail-ct.mchp.siemens.de (8.12.11/8.12.11) with ESMTP id k62EYO1L006351; Sun, 2 Jul 2006 16:34:24 +0200 (MEST) Received: (from localhost) by curry.mchp.siemens.de (8.13.6/8.13.6) id k62EYNJK086643; Date: Sun, 2 Jul 2006 16:34:23 +0200 From: Andre Albsmeier To: Matthias Andree Message-ID: <20060702143423.GA1108@curry.mchp.siemens.de> References: <20060628181045.GA54915@curry.mchp.siemens.de> <20060629054222.GA92895@leiferikson.flosken.lan> <20060629162319.GA94921@leiferikson.flosken.lan> <20060630045937.GB97868@leiferikson.flosken.lan> <417C9B11412FF8C17A1AD483@Zelazny> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Echelon: X-Advice: Drop that crappy M$-Outlook, I'm tired of your viruses! User-Agent: Mutt/1.5.11 Cc: freebsd-hackers@freebsd.org, Pat Lashley , Johannes Weiner , Andre.Albsmeier@siemens.com 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: Sun, 02 Jul 2006 14:34:37 -0000 On Sat, 01-Jul-2006 at 10:35:47 +0200, Matthias Andree wrote: > Pat Lashley 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