Date: Mon, 11 Dec 2006 09:44:23 -0800 From: Luigi Rizzo <rizzo@icir.org> To: stable@freebsd.org Subject: malloc(0) returns 0x800 on FreeBSD 6.2 ? Message-ID: <20061211094423.B44819@xorpc.icir.org>
next in thread | raw e-mail | index | archive | help
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 <stdio.h>
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.
The thing is, i don't know if this behaviour is intentional or not,
but certainly is not documented -- the manpage documents
something totally different (in the section for the 'V'
MALLOC_OPTION, see below).
TUNING
...
V Attempting to allocate zero bytes will return a NULL pointer
instead of a valid pointer. (The default behavior is to make a
minimal allocation and return a pointer to it.) This option is
provided for System V compatibility. This option is incompatible
with the ``X'' option.
So what should we do with this ? Just fix the manpage or fix the
code ? This behaviour is likely to break quite a few things...
cheers
luigi
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20061211094423.B44819>
