[sheepdog] [PATCH v2] dog/farm/slice: fix memory leak in dog cluster snapshot load command

Liu Yuan namei.unix at gmail.com
Wed Apr 23 08:49:19 CEST 2014


On Wed, Apr 23, 2014 at 12:08:30PM +0800, Ruoyu wrote:
> In normal branch, we should free(file->slices) directly.
> In error branch, we should check it first in case of slices not
> allocated.
> 
> Signed-off-by: Ruoyu <liangry at ucweb.com>
> ---
>  dog/farm/slice.c | 5 +++++
>  1 file changed, 5 insertions(+)
> 
> diff --git a/dog/farm/slice.c b/dog/farm/slice.c
> index 77db771..490a982 100644
> --- a/dog/farm/slice.c
> +++ b/dog/farm/slice.c
> @@ -102,10 +102,15 @@ void *slice_read(const unsigned char *sha1, size_t *outsize)
>  
>  	object = xmalloc(*outsize);
>  	strbuf_copyout(&buf, object, *outsize);
> +	free(file->slices);
>  	free(file);
>  	strbuf_release(&buf);
>  	return object;
>  err:
> +	if (file->slices) {
> +		free(file->slices);
> +		file->slices = NULL;
> +	}

free() can handle NULL parameter perfectly so no need to check file->slices.
Simply free(file->slices) is okay.

Thanks
Yuan



More information about the sheepdog mailing list