ia64/xen-unstable

changeset 8640:45f3b9cfbd83

fixup memory leak and return value, if malloc or realloc fail.

Signed-off-by: Vincent Hanquez <vincent@xensource.com>
author vhanquez@kneesa.uk.xensource.com
date Tue Jan 24 10:55:45 2006 +0000 (2006-01-24)
parents b89b94c71b2f
children c70109e64c9e
files tools/xenstore/utils.c
line diff
     1.1 --- a/tools/xenstore/utils.c	Tue Jan 24 10:52:52 2006 +0000
     1.2 +++ b/tools/xenstore/utils.c	Tue Jan 24 10:55:45 2006 +0000
     1.3 @@ -96,21 +96,29 @@ void *grab_file(const char *filename, un
     1.4  		return NULL;
     1.5  
     1.6  	buffer = malloc(max+1);
     1.7 +	if (!buffer)
     1.8 +		goto error;
     1.9  	*size = 0;
    1.10  	while ((ret = read(fd, buffer + *size, max - *size)) > 0) {
    1.11  		*size += ret;
    1.12  		if (*size == max) {
    1.13 +			void *nbuffer;
    1.14  			max *= 2;
    1.15 -			buffer = realloc(buffer, max + 1);
    1.16 +			nbuffer = realloc(buffer, max + 1);
    1.17 +			if (!nbuffer)
    1.18 +				goto error;
    1.19 +			buffer = nbuffer;
    1.20  		}
    1.21  	}
    1.22 -	if (ret < 0) {
    1.23 -		free(buffer);
    1.24 -		buffer = NULL;
    1.25 -	} else
    1.26 -		((char *)buffer)[*size] = '\0';
    1.27 +	if (ret < 0)
    1.28 +		goto error;
    1.29 +	((char *)buffer)[*size] = '\0';
    1.30  	close(fd);
    1.31  	return buffer;
    1.32 +error:
    1.33 +	free(buffer);
    1.34 +	close(fd);
    1.35 +	return NULL;
    1.36  }
    1.37  
    1.38  void release_file(void *data, unsigned long size __attribute__((unused)))