[PATCH:xmessage] Stop leaking temporary buffer when realloc fails to enlarge it

walter harms wharms at bfs.de
Tue Dec 10 00:13:51 PST 2013



Am 10.12.2013 06:30, schrieb Alan Coopersmith:
> Fixes cppcheck error:
> [readfile.c:108]: (error) Common realloc mistake: 'cp' nulled
>  but not freed upon failure
> 

Reviewed-by: wharms <wharms at bfs.de>

> Signed-off-by: Alan Coopersmith <alan.coopersmith at oracle.com>
> ---
>  readfile.c |    2 ++
>  1 file changed, 2 insertions(+)
> 
> diff --git a/readfile.c b/readfile.c
> index 9729854..122020f 100644
> --- a/readfile.c
> +++ b/readfile.c
> @@ -104,9 +104,11 @@ get_data_from_stdin (int *len_return)
>  	count += n;
>  	/* Here count <= allocated. Prepare for next round. */
>  	if (count + BUFSIZ > allocated) {
> +	    char *oldp = cp;
>  	    allocated = 2 * allocated;
>  	    cp = realloc (cp, allocated + 1);

just nitpicking:
i would go for
	cp = realloc (oldp, allocated + 1);

my 2 cents,
re,
 wh

>  	    if (!cp) {
> +		free(oldp);
>  		fprintf(stderr, "cannot get memory for message file\n");
>  		return NULL;
>  	    }


More information about the xorg-devel mailing list