mirror of
https://github.com/xen-project/xen.git
synced 2026-10-29 18:50:05 +01:00
fixup memory leak and return value, if malloc or realloc fail.
Signed-off-by: Vincent Hanquez <vincent@xensource.com>
This commit is contained in:
1 parent
69f3c56296
commit
caeab9b2e0
1 file changed
+14
-6
+14
-6
@@ -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)))
|
||||
|
||||