[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Xen-changelog] fixup memory leak and return value, if malloc or realloc fail.



# HG changeset patch
# User vhanquez@xxxxxxxxxxxxxxxxxxxxxxx
# Node ID 45f3b9cfbd83c68be431b7be4ed57376d1db7d51
# Parent  b89b94c71b2fd1e756b33189040d838af388713b
fixup memory leak and return value, if malloc or realloc fail.

Signed-off-by: Vincent Hanquez <vincent@xxxxxxxxxxxxx>

diff -r b89b94c71b2f -r 45f3b9cfbd83 tools/xenstore/utils.c
--- a/tools/xenstore/utils.c    Tue Jan 24 10:52:52 2006
+++ b/tools/xenstore/utils.c    Tue Jan 24 10:55:45 2006
@@ -96,21 +96,29 @@
                return NULL;
 
        buffer = malloc(max+1);
+       if (!buffer)
+               goto error;
        *size = 0;
        while ((ret = read(fd, buffer + *size, max - *size)) > 0) {
                *size += ret;
                if (*size == max) {
+                       void *nbuffer;
                        max *= 2;
-                       buffer = realloc(buffer, max + 1);
+                       nbuffer = realloc(buffer, max + 1);
+                       if (!nbuffer)
+                               goto error;
+                       buffer = nbuffer;
                }
        }
-       if (ret < 0) {
-               free(buffer);
-               buffer = NULL;
-       } else
-               ((char *)buffer)[*size] = '\0';
+       if (ret < 0)
+               goto error;
+       ((char *)buffer)[*size] = '\0';
        close(fd);
        return buffer;
+error:
+       free(buffer);
+       close(fd);
+       return NULL;
 }
 
 void release_file(void *data, unsigned long size __attribute__((unused)))

_______________________________________________
Xen-changelog mailing list
Xen-changelog@xxxxxxxxxxxxxxxxxxx
http://lists.xensource.com/xen-changelog


 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.