]> xenbits.xensource.com Git - libvirt.git/commitdiff
For 0.4.3, danpb's new memory management scheme went into libvirt. This is
authorChris Lalancette <clalance@redhat.com>
Thu, 19 Jun 2008 11:58:49 +0000 (11:58 +0000)
committerChris Lalancette <clalance@redhat.com>
Thu, 19 Jun 2008 11:58:49 +0000 (11:58 +0000)
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.

Signed-off-by: Chris Lalancette <clalance@redhat.com>
src/memory.c

index d35ada3b71ff9b49aa7b341bda4deeef2ba1c398..93f403fbfe7cda016d37eff4d971c8b39fb51230 100644 (file)
@@ -97,11 +97,6 @@ int __virAlloc(void *ptrptr, size_t size)
     }
 #endif
 
-    if (size == 0) {
-        *(void **)ptrptr = NULL;
-        return 0;
-    }
-
     *(void **)ptrptr = calloc(1, size);
     if (*(void **)ptrptr == NULL)
         return -1;
@@ -130,11 +125,6 @@ int __virAllocN(void *ptrptr, size_t size, size_t count)
     }
 #endif
 
-    if (size == 0 || count == 0) {
-        *(void **)ptrptr = NULL;
-        return 0;
-    }
-
     *(void**)ptrptr = calloc(count, size);
     if (*(void**)ptrptr == NULL)
         return -1;
@@ -163,11 +153,6 @@ int __virReallocN(void *ptrptr, size_t size, size_t count)
         return -1;
 #endif
 
-    if (size == 0 || count == 0) {
-        free(*(void **)ptrptr);
-        *(void **)ptrptr = NULL;
-        return 0;
-    }
     if (xalloc_oversized(count, size)) {
         errno = ENOMEM;
         return -1;