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

Peter Hutterer peter.hutterer at who-t.net
Mon Dec 9 22:57:26 PST 2013


On Mon, Dec 09, 2013 at 09:30:21PM -0800, Alan Coopersmith wrote:
> Fixes cppcheck error:
> [readfile.c:108]: (error) Common realloc mistake: 'cp' nulled
>  but not freed upon failure
> 
> Signed-off-by: Alan Coopersmith <alan.coopersmith at oracle.com>

Reviewed-by: Peter Hutterer <peter.hutterer at who-t.net>

Cheers,
   Peter

> ---
>  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);
>  	    if (!cp) {
> +		free(oldp);
>  		fprintf(stderr, "cannot get memory for message file\n");
>  		return NULL;
>  	    }
> -- 
> 1.7.9.2
> 
> _______________________________________________
> xorg-devel at lists.x.org: X.Org development
> Archives: http://lists.x.org/archives/xorg-devel
> Info: http://lists.x.org/mailman/listinfo/xorg-devel
> 


More information about the xorg-devel mailing list