[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [PATCH v2 2/4] fix a realloc-related memory leak in localhost()
From: |
Thomas Schwinge |
Subject: |
Re: [PATCH v2 2/4] fix a realloc-related memory leak in localhost() |
Date: |
Wed, 07 Sep 2011 17:19:12 +0200 |
User-agent: |
Notmuch/0.7-57-g64222ef (http://notmuchmail.org) Emacs/23.3.1 (i486-pc-linux-gnu) |
Hi!
On Mon, 15 Aug 2011 22:10:07 +0200, Jonathan Neuschäfer <j.neuschaefer@gmx.net>
wrote:
> * libshouldbeinlibc/localhost.c (localhost): assign the return value
> of realloc to a temporary variable to avoid losing the old value of
> buf in the case of realloc failing.
> ---
> libshouldbeinlibc/localhost.c | 21 ++++++++++++++-------
> 1 files changed, 14 insertions(+), 7 deletions(-)
>
> diff --git a/libshouldbeinlibc/localhost.c b/libshouldbeinlibc/localhost.c
> index f0c6754..f21f970 100644
> --- a/libshouldbeinlibc/localhost.c
> +++ b/libshouldbeinlibc/localhost.c
> @@ -39,18 +39,25 @@ localhost ()
> errno = 0;
>
> if (buf) {
> + char *new;
> buf_len += buf_len;
> - buf = realloc (buf, buf_len);
> + new = realloc (buf, buf_len);
> + if (! new)
> + {
Am I being dense (which is entirely possible), or did you mean to ``free
(buf)'' here, or what is the intent here?
> + errno = ENOMEM;
> + return 0;
> + }
> + else
> + buf = new;
> } else {
> buf_len = 128; /* Initial guess */
> buf = malloc (buf_len);
> + if (! buf)
> + {
> + errno = ENOMEM;
> + return 0;
> + }
> }
> -
> - if (! buf)
> - {
> - errno = ENOMEM;
> - return 0;
> - }
> } while ((gethostname(buf, buf_len) == 0 && !memchr (buf, '\0',
> buf_len))
> || errno == ENAMETOOLONG);
>
Grüße,
Thomas
pgpLTJAzFsjQT.pgp
Description: PGP signature
- Re: [PATCH v2 2/4] fix a realloc-related memory leak in localhost(),
Thomas Schwinge <=