From owner-freebsd-stable@FreeBSD.ORG Mon Dec 11 19:08:54 2006 Return-Path: X-Original-To: stable@freebsd.org Delivered-To: freebsd-stable@FreeBSD.ORG Received: from mx1.FreeBSD.org (mx1.freebsd.org [69.147.83.52]) by hub.freebsd.org (Postfix) with ESMTP id 3561216A47B for ; Mon, 11 Dec 2006 19:08:54 +0000 (UTC) (envelope-from rizzo@icir.org) Received: from xorpc.icir.org (xorpc.icir.org [192.150.187.68]) by mx1.FreeBSD.org (Postfix) with ESMTP id 857654402D for ; Mon, 11 Dec 2006 19:02:24 +0000 (GMT) (envelope-from rizzo@icir.org) Received: from xorpc.icir.org (localhost [127.0.0.1]) by xorpc.icir.org (8.12.11/8.13.6) with ESMTP id kBBJ35b1048268; Mon, 11 Dec 2006 11:03:06 -0800 (PST) (envelope-from rizzo@xorpc.icir.org) Received: (from rizzo@localhost) by xorpc.icir.org (8.12.11/8.12.3/Submit) id kBBJ35ui048267; Mon, 11 Dec 2006 11:03:05 -0800 (PST) (envelope-from rizzo) Date: Mon, 11 Dec 2006 11:03:05 -0800 From: Luigi Rizzo To: Dan Nelson Message-ID: <20061211110305.B47977@xorpc.icir.org> References: <20061211094423.B44819@xorpc.icir.org> <20061211182544.GG69299@dan.emsphone.com> <20061211185411.GH69299@dan.emsphone.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.2.5.1i In-Reply-To: <20061211185411.GH69299@dan.emsphone.com>; from dnelson@allantgroup.com on Mon, Dec 11, 2006 at 12:54:11PM -0600 Cc: stable@freebsd.org Subject: Re: malloc(0) returns 0x800 on FreeBSD 6.2 ? X-BeenThere: freebsd-stable@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Production branch of FreeBSD source code List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 11 Dec 2006 19:08:54 -0000 On Mon, Dec 11, 2006 at 12:54:11PM -0600, Dan Nelson wrote: > In the last episode (Dec 11), Dan Nelson said: > > In the last episode (Dec 11), Luigi Rizzo said: > > > i was debugging a program on FreeBSD 6, and much to my surprise, i > > > noticed that malloc(0) returns 0x800, as shown by this program: > > > > > > > more a.c > > > #include > > > int main(int argc, char *argv[]) > > > { > > > char *p = malloc(0); > > > printf(" malloc 0 returns %p\n", p); > > > } > > > > cc -o a a.c > > > > ./a > > > malloc 0 returns 0x800 > > > > > > if you look at the source this is indeed clear - internally the 0x800 > > > is ZEROSIZEPTR and is set when a zero length is passed to malloc() > > > unless you have malloc_sysv set. > > > > Right, it passed you a pointer to which you may write 0 bytes to; > > exactly what the program asked for :) > > > > The FreeBSD 6.x behaviour is slightly against POSIX rules that state > > all successful malloc calls must return unique pointers, so the 7.x > > malloc silently rounds zero-size mallocs to 1. Ideally malloc would > > return unique pointers to blocks of memory set to MPROT_NONE via > > mprotect() (you could fit 8192 of these pointers in an 8k page), to > > prevent applications from using that byte of memory. > > Also note that the 0x800 behaviour was added to malloc.c rev 1.60 back > in 2001, which means that all of the 5.x and 6.x releases did this. yep, just found out various threads on the mailing lists, but i first looked at the manpage and was surprised of not seeing it documented. I haven't figured out what the conclusion of the discussion was. I am glad that 7.x changes the behaviour back to what it was on 4.x, cheers luigi