[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [PATCH] (getdelim): Don't leak memory upon failed realloc.
From: |
Simon Josefsson |
Subject: |
Re: [PATCH] (getdelim): Don't leak memory upon failed realloc. |
Date: |
Sun, 02 Mar 2008 17:11:45 +0100 |
User-agent: |
Gnus/5.110007 (No Gnus v0.7) Emacs/22.1 (gnu/linux) |
Jim Meyering <address@hidden> writes:
> Hi Simon,
>
> I noticed that our getdelim replacement can leak when the initial realloc
> fails. While that can happen only when *N is 0 and *LINEPTR is malloc'd,
> I think it's worth fixing, if only to avoid further discussion on the topic.
>
> Here's the fix.
>
> Ok to apply?
Yes, please do. Thanks for careful code review.
/Simon
> * lib/getdelim.c (getdelim): Don't leak memory upon failed realloc.
>
> diff --git a/lib/getdelim.c b/lib/getdelim.c
> index 0547c7f..7c6f326 100644
> --- a/lib/getdelim.c
> +++ b/lib/getdelim.c
> @@ -69,13 +69,15 @@ getdelim (char **lineptr, size_t *n, int delimiter, FILE
> *fp)
>
> if (*lineptr == NULL || *n == 0)
> {
> + char *new_lineptr;
> *n = 120;
> - *lineptr = (char *) realloc (*lineptr, *n);
> - if (*lineptr == NULL)
> + new_lineptr = (char *) realloc (*lineptr, *n);
> + if (new_lineptr == NULL)
> {
> result = -1;
> goto unlock_return;
> }
> + *lineptr = new_lineptr;
> }
>
> for (;;)
> --
> 1.5.4.3.341.g956c8c