[pacman-dev] [PATCH] pacman/util.c: Fix memory leak if realloc fails

Allan McRae allan at archlinux.org
Sat Aug 8 00:34:24 UTC 2015


On 21/07/15 05:34, Rikard Falkeborn wrote:
> ---
>  src/pacman/util.c | 7 +++++--
>  1 file changed, 5 insertions(+), 2 deletions(-)
> 

Ack.   I better there is other unhandled realloc fails...

> diff --git a/src/pacman/util.c b/src/pacman/util.c
> index dd6e218..3d71d8b 100644
> --- a/src/pacman/util.c
> +++ b/src/pacman/util.c
> @@ -1449,11 +1449,14 @@ int multiselect_question(char *array, int count)
>  			size_t len;
>  			/* handle buffer not being large enough to read full line case */
>  			while(*lastchar == '\0' && lastchar[-1] != '\n') {
> +				char *new_response;
>  				response_len += response_incr;
> -				response = realloc(response, response_len);
> -				if(!response) {
> +				new_response = realloc(response, response_len);
> +				if(!new_response) {
> +					free(response);
>  					return -1;
>  				}
> +				response = new_response;
>  				lastchar = response + response_len - 1;
>  				/* sentinel byte */
>  				*lastchar = 1;
> 


More information about the pacman-dev mailing list