[libvirt] [PATCH]: Fix VIR_ALLOC_N for 0 byte arrays

Jim Meyering jim at meyering.net
Thu Jun 19 09:18:57 UTC 2008


Chris Lalancette <clalance at redhat.com> wrote:
>      For 0.4.3, danpb's new memory management scheme went into libvirt.  This is
> fine, except that is subtly alters the semantics of malloc(), calloc(), and
> realloc().  In particular, if you say:
>
> foo = malloc(0);
>
> glibc will happily return a non-NULL pointer to you.  However, with the new
> memory management stuff, if you say:
>
> foo = VIR_ALLOC(0);
>
> you will actually get a NULL pointer back.  Personally, I think this is a
> dangerous deviation from malloc() semantics that everyone is used to, and is
> indeed causing problems with the remote driver.  The short of it is that the
> remote driver allocates memory on behalf of the remote side using VIR_ALLOC_N,
> and this call is returning NULL so that the NULL checks elsewhere in the code
> fire and return failure.
>
> The attached patch fixes this situation by removing the 0 checks from the memory
> allocation paths, and just lets them fall through to the normal malloc(),
> calloc(), or realloc() routines, restoring old semantics.

Wow.  That's a nasty one, violating such assumptions.
Good catch.

In case anyone reading along wonders about the portability of libvirt's
assumption that those functions return non-NULL for N=0, it's fine
because gnulib wrappers protect us from libc functions with the
undesirable semantics.

ACK.




More information about the libvir-list mailing list