From owner-freebsd-current@FreeBSD.ORG Wed Dec 1 05:40:20 2004 Return-Path: Delivered-To: freebsd-current@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id BF2D916A4CE for ; Wed, 1 Dec 2004 05:40:20 +0000 (GMT) Received: from mail1.webmaster.com (mail1.webmaster.com [216.152.64.168]) by mx1.FreeBSD.org (Postfix) with ESMTP id 825DA43D54 for ; Wed, 1 Dec 2004 05:40:20 +0000 (GMT) (envelope-from davids@webmaster.com) Received: from however by webmaster.com (MDaemon.PRO.v7.1.0.R) with ESMTP id md50000304820.msg for ; Tue, 30 Nov 2004 21:16:33 -0800 From: "David Schwartz" To: Date: Tue, 30 Nov 2004 21:40:25 -0800 Message-ID: MIME-Version: 1.0 Content-Type: text/plain; charset="US-ASCII" Content-Transfer-Encoding: 7bit X-Priority: 3 (Normal) X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook IMO, Build 9.0.6604 (9.0.2911.0) Importance: Normal In-Reply-To: X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2180 X-Authenticated-Sender: joelkatz@webmaster.com X-Spam-Processed: mail1.webmaster.com, Tue, 30 Nov 2004 21:16:33 -0800 (not processed: message from trusted or authenticated source) X-MDRemoteIP: 206.171.168.138 X-Return-Path: davids@webmaster.com X-MDaemon-Deliver-To: current@FreeBSD.org X-MDAV-Processed: mail1.webmaster.com, Tue, 30 Nov 2004 21:16:36 -0800 Subject: RE: malloc(0) returns an invalid address X-BeenThere: freebsd-current@freebsd.org X-Mailman-Version: 2.1.1 Precedence: list Reply-To: davids@webmaster.com List-Id: Discussions about the use of FreeBSD-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 01 Dec 2004 05:40:20 -0000 > In fact, if you compile the following code, > > #include > #include > > main() > { > char *p = malloc(0); > > printf("address of p is %p\n", p); > *p = 0; /* XXX */ > } > > the result of the execution would be as follows: > > % ./a.out > address of p is 0x800 > zsh: 794 segmentation fault (core dumped) ./a.out > > Is this a malloc bug? Or is this the intended behavior and the man > page description is old? This is the intended behavior but the man page description is correct. The problem is not that the pointer is invalid but that you assumed that it was large enough to hold a 'char' and it is not. DS