Hi,
malloc(0) on AIX 4.3 and 5.1 returns NULL, which crashes some apps. We
fixed it in our applications. As we migrate from ctree to sqlite2 this
needs to be fixed or increased from malloc(0) to 1.
On AIX it will definitely return NULL and will crash as soon the pointer
is referenced.
Is the malloc(0) pointer used?
Helmut Tschemernjak
Lauri Nurmi wrote:
D. Richard Hipp wrote:
On Wed, 2005-06-29 at 15:19 +0300, Lauri Nurmi wrote:
So, basically, is there a reason why sqlite3Malloc(n) & co should even
try to allocate zero bytes, instead of just returning NULL if n==0? Does
a pointer pointing to zero bytes of memory have any use anyway?
Yes. A NULL return from sqlite3Malloc() is an error condition meaning
that you have run out of memory. That is very different from returning
a zero-length memory allocation.
I see the difference, yes. Still, I'm not convinced that doing a
malloc(0) is a good thing.
I found this quotation from the net
<http://lists.freebsd.org/pipermail/freebsd-net/2004-February/002946.html>,
it is probably from the C99 standard:
---
If the size of the space requested is zero, the behavior is
implementation-defined: either a null pointer is returned,
or the behavior is as if the size were some nonzero value,
except that the returned pointer shall not be used to
access an object.
---
So malloc(0) may return NULL, and even if it doesn't, the pointer should
not be used.
You wouldn't want to substitute a NULL for an empty string in SQL would
you? Having sqlite3Malloc return NULL for a zero-length allocation is
roughly the same thing.
Hmm, an empty (C-style) string doesn't fit into zero bytes, it needs one
byte for the '\0'. If you have a pointer pointing to zero allocated
bytes, you can't even use strlen() or anything else on it [according to
the quotation above].