aboutsummaryrefslogtreecommitdiffstats
path: root/tools/xenstore/utils.c
diff options
context:
space:
mode:
authorvhanquez@kneesa.uk.xensource.com <vhanquez@kneesa.uk.xensource.com>2006-01-24 10:55:45 +0000
committervhanquez@kneesa.uk.xensource.com <vhanquez@kneesa.uk.xensource.com>2006-01-24 10:55:45 +0000
commitcaeab9b2e04d046e86a003c8ed387fe6d63be641 (patch)
tree67047fe35b4a7a1980f932746fdd5f01956ecb54 /tools/xenstore/utils.c
parent69f3c56296800ab0283a35a599c0bdd0e00443b4 (diff)
downloadxen-caeab9b2e04d046e86a003c8ed387fe6d63be641.tar.gz
xen-caeab9b2e04d046e86a003c8ed387fe6d63be641.tar.bz2
xen-caeab9b2e04d046e86a003c8ed387fe6d63be641.zip
fixup memory leak and return value, if malloc or realloc fail.
Signed-off-by: Vincent Hanquez <vincent@xensource.com>
Diffstat (limited to 'tools/xenstore/utils.c')
-rw-r--r--tools/xenstore/utils.c20
1 files changed, 14 insertions, 6 deletions
diff --git a/tools/xenstore/utils.c b/tools/xenstore/utils.c
index f8bf989c22..6655777bcd 100644
--- a/tools/xenstore/utils.c
+++ b/tools/xenstore/utils.c
@@ -96,21 +96,29 @@ void *grab_file(const char *filename, unsigned long *size)
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)))