git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Junio C Hamano <gitster@pobox.com>
To: Stefan Beller <sbeller@google.com>
Cc: git@vger.kernel.org
Subject: Re: [PATCH] read-cache.c: free cache entry when refreshing fails
Date: Tue, 17 Feb 2015 10:14:54 -0800	[thread overview]
Message-ID: <xmqqoaos2yn5.fsf@gitster.dls.corp.google.com> (raw)
In-Reply-To: <1424196374-29509-1-git-send-email-sbeller@google.com> (Stefan Beller's message of "Tue, 17 Feb 2015 10:06:14 -0800")

Stefan Beller <sbeller@google.com> writes:

> This fixes a memory leak, when building the cache entries as
> refresh_cache_entry may decide to return NULL in case of
>

in case of what?

I briefly wondered if refresh_cache_ent() should do the freeing but
that does not make sense at all if done unconditionally because the
other caller of the function does want to retain ce on error, and it
makes little sense to invent FREE_CE_ON_ERROR bit in refresh_option,
either, so this fix looks sensible.

> I am not sure how severe this memory leak is as it was found by
> scan.coverity.com, CID 290041.
>
> Signed-off-by: Stefan Beller <sbeller@google.com>
> ---
>  read-cache.c | 10 ++++++++--
>  1 file changed, 8 insertions(+), 2 deletions(-)
>
> diff --git a/read-cache.c b/read-cache.c
> index 9cff715..8d71860 100644
> --- a/read-cache.c
> +++ b/read-cache.c
> @@ -725,7 +725,7 @@ struct cache_entry *make_cache_entry(unsigned int mode,
>  		unsigned int refresh_options)
>  {
>  	int size, len;
> -	struct cache_entry *ce;
> +	struct cache_entry *ce, *ret;
>  
>  	if (!verify_path(path)) {
>  		error("Invalid path '%s'", path);
> @@ -742,7 +742,13 @@ struct cache_entry *make_cache_entry(unsigned int mode,
>  	ce->ce_namelen = len;
>  	ce->ce_mode = create_ce_mode(mode);
>  
> -	return refresh_cache_entry(ce, refresh_options);
> +	ret = refresh_cache_entry(ce, refresh_options);
> +	if (!ret) {
> +		free(ce);
> +		return NULL;
> +	} else {
> +		return ret;
> +	}
>  }
>  
>  int ce_same_name(const struct cache_entry *a, const struct cache_entry *b)

  reply	other threads:[~2015-02-17 18:15 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-02-17 18:06 [PATCH] read-cache.c: free cache entry when refreshing fails Stefan Beller
2015-02-17 18:14 ` Junio C Hamano [this message]
2015-02-17 18:27   ` (unknown), Stefan Beller

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=xmqqoaos2yn5.fsf@gitster.dls.corp.google.com \
    --to=gitster@pobox.com \
    --cc=git@vger.kernel.org \
    --cc=sbeller@google.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).